Why am i getting "LoaderLock" detection?

281 Views Asked by At

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!

0

There are 0 best solutions below