Load a DLL from another directory at program start

后端 未结 6 1023
予麋鹿
予麋鹿 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: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.

提交回复
热议问题