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

时间:2013-11-15 15:03:14

标签: c++ dll build dependencies exe

我有一个C ++项目,它对.dll文件有更多依赖。我如何构建可执行文件,以便创建的.exe文件找到相对于.exe的给定文件夹中的.dll文件? 我正在使用Visual Studio。

2 个答案:

答案 0 :(得分:1)

您可以使用此代码加载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;
}

答案 1 :(得分:1)

要从exe相对目录加载dll,您只需指定格式为"\\mydlldir\\dllnamehere.dll"的路径,而不是"driveletter:\\dir\\dir2\\dirwithexeinit\\mydlldir\\dllnamehere.dll"的完全限定路径。

第一种方法总是查看从exe存在的目录指定的目录,其中第二种方法总是在指定的确切目录中查找。