Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c++ - Demangling in MSVC

How can i demangle name in MSVC? There's abi::__cxa_demangle function in gcc. In MSDN i've found UnDecorateSymbolName:

http://msdn.microsoft.com/ru-ru/library/windows/desktop/ms681400%28v=vs.85%29.aspx

Unfortunately, this function can't undecorate even such symbol:

#include <Windows.h>
#include <DbgHelp.h>

#include <cstdlib>
#include <iostream>
#include <typeinfo>

int main()
{
    SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);

    if (!SymInitialize(GetCurrentProcess(), NULL, TRUE))
    {
        std::cout << "SymInitialize returned error: " << GetLastError() << '
';
        return EXIT_FAILURE;
    }

    class Foo {};
    Foo instance;

    const char* decorated_name = typeid(instance).name();
    char undecorated_name[1024];
    if (!UnDecorateSymbolName(decorated_name, undecorated_name, sizeof(undecorated_name) / sizeof(*undecorated_name), UNDNAME_COMPLETE))
    {
        std::cout << "UnDecorateSymbolName returned error: " << GetLastError() << '
';
        return EXIT_FAILURE;
    }

    std::cout << "Decorated name: " << decorated_name << '
'
              << "Undecorated name: " << undecorated_name << '
';
}

Output

Decorated name: ?AVFoo@?4?main@

Undecorated name: ?AVFoo@?4?main@

If i am doing it wrong?

I've heard somewhere about _unDName function, but i can't find any example with it. In which header file it is defined?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Visual studio already shipped a utility called undname. For my VS2010 and VS2013 install, it's installed to %VSINSTALL%vcin directory. And for x64 platform, in %VSINSTALL%vcamd64in directory.

The example usage is:

D:workVS2013>undname "?static_x@?1??getX@@YAAAUX@@XZ@4U2@A"
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.

Undecoration of :- "?static_x@?1??getX@@YAAAUX@@XZ@4U2@A"
is :- "struct X `struct X & __cdecl getX(void)'::`2'::static_x"

Another way to get the demangled name is use /FAsc /Faoutput.asm compiler option, which will produce the assembly list, in which each mangled name is commented with it's demangled name. See This link for reference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...