Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native VC++ using external (not project) dll reference how to specify path to dll

I have a native VC++ project that uses a dll (which is not in a project). Now, I must to put the dll in one the "Search Path Used by Windows to Locate a DLL" link

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

So my only option according to that is adding the path to the %PATH% environment variable.

Is there any other way?

Is there an elegant way to do so (adding to PATH)? should I do this on installation? should I be concerned if i'm doing this?

like image 679
Hanan Avatar asked Dec 18 '22 10:12

Hanan


2 Answers

Summing up all the techniques I have found:

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

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

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

when debugging in VS the appPath doesn't 'work' use properties->debug->environment and merge environment variables link

  • If you use a native: do explicit linking - seems like big work for something simple, maybe use appPath registery key on deployment link, nobody had a tested-and-proved answer
like image 173
Hanan Avatar answered Jan 13 '23 23:01

Hanan


Here are a few suggestions:

  • You could embed the dll as a resource in your main executable and then extract it to a temporary directory and load it from their using LoadLibrary and then get the relevant function addresses using GetProcAddress.

  • You could modify you main processes search path using SetDllDirectory() to include the location of the DLL. This avoids having to make any global changes to the system. And again use LoadLibrary/GetProcAddress to resolve the function addresses.

like image 26
QAZ Avatar answered Jan 13 '23 22:01

QAZ