Question

I have a set of APIs that are explicitly designed to be used only in C++. I do not expect them to be used by a C program (or any other language for that matter), and as such I export namespace and class information as opposed to going the extern "C" route and using inlined utility functions to call the plain C functions.

Right now I am only working on dlls that are linked at compile time, which means importing the functions to the executable is very easy as it involves no work on my part. However, I plan on developing a plugin system which will require me to load dlls dynamically at run time. Will I be able to find name-mangled C++ functions using GetProcAddress()?

Était-ce utile?

La solution

What you're doing is not necessarily a good idea unless you control the entire build chain and can ensure both your DLL and any apps using it are built with the same version of the same compiler.

With that said, yes, you can load name-mangled functions using GetProcAddress. Just use Dependency Walker or look at the generated .def file for your DLL, if your compiler is set to produce one, to get the mangled function name. Then you can GetProcAddress it. You cannot, however, call GetProcAddress with an unmangled name and expect it to find the right mangled name. For example, if your DLL's function is named Add, and is mangled to _Z3Addv, you would need to call GetProcAddress(myDLL, "_Z3Addv"); to access the function properly.

You would need to change the call to GetProcAddress every time you change the declaration of your function, since the mangled name would also change. Be aware you will also need to change the GetProcAddress call if you change the compiler your DLL is built with - MSVC's mangling is a lot different from GCC's, and clang's mangling is probably different from both of them. So you might want to reconsider the way you're doing this, since it seems rather prone to breaking somewhere along the way.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top