4
votes

Objective: Application should be able to load a dll dynamically using LoadLibrary and call its exported function using GetProcAddress.

My dll class has a function returning a unique_ptr of the class type.

I want to export this function such that the application can call this function using getProcAddress after dll is loaded successfully.

Using extern "C" will not allow to use a C++ class(unique_ptr class template, in this case) in the function signature.

I know, If do not use extern "C", it will export the function ( via __declspec(dllexport) ) with a mangled name.

The client will not know the mangled name during the call to getProcAddress, so how will a client call this function?

Is there a way to export such a function?

1
"Is there a way to export such a function?" Of course, but only if you're willing to use the mangled/decorated name when using GetProcAddress. And that you use it from a C++ program where you can actually create the objects of the C++ classes. - Some programmer dude
@user2100866: Your client application is already forced to use the same compiler, std::unique_ptr is implementation-dependent. - MSalters
@user2100866 "that will force my client application to be built using the same compiler as my dll" - sorry, but that is a requirement no matter what you do, since your function returns a class type that depends on a specific implementation. That is not portable across compilers. "And, yes, the client is a C++ application and hence there is no issue in exporting the c++ class as return type" - yes, there is. The DLL and client MUST be compiled with the same compiler, and even the same standard library, to ensure the same implementation of the class, as well as the memory manager, is used... - Remy Lebeau
@user2100866 ... The binary layout of the class, and its runtime behaviors, between the DLL and client must be compatible . And you can't new memory inside the DLL and delete it inside the client app if they don't share the same memory manager. Just because the DLL and client are both compiled in any C++ compilers does not guarantee that. Even using the same compiler does not guarantee that. - Remy Lebeau
Tip: You can also provide a pair of old-fashioned C functions in the DLL, and a C++ header which wraps that in code compiler by the customer's choice of C++ compiler. std::unique_ptr supports custom deleters, which would be the second C function. - MSalters

1 Answers

2
votes

Yes, the old-fashioned way. Looking up functions by name is the "modern" way (although it predates Windows 95). The old way is to look them up by ordinal.

You'll need to provide a module definition file with EXPORTS to number your functions.