Native VC ++ using a link to an external (non-design) DLL, how to specify the path to the dll

I have my own VC ++ project that uses dll (which is not in the project). Now I have to put the dll in one "Search path used by Windows to search the DLL" link

but I don’t want the dll to sit in exectuable or the current or Windows or system directory.

Thus, my only option according to this is to add the path to the% PATH% environment variable.

Is there another way?

Is there an elegant way to do this (adding in PATH)? Should I do this during installation? Should I be bothered if I do this?

+4
source share
8 answers

To summarize all the methods I found:

  • If you are using a managed project as a startup project (this is actually my case) use the Enviroment class

string temp = "myFullDirectoryPathToDll"; string temp2 = Environment.GetEnvironmentVariable ("PATH") + ";" + temp; Environment.SetEnvironmentVariable ("PATH", temp2);

and I think that MSDN should have emphasized that it changes the PATH environment variable only in this process.

when debugging VS the apppath application does not work, use properties-> debug-> environment and change the environment variables link

  • If you use native: make explicit links - it seems like a lot of work for something simple, perhaps use the apppath registration key when deploying the link , no one has a verified and reasonable answer.
+5
source

Here are some suggestions:

  • You can embed the DLL as a resource in your main executable, and then extract it to a temporary directory and load it using LoadLibrary, and then get the corresponding function addresses using GetProcAddress.

  • You can change the search path for the main processes using SetDllDirectory () to enable the location of the DLL. This avoids any global changes in the system. And again, use LoadLibrary / GetProcAddress to resolve function addresses.

+4
source

If you know where the DLL is located, you can try loading it at runtime using LoadLibrary (), and then use GetProcAddress () to bind to the functions you need to call.

+1
source

I would not be happy if the installed application added random things to my global PATH. Since it affects all applications and can have unpleasant side effects.

What I saw is starting a starter script.
The script looks and acts like an application to the user (so you still double his clock). But the script sets the appropriate path, then runs the real application.

+1
source

If you use DelayLoad, before calling any function that will cause the dll to load, call LoadLibrary. It will be a β€œsimple” application and it will not look for it. No need for GetProcAddress

+1
source

If you start from the Windows shortcut, you can specify the path to the DLL in the "Start" location by specifying the fully qualified name and .exe path in the "Target".

If there are DLLs in the .exe directory, Windows will also be able to find them, because I believe that the search order in the Windows DLL is first viewed in the .exe path (the current directory is the fifth in the list).

I have used the LoadLibrary / GetProcAddress method many times, I try to avoid it, as this entails additional work - a lot of typedefs and typecasts.

0
source

The delay loading method works with the working directory when it decides to call LoadLibrary . You can take advantage of this. For more information about the search order, see http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx .

0
source

I tried to set the path to the program in the registry. It works fine only when the user has permissions to regedit. As well as changing the PATH environment variable. My test user does not have administrator privileges to change the variable.

0
source

All Articles