Picking a dll in a particular path while loading a shared library in Windows

I am using MinGW to create a shared library that is dynamically linked and loaded using the LoadLibrary function. My shared library is dependent on a dll that can be found in two different places on the path. These two are different (two different versions) and at the time of loading the library, it picks the wrong dll. How can I explicitly specify which dll to pick?

To be more specific these are the commands I am using:

Compilation

g++ -m64 -O3 -c my_file.cpp -o myfile.o

Creating shared library

g++ -m64 -shared myfile.o -o myfile.dll

I have tried many things including -L option but have not yet been able to figure out how to do this. I also have searched online for a couple of hours but it turn out to be of no use.

Any help is much appreciated.

Thanks in advance


It turns out that the dll I don't want to load is indeed at "The directory where the executable module for the current process is located." The other dll, with the same name is on the path but not where windows looks first. From what this document says, LoadLibrary will always pick the dll from the directory where the executable module for the current process is located, if the dll exists there, no matter what. Is there a way around this? Moving the dlls is really not an option as this is a third party program that we are building on


The chronological order of, how windows looks for DLLs is explained here step by step

https://msdn.microsoft.com/en-us/library/7d83bc18.aspx

After all steps in above link, when it comes to PATH environment variable, windows looks through each directory listed in PATH variable, from left to right.

First try if you can place the correct DLL in any of the locations as listed in the above link. Because path environment variable is the last thing that gets looked into.

If the above step is not possible, then ensure the correct DLL path is on the left of the wrong DLL path in PATH environment variable .


The following article gives some guidance for effecting alternate DLL search strategies using the SetDllDirectory and LoadLibraryEx API calls, assuming those are options (you mentioned LoadLibrary is currently used).

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx


It turns out that there is another windows function called LoadLibraryEx that allows to explicitly specify a path where the libraries will get loaded. You can learn more about it here:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx

Specifically, as the docment mentions:

If this value is used and lpFileName specifies an absolute path, the system uses the alternate file search strategy discussed in the Remarks section to find associated executable modules that the specified module causes to be loaded.

and

If lpFileName specifies an absolute path and dwFlags is set to LOAD_WITH_ALTERED_SEARCH_PATH, LoadLibraryEx uses the altered search path.

链接地址: http://www.djcxy.com/p/44508.html

上一篇: 如何从Inno Setup脚本调用.NET DLL?

下一篇: 在Windows中加载共享库时在特定路径中采用dll