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

Using __declspec(dllexport) instead of -EXPORT:

本文关键字:-EXPORT declspec dllexport 使用      更新时间:2023-10-16

我正在查看导出函数的文档,其中指出__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"));

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

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

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

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

LIBRARY_API __stdcall void PrintTest

或者使用CCD_ 1。__stdcall更简单,并将调用约定从C++样式更改为C样式。(这可能需要将"_PrintTest"传递到GetProcAddress,因为C函数名是如何导出的。)