簡體   English   中英

使用.dll文件,相對於.exe的.dll文件編譯可執行文件

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

我有一個C ++項目,該項目對.dll文件的依賴性更高。 如何構建可執行文件,以便創建的.exe文件在相對於.exe的給定文件夾中找到.dll文件? 我正在使用Visual Studio。

您可以使用以下代碼加載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;
}

要從exe相對目錄加載dll,您要做的就是以"\\\\mydlldir\\\\dllnamehere.dll"的格式指定一個路徑,而不是"driveletter:\\\\dir\\\\dir2\\\\dirwithexeinit\\\\mydlldir\\\\dllnamehere.dll"的完全限定路徑。 "driveletter:\\\\dir\\\\dir2\\\\dirwithexeinit\\\\mydlldir\\\\dllnamehere.dll"

第一種方法將始終在exe所在位置指定的目錄中查找,第二種方法將始終在指定的確切目錄中查找。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM