1

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()?

NmdMystery
  • 2,778
  • 3
  • 32
  • 60

1 Answers1

3

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.

Community
  • 1
  • 1
cf-
  • 8,598
  • 9
  • 36
  • 58
  • Is it safe to continue to allow name-mangling on a compile time linked dll, and to embellish on the question you linked, pass objects to and from it? Both the dll and the executable it's linked to will always be built with the same compiler. – NmdMystery Apr 26 '14 at 18:35
  • 2
    If you're linking to the DLL, name-mangling should be irrelevant since the compiler will automatically adjust any calls your EXE makes to access the correct function within the DLL. And as long as you're *certain* you'll always be building both the DLL and any calling EXEs with the same version of the same compiler, you should be able to safely pass objects to and from your DLL. – cf- Apr 26 '14 at 18:40
  • Of course no one can guarantee code will always be built the same compiler: the code lives on, programmers move on, new programmers come in, organisation goals shift, companies are sold etc etc. It is totally amazing how long code lives, typically outliving the wildest expectations of its creators. – nyholku Nov 09 '16 at 09:00