5

I have my own debugger for .NET apps that uses IMetaDataImport interface

When I call ResolveTypeRef method, I always get NotImplementedException.

The definition of ResolveTypeRef is like this:

[ComImport]
[Guid("....")] //a valid GUID
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[CLSCompliant(false)]
public interface IMetaDataImport {
  void ResolveTypeRef(
    [ComAliasName("mdTypeRef")] mdToken tr,
    [ComAliasName("REFIID")] ref Guid riid,
    [ComAliasName("IUnknown**"), Out, MarshalAs(UnmanagedType.IUnknown)] out object ppIScope,
    [ComAliasName("mdTypeDef*"), Out] out mdToken ptd
  );
  // ... other methods from IMetaDataImport
}

The method calling:

metadataImport.ResolveTypeRef(typeRefToken, ref metadataImportGuid, out metadataObject, out typeDefToken)

Typically, the method should resolve type System.Exception or System.SystemException.

The problem occured when I moved app from .NET 3.5 to .NET 4.

Thanks for the answers!

svick
  • 236,525
  • 50
  • 385
  • 514
Paulie
  • 81
  • 7
  • I wonder if the "ref" keyword should be befor the "riid" parameter. But it doesn't work with or without it. – Paulie Jan 14 '12 at 20:50

2 Answers2

3

Uff, I finally get rid of this ... The solution is not call ResolveTypeRef, but create your own method to resolve type:

  mdToken ptkResScope;
  uint len;
  metadataImport.GetTypeRefProps(typeRef, out ptkResScope, null, 0, out len);
  StringBuilder sb = new StringBuilder((int)len);
  metadataImport.GetTypeRefProps(typeRef, out ptkResScope, sb, len, out len);
  string className = sb.ToString();
  foreach (loadedModule ) {   // this is quite tricky part ...     
    metadataImportForLoadedModule = GetMetaDataImportForModule(loadedModule);
    metadataImportForLoadedModule.FindTypeDefByName(className, mdToken.Nil, out typeDef);
    if ( typeDef.IsNonNil(CorTokenType.mdtTypeDef) ) {
      return typeDef;
      }
    }

The idea comes from David Broman's CLR Profiling API Blog: Metadata Tokens, Run-Time IDs, and Type Loading (really good reading if you are interested in MetaDataImport).

valiano
  • 16,433
  • 7
  • 64
  • 79
Paulie
  • 81
  • 7
  • funny, I want to mark this as an answer, but I will be able in 22 hours :) – Paulie Jan 15 '12 at 22:06
  • Damn, and at [MSDN](https://msdn.microsoft.com/en-us/library/ms230990(v=vs.110).aspx) there is nothing about this **not implemented exception**! Thanks! – 3615 Jul 29 '16 at 12:21
0

If you actually try and step into the method ResolveTypeRef, it seems you actually end up at SymMethod::GetSourceStartEnd which is what is returning E_NOTIMPL. Furthermore, if you have a look at the vtable in IDA, as far as I can see the method ResolveTypeRef doesn't even exist on IMetaDataImport/2's vtable; rather, it is a method on CordbModule, however evidently isn't actually exposed in the ICorDebugModule interface.

In any case, the conclusion here appears to be that Microsoft's documentation/header files are out of date with the actual implementation

lordmilko
  • 161
  • 1
  • 8