如何将dll包含为相对路径?

How to include dll as relative path?

我的项目中有一个 dll,我通过绝对路径 (C:\test\something\abc.dll) 将其加载到我的项目中。如何相对加载它或者是否有更好的方法将它包含在 visual studio 项目中?

您可以指定不带路径的 dll 名称,并将 dll 放在加载它的可执行文件所在的同一目录中,或者放在当前目录(您启动程序的目录)中。

MSDN::

Before the system searches for a DLL, it checks the following:

  • If a DLL with the same module name is already loaded in memory, the system uses the loaded DLL, no matter which directory it is in. The system does not search for the DLL.

  • If the DLL is on the list of known DLLs for the version of Windows on which the application is running, the system uses its copy of the known DLL (and the known DLL's dependent DLLs, if any). The system does not search for the DLL. For a list of known DLLs on the current system, see the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs.

Dll 的标准搜索顺序是::

  1. The directory from which the application loaded.
  2. The current directory.
  3. The system directory.
  4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  5. The Windows directory.
  6. The directories that are listed in the PATH environment variable.

您始终可以将您的 Dll 与应用程序放在同一目录中。不建议将其作为应用程序中的资源携带。