Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable automatic DLL loading in C++

My scenario is as follows: my application depends on a certain DLL (I use it's lib during linkage). However, when my application executes, I want to explicitly load that DLL using LoadLibrary. However, by default, when the code reaches a scope where that DLL is needed, the environment automatically look it up, and then loads it. I want to disable this behavior, and for all I care, if the application reached a point where it wants to execute code that belongs to that DLL, I prefer that it will crash instead of loading it automatically (So the DLL will be loaded only because I explicitly called LoadLibrary).
In the meanwhile, I'm using the delay-load ability (so the load trigger will occur only when the DLL actually needs to be loaded). However, I would prefer that the application will just crash if the DLL wasn't loaded yet.

Perhaps anyonehere is familiar with a way to achieve this?


2 Answers

If you want to use LoadLibrary, then don't link application with the import library. PE format doesn't support unresolved externals, so you either use headers and dllimport, or LoadLibrary, GetProcAddress and pointers to functions.

like image 87
Cat Plus Plus Avatar answered Sep 13 '25 23:09

Cat Plus Plus


(I use it's lib during linkage)

If you want to load it manually using LoadLibrary and GetProcAddress then you shouldn't pass its *.lib file to your linker.

like image 43
ChrisW Avatar answered Sep 14 '25 00:09

ChrisW