Search code examples
.netclr-profiling-api

.NET Profiler - Getting The Type Of A Function Parameter


I've implemented the .Net Profiler callback which allows me to get data about all the functions called in a .NET application. The function callback works great. Starting in the callback, I make a call to

GetModuleMetaData(moduleId, ofRead, IID_IMetaDataImport, (IUnknown**)&metaDataImport);

to which I make a subsequent call to

metaDataImport->EnumParams(&phEnum, (mdMethodDef)metaDataToken, rParams, cMax, &pcTokens);

pcTokens contains an array of parameter reference tokens. I can use those tokens to get the parameter names with the following call.

metaDataImport->GetParamProps(rParams[i], &(mdMethodDef)metaDataToken, &pulSequence, szName3, cchName3, &pchName3, NULL, NULL, NULL, NULL);

I'm stuck at trying to find the type of each parameter. I can't find any documentation that would give me the parameter type. https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/metadata/imetadataimport-interface

Any thoughts?


Solution

  • If you want to receive CorElementType of the parameter, you can do it by signature parsing. Each method has signature with types of all locals and parameters. By GetSigFromToken or GetMethodSpecProps (for methodSpec) you can receive the signature.

    After that you can parse the signature and extract the needed information. I recommend you to read I.8.6.1 Signatures of ECMA-355, especially I.8.6.1.5 Method signatures paragraph, to understand the format of the signature.

    Here is an example of type parser in c# or c++ parser by David Broman.