Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a DLL from another directory at program start

My basic issue is this: my program (MyProgram.exe) has a dependency on a DLL from another program (OtherProgram), and I'm trying to avoid repackaging a new DLL every time OtherProgram updates. I'd like to have MyProgram.exe link in OtherProgram's DLL when it launches, but I'm not completely sure that Windows allows for this. So if there is some kind of workaround that would also be acceptable.

And just for some background, the platform is Windows 7 x64, and MyProgram.exe runs fine when I create a symlink in the MyProgram.exe project directory to the DLL in OtherProgram's install directory. When I try to run it without the symlink, I get the "program can't start because OtherProgramDLL.dll is missing from your computer" error.

Any advice or links to relevant info is greatly appreciated!

EDIT: Clarification: the DLL is not linked at compile-time, this issue crops up at runtime

like image 728
CJ McAllister Avatar asked May 15 '12 18:05

CJ McAllister


2 Answers

You could use LoadLibrary, but you would need a way to guarantee the DLL's location. This Wikipedia article provides good example on how to use the DLL after it has been loaded.

like image 61
Eckstein Avatar answered Nov 11 '22 08:11

Eckstein


There are two types of dynamic linking in the Windows world:

  1. Load-Time linking is when a DLL is loaded automatically when your program starts up. Windows finds this DLL using a specific algorithm I'll discuss below.
  2. Run-Time linking is when you specifically load a DLL by calling LoadLibrary in your code. Similar rules apply as to how the library is found, but you can specify a fully-qualified or relatively-qualified path to control the search.

In the case of Load-Time linking, MS recommends that your program's DLLs are stored in and loaded from the same directory where your application is loaded from. If this is at all workable, this is probably your best option.

If that doesn't work, there are several other options, outlined here. One is to leverage the search order by putting the DLL in either the working directory or the directory where the application was loaded from.

You can change the working directory of an application by:

  1. Create a shortcut to your application.
  2. Bring up the shortcut's properties
  3. Edit the "Start in" property with the directory where the DLL is located.

When you launch your application using the shortcut, it will load the right DLL.

Other options for load-time linking include:

  • Adding a manifest to your application which specifies where your dependent assemblies are, or,
  • Setting the PATH.
like image 13
John Dibling Avatar answered Nov 11 '22 06:11

John Dibling