0

i am calling a c++ DLL (not my code) from my c# code using my P/Invoked LoadLibrary API. but when i run the application, a LoaderLock error is being detected.

at first i blame my DllImport C# wrappers for this, but when i try to load other C++ DLL, it works fine.

i've read some articles online regarding LoaderLock and it seems the #1 rule is not to do anything funny inside the DllMain. and when i checked the c++ DllMain is calling some APIs in DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH.

BOOL APIENTRY DllMain(
    HANDLE  hModule, 
    DWORD   ul_reason_for_call, 
    LPVOID  lpReserved )
{
    switch (ul_reason_for_call) {
        case DLL_PROCESS_ATTACH:

                StartAPI();
                break;

        case DLL_THREAD_ATTACH:
                break;

        case DLL_THREAD_DETACH:
                break;

        case DLL_PROCESS_DETACH:

                ExitAPI();
                break;
    }
    return true;
}

when i checked StartAPI(), it has a big chunk of code and calls a function that creates a proccess using CreateProccessA

is this the real reason as to why im getting a LoaderLock Error?

i was told that the StartAPI() should run upon the initialization of the DLL, is there any other way to make this work?

i was planning to call StartAPI() in another thread, but i've read that creating a thread inside DllMain is also a recipe for a disaster.

thanks!

Rhen Bon
  • 87
  • 7
  • 3
    Per [MSDN documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971.aspx), `CreateProcess` is NOT safe to call in `DllMain`, and `CreateThread` is risky if you are not careful with it. The best solution is for the DLL to export `StartAPI` and `ExitAPI` and require the app to call them after the DLL has been loaded and before unloading it. Clearly the DLL is doing things that are not safe to automate. – Remy Lebeau Aug 23 '17 at 04:50
  • but this dll has been tested and used for a long time in c++ applications without the LoaderLock error ever occurring. LoaderLock error just occurred when we interoperate with it in C# winforms. – Rhen Bon Aug 24 '17 at 00:43
  • https://stackoverflow.com/a/2480657/65863 – Remy Lebeau Aug 24 '17 at 01:02
  • And BTW, since the DLL does not actually do anything for `DLL_THREAD_(ATTACH|DETACH)`, consider making `StartAPI()` call [`DisableThreadLibraryCalls()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682579.aspx) when called by `DLL_PROCESS_ATTACH`. – Remy Lebeau Aug 24 '17 at 01:06
  • Well that's how it goes. Different hosts may or may not run into problems. You know the code is broken. You know how it must be fixed. Fix it. – David Heffernan Aug 24 '17 at 03:45
  • Okay, i tried to remove the function calls inside the DLLMain. The LoaderLock error did not occur when i called LoadLibrary. I will call APIStart after I loaded the target DLL. but when should i call APIExit? should i call it in the destructor of the class who loads the dll? – Rhen Bon Aug 25 '17 at 01:46

0 Answers0