Kookehs
Kookehs

Reputation: 84

Using __declspec(dllexport) instead of -EXPORT:

I was looking at documentation for exporting functions, and it stated __declspec(dllexport) should be used before the command line version -EXPORT: if possible. I'm currently using the command line variant. In attempting to make these changes I'm trying to understand the correct implementation, but I'm running into problems.

DLL's header file:

#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's source file:

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

Application's source file:

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

Is the issue because of __declspec(dllexport) is included in the typedef? Therefore the statement in the application's source file actually is:

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

I'm not getting any compiler errors or warnings.

Upvotes: 0

Views: 821

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32727

The problem is because you're exporting a C++ function, which has a mangled name. You either need to pass that mangled name to GetProcAddress (never fun) or you need to unmangle the export using __stdcall in the function declaration

LIBRARY_API __stdcall void PrintTest

or with extern "C". __stdcall is simpler and changes the calling convention from C++ style to C style. (This may require passing "_PrintTest" to GetProcAddress because of how C function names are exported.)

Upvotes: 2

Related Questions