简体   繁体   English

使用.dll文件,相对于.exe的.dll文件编译可执行文件

[英]Compile executable with .dll files, .dll files relative to .exe

I have a C++ project that has more dependencies to .dll files. 我有一个C ++项目,该项目对.dll文件的依赖性更高。 How can I build the executable so, that the created .exe file finds the .dll files in a given folder relative to the .exe? 如何构建可执行文件,以便创建的.exe文件在相对于.exe的给定文件夹中找到.dll文件? I am using Visual Studio. 我正在使用Visual Studio。

You can use this code to load a dll and call a function exported in the dll: 您可以使用以下代码加载dll并调用在dll中导出的函数:

#include <windows.h>
#include <iostream>

/* Define a function pointer for our imported
 * function.
 * This reads as "introduce the new type f_funci as the type: 
 *                pointer to a function returning an int and 
 *                taking no arguments.
 */
typedef int (*f_funci)();

int main()
{
  HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Documents and Settings\\User\\Desktop  \\fgfdg\\dgdg\\test.dll");

  if (hGetProcIDDLL == NULL) {
    std::cout << "could not load the dynamic library" << std::endl;
    return EXIT_FAILURE;
  }

  # resolve function address here
  f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "funci");
  if (!funci) {
    std::cout << "could not locate the function" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "funci() returned " << funci() << std::endl;

  return EXIT_SUCCESS;
}

To load a dll from an exe relative directory all you have to do is specify a path in the format of "\\\\mydlldir\\\\dllnamehere.dll" as opposed to the fully qualified path of "driveletter:\\\\dir\\\\dir2\\\\dirwithexeinit\\\\mydlldir\\\\dllnamehere.dll" . 要从exe相对目录加载dll,您要做的就是以"\\\\mydlldir\\\\dllnamehere.dll"的格式指定一个路径,而不是"driveletter:\\\\dir\\\\dir2\\\\dirwithexeinit\\\\mydlldir\\\\dllnamehere.dll"的完全限定路径。 "driveletter:\\\\dir\\\\dir2\\\\dirwithexeinit\\\\mydlldir\\\\dllnamehere.dll"

The first method will always look in the directory specified from where the exe exists, where the second will always look in the exact directory specified. 第一种方法将始终在exe所在位置指定的目录中查找,第二种方法将始终在指定的确切目录中查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM