Search code examples
c#c++dllloadlibraryborland-c++

Is it possible to use LoadLibrary from a dll in Borland C++ from a given path?


I have a C++ dll which loads a C++ CLI library using LoadLibrary function as follows:

HMODULE myDLL = LoadLibrary("DLLtoBeLoaded.dll");

and this works. But I want my "DLLtoBeLoaded.dll" in a different directory(Different from the executable directory). So I tried:

HMODULE myDLL = LoadLibrary("C:\\DLLtoBeLoaded.dll");

This does not work. Although myDLL is not null after this but DLLtoBeLoaded.dll's constructor is not called.

EDIT

MyDLL is not null because LoadLibrary is actually successful. But the reason why it did not seem to work was that the DLLtoBeLoaded.dll references some dlls which are also not in executable directory(The program works if I just copy referenced dlls in executable folder). Is it possible to keep the referenced dlls in different folder from executable?


Solution

  • It is possible to reference dlls from a different folder than the executable. Whenever the runtime fails to load the referenced assemblies, it fires Assembly Resolver event. I got the basic idea from this link. There are other methods too (probing and code base). But I liked this one the most.