gpt4 book ai didi

c++ - 使用 __declspec(dllexport) 而不是 -EXPORT :

转载 作者:行者123 更新时间:2023-11-28 02:14:42 24 4
gpt4 key购买 nike

我正在查看有关导出函数的文档,它指出 __declspec(dllexport) 应该在命令行版本 -EXPORT: 之前使用,如果可能的话。我目前正在使用命令行变体。在尝试进行这些更改时,我试图了解正确的实现方式,但遇到了问题。

DLL的头文件:

#ifdef LIBRARY_EXPORTS
#define LIBRARY_API __declspec(dllexport)
#else
#define LIBRARY_API __declspec(dllimport)
#endif

#define PRINT_TEST(name) LIBRARY_API void name()
typedef PRINT_TEST(print_log);
// ^ What's the C++11 equivalent with the using keyword?

DLL的源文件:

PRINT_TEST(PrintTest) {
std::cout << "Testing DLL" << std::endl;
}

应用程序的源文件:

print_test* printTest = reinterpret_cast<print_test*>(GetProcAddress(testDLL, "PrintTest"));

问题是因为 __declspec(dllexport) 包含在 typedef 中吗?因此,应用程序源文件中的语句实际上是:

__declspec(dllexport) void (*print_test)() printTest = reinterpret_cast<print_test*>(GetProcAddress(testDLL, "PrintTest"));

我没有收到任何编译器错误或警告。

最佳答案

问题是因为您正在导出一个 C++ 函数,它有一个损坏的名称。您要么需要将该损坏的名称传递给 GetProcAddress(从来都不是有趣的),要么您需要在函数声明中使用 __stdcall 取消导出

LIBRARY_API __stdcall void PrintTest

或使用 extern "C"__stdcall 更简单,并将调用约定从 C++ 风格更改为 C 风格。 (由于 C 函数名称的导出方式,这可能需要将“_PrintTest”传递给 GetProcAddress。)

关于c++ - 使用 __declspec(dllexport) 而不是 -EXPORT :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34365367/

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