LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Giving the path to a DLL in the EXE ?

Solved!
Go to solution

Hello all,

I'm rusty at this stuff. I feel like this has been covered over and over but i can't find the obvious solution.

I have a LW executable, it uses a DLL via an export library. But I don't want to copy the DLL in the program directory or the system directory, it needs to be installed in its own separate directory. How do I set the path to that DLL from within the program ?

Unless I'm missing the obvious, this page (https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order) doesn't seem to cover this simple case. That's not "from within", but on Linux you can simply call the program with "LD_LOAD_LIBRARY=/path/to/dynamic/lib MyProg"

Thanks

0 Kudos
Message 1 of 4
(123 Views)

Hi Gdargaud,

 

You can explicitely add your DLL directory to the search path at runtime using SetDllDirectory().

Here you can find a small code generated by AI to explain you how it works :

 

#include <windows.h>

int main()

{

                SetDllDirectory(L"C:\\My\\DLL\\Directory");

                // Now load your DLL implicitly or call into it via import lib

}


This will add the specified path to the search path for DLLs for the current process. It does not override the system path permanently, just for your running app.

However, it only works before the DLL is loaded. If it's linked statically via import lib (.lib), make sure this is called before any function calls or implicit loading.

 

Best regards,

 

Louis Zakarian

Message 2 of 4
(86 Views)
Solution
Accepted by gdargaud

@L.Zakarian wrote:

 

However, it only works before the DLL is loaded. If it's linked statically via import lib (.lib), make sure this is called before any function calls or implicit loading.


Actually if it is a statically linked import library the moment your process executable loads, these imports are already resolved and will make your starting fail if they can't be resolved, before your main() function gets invoked. You would have to have an import library with delay loading enabled, but that is not standard for import libraries.

 

Or you relocate all de relevant code into its own DLL and load that explicitly with LoadLibrary() during your main() initialization, after having determined your import path and setting the according import directory too for secondary dependencies. Another option is to LoadLibrary() your secondary dependency explicitly too before loading the main directory but if that secondary dependency has yet again other dependencies in that same directory, using SetDllDirectory() is the easier solution.

 

Windows DLL search order is a somewhat complicated topic and the new .Net rules are not easier but even more restrictive.

Rolf Kalbermatter
My Blog
Message 3 of 4
(74 Views)

So in other words, 'no' if it's from an import library, and complicated with LoadLibrary...

Thanks

0 Kudos
Message 4 of 4
(42 Views)