dxwrapper icon indicating copy to clipboard operation
dxwrapper copied to clipboard

LoadLibrary in DLLMain is expressly forbidden

Open bo3b opened this issue 5 years ago • 1 comments

Just noting a fairly serious problem with your code structure.

In Dllmain.cpp, during Attach, you call LoadLibrary. This is expressly forbidden by Microsoft:

https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain

The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary) during process termination, because this can result in a DLL being used after the system has executed its termination code.

This can cause the wrapper to crash or hang. I see that you try to work around the problem by increasing the thread priority, but this is not a good idea. You'll still have scenarios where it will crash or hang.

Best strategy is to do a minimalist DLLMain code, not even logging there, to avoid the limited runtime. Since you are using Detours, you can make a later init at first code instruction. Or you can do what we do in 3Dmigoto, which is to only init upon first active API call. (late-binding)

bo3b avatar Dec 30 '19 01:12 bo3b

Agreed. So far I have not run into any issues doing this. Fixing this would not be very easy, but I have a few ideas how to fix this. Probably won't happen very soon.

elishacloud avatar Sep 28 '20 00:09 elishacloud