Load a DLL from another directory at program start

后端 未结 6 1034
予麋鹿
予麋鹿 2021-02-03 11:43

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 Other

相关标签:
6条回答
  • 2021-02-03 12:11

    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.

    0 讨论(0)
  • 2021-02-03 12:12

    I have the same problem with one application I am working on.

    I do not want to use runtime loading because there are tens of functions I would need to manually create function pointer for.

    Mr Dibling's mention of manifest file opened a new door for me but I sadly found out that the oldest version of windows that supports the feature is Windows 7. It won't even work on Vista.

    Long story short, a friend familiar with Windows Application development told me to look up Delay-Loaded DLL, which turns out to solve the problem perfectly with minimal effort. It delays the loading of DLL library to either the point you manually do, or the first time its function is called. So you just need to add your DLL path to the search path before that happens, where SetDllDirectory helps.

    Here is the steps to make it work:

    1) Specify the DLL to be delay-loaded to linker, either through your makefile, cmake or VS property page (Linker->Input of VS2015)

    2) Call SetDllDirectory at the beginning of your program, before any call to the DLL is made.

    Delay-loaded DLL is supported all the way back to VC6. SetDllDirectory is supported after XP SP1.

    0 讨论(0)
  • 2021-02-03 12:27

    You can add the directory where the dll is located to the PATH environment variable.

    0 讨论(0)
  • 2021-02-03 12:29

    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.
    0 讨论(0)
  • 2021-02-03 12:33

    Use Symbolic Links to the 3rd Party Executables

    I found the approach advocated by Aaron Margosis useful. See:

    Using NTFS Junctions to Fix Application Compatibility Issues on 64-bit Editions of Windows

    Essentially, create symbolic links to each of the dependent 3rd Party executables. Place these symbolic link files in and amongst your own dependent executable files. Except for filename changes to the targets, the 'soft' symbolic links will resolve the load-time dependencies even as the target of the links are changed by future updates.

    0 讨论(0)
  • 2021-02-03 12:35

    I have struggled with the same problem and also found a dead end with the suggested methods like LoadLibrary, SetDllDirectory, Qt's addLibraryPath and others. Regardless of what I tried, the problem still remained that the application checked the libraries (and didn't find them) before actually running the code, so any code solution was bound to fail.

    I almost got desperate, but then discovered an extremely easy approach which might also be helpful in cases like yours: Use a batch file! (or a similar loader before the actual application)

    A Windows batch file for such a purpose could look like this:

    @echo off
    PATH=%PATH%;<PATH_TO_YOUR_LIB>
    <PATH_TO_YOUR_APP_EXE>
    

    /edit: Just saw @SirDarius comment in Luchian's answer which describes that way, so just take my batch code bit as a reference and all credits go to him.

    0 讨论(0)
提交回复
热议问题