c++dllloadlibrarydynamic-loadingdelay-load

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?


Solution

  • 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.