I'm trying to call the GetLibStatistics method of the ITypeLib2 interface. I've tried several variations and techniques, but they all throw System.AccessViolationException: Attempted to read or write protected memory.
I was able to successfully execute that method in native COM in C++, so I know the .tlb file I'm using is not at fault.
My guess at this point is that GetLibStatistics is not implemented in C#. Please advise.
public static void GetLibStatisticsTest()
{
ITypeLib tlb;
LoadTypeLibEx(@"C:\Sample.tlb", RegKind.None, out tlb);
var tlb2 = tlb as ITypeLib2;
if (tlb2 == null ) { return; }
try
{
IntPtr pcUniqueNames = IntPtr.Zero;
int pcchUniqueNames;
// This always throws `System.AccessViolationException`. Why??
tlb2.GetLibStatistics(pcUniqueNames, out pcchUniqueNames);;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
[DllImport("oleaut32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int LoadTypeLibEx(string szFile, RegKind regKind, out ITypeLib pptlib);
ITypeLib2defined by .NET is "simply" wrong. It's defined like this in C/C++:And like this in .NET
So
GetDocumentation2andGetLibStatisticshave been switched and you're callingGetDocumentation2instead with obviously wrong parameters ().This can be checked in a debugger:
So you must redefine
ITypeLib2in your code like this:And it will work.
I've created an issue for this https://github.com/dotnet/runtime/issues/99946