Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To avoid dll pre-loading I tried to use SetDllDirectory("") as it is mentioned in MSDN to remove current directory from search path.

The directory to be added to the search path. If this parameter is an empty string (""), the call removes the current directory from the default DLL search order. If this parameter is NULL, the function restores the default search order.
Surprisingly it didn't work. I can see dlls are still getting loaded from current directory if dll present there. I also tired to set dll directory path as system 32 path, but still it picks dll from current directory. Lastly I decide to get all the modules loaded in my application and reloading them again. My code looks like this,


Do you see any problem with my code?

What I have tried:

My Complete POC code,

int main( void )
    LPWSTR s = new WCHAR[100];
    HINSTANCE myDLL = LoadLibrary(L"TestLibrary.dll");
    //myDLL returns non-null, there is file in current dir and not at any other location, it should have return null.
    return 0;
Updated 1-Jun-17 20:12pm
Jochen Arndt 2-Jun-17 3:26am    
"I can see dlls are still getting loaded from current directory if dll present there."

Maybe the current directory is one of the directories from the search order (e.g. the directory from where the application is loaded).

1 solution

You can use the delay-load feature of Microsoft. Here is an article about Delay Loading a DLL.

And you wont change that behavoir of Windows, but you can move the dll in another or sub directory to avoid the pre-loading. This needs than really some dll loading.

But what is the real problem with your dll. May some implementation in your dll is weired.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900