6

I hope to LoadLibrary on an unmanaged C++ DLL with managed code, and then call GetProcAddress on extern functions which have been mangled. My question is are the mangled names you get from a C++ compiler deterministic? That is: Will the name always by converted to the same mangled name, if the original's signature hasn't changed?

agf
  • 171,228
  • 44
  • 289
  • 238
Blanthor
  • 2,568
  • 8
  • 48
  • 66
  • possible duplicate of [What is name mangling, and how does it work ?](http://stackoverflow.com/questions/1314743/what-is-name-mangling-and-how-does-it-work) – Kirill V. Lyadvinsky Jul 15 '10 at 06:35
  • 1
    As the compiler revs version it may change how it does name mangling. So as long as you stick with a specific compiler and a specific version you should be OK. Note some compiler provide in there runtime an API that allows you to convert to <--> from mangled names. You could look at how c++filt works to find out about gcc mangling libraries. – Martin York Jul 15 '10 at 15:03
  • @Martin. That's great information. I am confined to the Windows platforms. This does tell me that I need to prepare for breaking changes or find another way. – Blanthor Jul 17 '10 at 20:15
  • If you dynamically load a DLL and need to resolve symbols manually. It is usually a good idea to provide a C interface (as there is no mangling and the ABI is defined (on a per platform basis)). The C function can then return a pointer to a factory object that allows you to create all the C++ objects in the DLL. – Martin York Jul 17 '10 at 20:19

3 Answers3

6

It isn't specified by the standard, and has certainly changed between versions of the same compiler in my experience, though it has to be deterministic over some fixed set of circumstances, because otherwise there would be no way to link two separately compiled modules.

If you're using GetProcAddress, it would be far cleaner to export the functions as extern "C" so their names are not mangled.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • 4
    A particular version of a compiler will mangle names consistently, otherwise it wouldn't be able to link with things it produced. Other than that, all bets are off. – Eltariel Jul 15 '10 at 06:36
2

It's compiler specific, as others have said. However, you can find details in a document by Agner Fog...

http://www.agner.org/optimize/#manuals

See item 5 on that page.

Also, these days, there are libraries which can handle mangling and demangling for common compilers for you. For Visual C++, the starting point would be the dbghelp and imagehlp libraries.

http://msdn.microsoft.com/en-us/library/ms679292%28v=VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms680321%28v=VS.85%29.aspx

0

Name mangeling is handled differently by every compiler (maybe or not-there is no standard). If you use pure C functions in your C++ code, you can use the extern "C" to supress name mangeling for the C functions so the compiler is able to find them.

InsertNickHere
  • 3,616
  • 3
  • 26
  • 23