I am writing a CLR profiler using the ICorProfilerInfo
interface.
When using SetEnterLeaveFunctionHooks
, the callback methods are passed a FunctionID
.
How do I get the metadata (I am after the name in particular), of a function given this FunctionID
?
An MSDN article suggests that the first call should be to GetFunctionInfo
. The documentation for this function states:
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.
It does not elaborate on the last sentence ('the metadata token ... can be used to access metadata for the function'). How does this work?
So far, I am doing the following:
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?
}
I have tried to call GetTypeRefProps
like this:
mdToken ptkResolutionScope;
WCHAR szName[1024];
ULONG cchName = 1024;
ULONG pchName;
HRESULT result = MetaDataImport->GetTypeRefProps(pToken, &ptkResolutionScope, szName, cchName, &pchName);
The final call returns S_FALSE
, and does not populate szName
.
GetTypeRefProps
is only applicable when your token is a TypeRef token, GetFunctionInfo
will give you a MethodDef token which requires you to use the GetMethodProps
method.
metaDataImport->GetMethodProps(metaDataToken, NULL, szName, cchName, &pchName, NULL, NULL, NULL, NULL, NULL);