gpt4 book ai didi

c++ - 用.dll文件编译可执行文件,.dll文件相对于.exe

转载 作者:行者123 更新时间:2023-11-30 04:12:00 25 4
gpt4 key购买 nike

我有一个 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;
}

关于c++ - 用.dll文件编译可执行文件,.dll文件相对于.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20004040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com