unknownv2 / CoreHook

A library that simplifies intercepting application function calls using managed code and the .NET Core runtime
MIT License
259 stars 40 forks source link

Fix: Retry EnumProcessModulesEx to avoid failures #119

Closed unknownv2 closed 5 years ago

unknownv2 commented 5 years ago

EnumProcessModulesEx can fail for reasons known as an operating system issue, so we increase the number of attempts we try so failures don't stop the injection process.

See here for more information:

https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs#L101

Also, EnumProcessModules is not a reliable method to get the modules for a process. If OS loader is touching module information, this method might fail and copy part of the data. This is no easy solution to this problem. The only reliable way to fix this is to suspend all the threads in target process. Of course we don't want to do this in Process class. So we just to try avoid the race by calling the same method 50 (an arbitrary number) times.