Get signature from C++ function name?

Started by
13 comments, last by Antheus 14 years, 2 months ago
I exported a function from my DLL called "add" which just adds two ints together and returns the sum. I exported it as a C++ function in order to get the signature from the name, I know it can be done but I don't know how to do it. "?add@@YAHHH@Z" is the name of the function after it is exported from the DLL, now how do I get data which represents the signature from that?
Advertisement
A quick Google of "unmangle C++ name" turns up this and this. They should get you on the right track :)
Note that while this will work as long as your functions export only simple types (int, float, etc), it won't be much good if they export more "complex" types. For example, if the method takes a class or struct, you'll be able to find out the name of the struct, but that won't tell you anything about the members of that struct.

That may or may not be important, of course...
You're pretty much out of luck here. The name mangling algorithm is undocumented, and while there's been some work into reverse engineering it, it's not complete.

If symbol files for the DLL are present, you can use the DBGHELP api and/or the DIA SDK. Be warned however, this is no easy task. There are lots of little corner cases to handle if you want to be able to parse arbitrarily complex function signatures.
Hmm, I admit I didn't know about this function. My response came from the fact that I once implemented my own undecoration based on DIA SDK because I actually needed semantic info about the types of various arguments, as opposed to just a flat textual representation of the entire signature. I guess if the OP doesn't need that though, perhaps this function suffices.
Arent you guys overcomplicating this? All i do when i want to unmangle exported function name from a dll project is to add a simple .def file to it, wich look like this:

EXPORTS
SomeFunction @1
SomeOtherFunction @2
... ect

in your case, it should look that way:

.def file
EXPORTS    Add       @1


.h file
#define EXP_FUNC __stdcallvoid EXP_FUNC Add(int x1, int x2);


.cpp
void EXP_FUNC Add(int x1, int x2){}
Original post by Vortez
Arent you guys overcomplicating this? All i do when i want to unmangle exported function name from a dll project is to add a simple .def file to it, wich look like this:/quote]

His original problem statement implies that he wants to take an EXISTING dll file, for which he does not necessarily have source code and cannot recompile, and DETERMINE at runtime the signature of every function exported by that dll. He doesn't want to actually produce DLL files whose exported function names are unmangled, because as you mention, that is easy.
Ah, i though it was is own dll.
Quote:Original post by Vortez
Ah, i though it was is own dll.


Well actually when I read it again, you're right it does say it's his own DLL. But still, the problem with C++ is that you can export classes and member functions. And for this to work, it has to mangle the name, If he's only exporting global functions and no class types, then maybe your solution is appropriate
Quote:His original problem statement implies that he wants to take an EXISTING dll file, for which he does not necessarily have source code and cannot recompile, and DETERMINE at runtime the signature of every function exported by that dll. He doesn't want to actually produce DLL files whose exported function names are unmangled, because as you mention, that is easy.


Not necessarily, it is my own DLL but the reason I want to demangle the names is not only to get the name but also the structure of the function, if there is another way to get the structure of a function from the DLL please tell me because I would love to be able to support functions that are exported as C functions.

This topic is closed to new replies.

Advertisement