C++ Loading a DLL

C++ Loading a DLL

本文关键字:DLL Loading C++      更新时间:2023-10-16

我即将开发应使用DLL分发的API。因为我无法直接测试我的代码,所以我必须将DLL加载到测试项目中并在此处运行。

我正在使用Visual Studios2015。在那里我进行了一个测试项目,在此中,我为DLL做了我的项目:

solution-tree" testproject"

  • dllproject

  • testProject

我导出一个虚拟功能,以测试是否能够加载DLL:

#ifndef EXPORT
#define EXPORT __declspec(dllexport)
#endif
extern "C" {
    EXPORT int dummy() {
        return 5;
    }
}

然后,在我的testproject中,我尝试加载DLL,提取功能并运行它:

#include <windows.h>
#include <iostream>
using namespace std;
typedef int (__stdcall *dll_func)();
int main() {
    HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("H:\path\to\project\Debug\DLLProject\DLLProject.dll"));
    if (hGetProcIDDLL == NULL) {
        cout << "DLL could not be loaded. " << GetLastError() << endl;
        return;
    }
    dll_func f = (dll_func)GetProcAddress(hGetProcIDDLL, "create");
    if (f == NULL) {
        cout << "Factory function could not be resolved. " << GetLastError() << endl;
        return;
    }
    cout << "Factory function returns: " << f() << endl;
}

我从这个问题中复制了几乎所有内容。不幸的是,当我运行testproject时,我的控制台打印出来:" DLL无法加载。4250"

在这一点上,我真的不知道该怎么办,因为此处描述的错误基本上什么也没说。有了一些研究,我无法得到任何答案...希望您可以帮助我:D

这可能是由链接器设置引起的。确保您在链接器设置中指定"/appContainer:no"。