Demangling in MSVC

后端 未结 4 1385
轻奢々
轻奢々 2020-12-16 23:47

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/windo

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 00:48

    Update for Visual Studio 2019.

    (1) typeid(instance).name() - used in the request - already returns the undecorated name: further demangling is not required in this case

    (2) the command UNDNAME.EXE, provided in the bin folders, does not work correctly, even if we take off the initial dot. For example ".?AVFoo@?1??main@@YAHXZ@" is unmangled as " ?? int __cdecl main(void)'::2'::AVFoo", giving an invalid name AVFoo. The correct name must be Foo

    (3) the dbghelp API UnDecorateSymbolName() does not work either

    (4) the correct code can be deduced from the assembly generated by the compiler for the directive typeid()

    Here is a little program which will undecorate correctly all the C++ mangled symbols:

    // compile as: cl -W3 und.c
    
    #include 
    #include "dbghelp.h"
    #include 
    
    #pragma comment(lib, "dbghelp.lib")
    
    extern char *__unDName(char*, const char*, int, void*, void*, int);
    
    int
    main(int argc, char **argv)
    {
        const char *decorated_name = 0;
        char undecorated_name[1024];
    
        if (argc == 2) decorated_name = argv[1];
        else {
            printf("usage: %s \n", argv[0]);
            return 1;
            }
    
        __unDName(undecorated_name, decorated_name+1, 1024, malloc, free, 0x2800);
    
        printf("Decorated name: %s\n", decorated_name);
        printf("Undecorated name: %s\n", undecorated_name);
        return 0;
    }
    

    For example:

    und ".?AVFoo@?1??main@@YAHXZ@"

    Decorated name: .?AVFoo@?1??main@@YAHXZ@

    Undecorated name: class int __cdecl main(void)'::2'::Foo

提交回复
热议问题