简体   繁体   English

如何使用 WIN32 Debug Info API 获取多态 C++ 对象的类名?

[英]How to get the class name of a polymorph C++ Object using WIN32 Debug Info API?

The win32 debugger shows the class name in question in the inspector window that means the information must be available, it's only in question wether the microsoft developers made the function public. win32 调试器在检查器窗口中显示有问题的类名,这意味着信息必须可用,只是微软开发人员是否公开了该函数。

For Debug/Diagnostic/development purpose i wanna write a function to get the instanciated class name of a c++ object as an output and as an input the pointer to the base class.对于调试/诊断/开发目的,我想编写一个函数来获取 c++ 对象的实例化类名作为输出,并将指向基类的指针作为输入。

For example:例如:

const char* GetNameOfInstanciatedClassByVoidPtr(void* pBaseClassOrInstanciatedClass);

class B { public: virtual void Init (){
  assert(StringIsEqual(GetNameOfInstanciatedClassByVoidPtr(this), "C")); } };
class C : B {} c; c.Init(); 

So please provide a solution for implementation of function GetNameOfInstanciatedClassByVoidPtr .所以请提供一个实现函数GetNameOfInstanciatedClassByVoidPtr的解决方案。

So question is: Whats the required lib/header/function ?所以问题是:所需的 lib/header/function 是什么? I guess a API for the win32 Debug info would help?我猜 win32 调试信息的 API 会有所帮助吗?

Writing virtual functions which return strings or type_info is not an acceptable solution in my situation.在我的情况下,编写返回字符串或 type_info 的虚拟函数不是可接受的解决方案。

As i said before: this is for diagnostic/debug/development purpose only, NOT for production code so please avoid discussions about clean code purposes.正如我之前所说:这仅用于诊断/调试/开发目的,不适用于生产代码,因此请避免讨论干净代码目的。 I could also collect the information manually but since it's about 260 classes i might be faster this way.我也可以手动收集信息,但由于它大约有 260 个类,我可能会以这种方式更快。 ;) ;)

EDIT: In the first version of the question i had the function 'GetNameOfInstanciatedClassByVoidPtr ' called in the constructor but i observed the information is not yet available in the constructor time.编辑:在问题的第一个版本中,我在构造函数中调用了函数“GetNameOfInstanciatedClassByVoidPtr”,但我观察到该信息在构造函数时间尚不可用。 So i put it to an Init method which is always called.所以我把它放在一个总是被调用的 Init 方法中。 In this method the debugger shows the name of the instanciated class so if i put the function GetNameOfInstanciatedClassByVoidPtr into my init method the information can be obtained.在此方法中,调试器显示实例化类的名称,因此如果我将函数 GetNameOfInstanciatedClassByVoidPtr 放入我的 init 方法中,则可以获得信息。

You can use typeid :您可以使用typeid

#include <iostream>
#include <typeinfo>

struct A {
    virtual void printName(){
        std::cout << typeid(*this).name() << "\n";
    }
    virtual ~A() = default;
};
struct C : A{};

int main() {
    C c;
    c.printName();
    A a;
    a.printName();
}

However, the name is implementation defined.但是,名称是实现定义的。 Possible output is :可能的输出是

1C
1A

And you cannot get the typeid of the dynamic type of the object from a void* .并且您无法从void*获取对象的动态类型的typeid See this somewhat related question: How to get the typeid of a void* pointer?看到这个有点相关的问题: How to get the typeid of a void* pointer?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM