我正在使用 ICorProfilerInfo
接口(interface)编写 CLR 探查器。
当使用 SetEnterLeaveFunctionHooks
时,回调方法会传递一个 FunctionID
。
在给定此 FunctionID
的情况下,如何获取函数的元数据(特别是在名称之后)?
一篇 MSDN 文章建议第一个调用应该是 GetFunctionInfo
。 documentation对于此功能状态:
The profiler code can call ICorProfilerInfo::GetModuleMetaData to obtain a metadata interface for a given module. The metadata token that is returned to the location referenced by pToken can then be used to access the metadata for the function.
它没有详细说明最后一句话(“元数据 token ......可用于访问函数的元数据”)。 这是如何工作的?
到目前为止,我正在做以下事情:
void MyProfiler::EnterMethod(FunctionID functionID)
{
ClassID classId = 0;
ModuleID moduleId = 0;
mdToken metaDataToken = 0;
IMetaDataImport* metaDataImport = NULL;
// (m_info is ICorProfilerInfo3)
m_info->GetFunctionInfo(functionID, &classId, &moduleId, &metaDataToken);
m_info->GetModuleMetaData(moduleId, ofRead, IID_IMetaDataImport, (IUnknown**)&metaDataImport);
// What goes here?
}
我试过像这样调用 GetTypeRefProps
:
mdToken ptkResolutionScope;
WCHAR szName[1024];
ULONG cchName = 1024;
ULONG pchName;
HRESULT result = MetaDataImport->GetTypeRefProps(pToken, &ptkResolutionScope, szName, cchName, &pchName);
最终调用返回 S_FALSE
,并且不填充 szName
。
最佳答案
GetTypeRefProps
仅在您的 token 是 TypeRef token 时适用,GetFunctionInfo
将为您提供一个 MethodDef token ,它要求您使用 GetMethodProps
方法。
metaDataImport->GetMethodProps(metaDataToken, NULL, szName, cchName, &pchName, NULL, NULL, NULL, NULL, NULL);
关于c++ - 使用 GetFunctionInfo 从 FunctionID 获取函数的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340776/