0
votes

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.

1
I have a question, why are you getting your function from the .dll dynamically, instead of including the .dlls header file, and linking to the .dlls lib file?Algirdas Preidžius
Hard to guess what LIBRARY_API might be. Using the /EXPORT linker option is one way to export an identifier from a DLL. It is the most painful way to do it. The next lesser painful way is by using a .def file. By far the easiest is to apply the __declspec(dllexport) attribute in source code. Take your pick, consider that easiest is the easier way.Hans Passant
What problems are you having? GetProcAddress returning NULL?1201ProgramAlarm
@AlgirdasPreidžius I'm testing out runtime-compiled C++.Kookehs
@1201ProgramAlarm Yes, that is exactly what it is returning. Therefore I'm unsure if I am using _declspec(dllexport) in the correct manner.Kookehs

1 Answers

2
votes

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.)