gpt4 book ai didi

c++ - MSVC 中的分解

转载 作者:可可西里 更新时间:2023-11-01 18:08:16 25 4
gpt4 key购买 nike

如何在 MSVC 中分解名称? gcc 中有 abi::__cxa_demangle 函数。在 MSDN 中我找到了 UnDecorateSymbolName:

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

不幸的是,这个函数甚至不能取消修饰这样的符号:

#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() << '\n';
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() << '\n';
return EXIT_FAILURE;
}

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

输出

Decorated name: ?AVFoo@?4?main@

Undecorated name: ?AVFoo@?4?main@

如果我做错了?

我在某处听说过 _unDName 函数,但找不到任何示例。在哪个头文件中定义?

最佳答案

Visual Studio 已经发布了一个名为 undname 的实用程序。对于我的 VS2010 和 VS2013 安装,它被安装到%VSINSTALL%\vc\bin 目录。对于 x64 平台,在%VSINSTALL%\vc\amd64\bin 目录。

示例用法是:

D:\work\VS2013>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"

另一种获取分解名称的方法是使用/FAsc/Faoutput.asm 编译器选项,这将生成汇编列表,其中每个分解名称都用它的分解名称进行注释。参见 This link for reference .

关于c++ - MSVC 中的分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com