public static ISymbolReader GetSymbolReaderForFile(
System.Diagnostics.SymbolStore.SymBinder binder, string pathModule, string searchPath)
{
// Guids for imported metadata interfaces.
Guid dispenserClassID = new Guid(0xe5cb7a31, 0x7512, 0x11d2, 0x89,
0xce, 0x00, 0x80, 0xc7, 0x92, 0xe5, 0xd8); // CLSID_CorMetaDataDispenser
Guid dispenserIID = new Guid(0x809c652e, 0x7396, 0x11d2, 0x97, 0x71,
0x00, 0xa0, 0xc9, 0xb4, 0xd5, 0x0c); // IID_IMetaDataDispenser
Guid importerIID = new Guid(0x7dac8207, 0xd3ae, 0x4c75, 0x9b, 0x67,
0x92, 0x80, 0x1a, 0x49, 0x7d, 0x44); // IID_IMetaDataImport
// First create the Metadata dispenser.
object objDispenser;
NativeMethods.CoCreateInstance(ref dispenserClassID, null, 1,
ref dispenserIID, out objDispenser);
// Now open an Importer on the given filename. We'll end up passing this importer
// straight through to the Binder.
object objImporter = null;
IMetaDataDispenser dispenser = (IMetaDataDispenser)objDispenser;
try
{
dispenser.OpenScope(pathModule, 0, ref importerIID, out objImporter);
}
catch
{
Console.WriteLine(" ERROR: Failed dispenser.OpenScope() - perhaps this app needs to be compiled in x86?");
return(null);
}
IntPtr importerPtr = IntPtr.Zero;
ISymbolReader reader;
try
{
// This will manually AddRef the underlying object, so we need to
// be very careful to Release it.
importerPtr = Marshal.GetComInterfaceForObject(objImporter,
typeof(IMetadataImport));
reader = binder.GetReader(importerPtr, pathModule, searchPath);
}
finally
{
if (importerPtr != IntPtr.Zero)
{
Marshal.Release(importerPtr);
}
}
return(reader);
}