public unsafe static ITypeInfo GetTypeInfoByName(this ITypeLib typeLib, string typeName) { // The find method is case insensitive, and will overwrite the input buffer // with the actual found casing. char * nameBuffer = stackalloc char[typeName.Length + 1]; Span <char> nameSpan = new Span <char>(nameBuffer, typeName.Length); typeName.AsSpan().CopyTo(nameSpan); nameBuffer[typeName.Length] = '\0'; IntPtr * typeInfos = stackalloc IntPtr[1]; MemberId *memberIds = stackalloc MemberId[1]; ushort found = 1; typeLib.FindName( nameBuffer, lHashVal: 0, typeInfos, memberIds, &found).ThrowIfFailed(typeName); return((ITypeInfo)Marshal.GetTypedObjectForIUnknown(typeInfos[0], typeof(ITypeInfo))); }
public unsafe void FindName() { HResult result = LoadStdOle2(out ITypeLib typeLib); result.Should().Be(HResult.S_OK); typeLib.Should().NotBeNull(); string name = "picture"; char * nameBuffer = stackalloc char[name.Length + 1]; Span <char> nameSpan = new Span <char>(nameBuffer, name.Length); name.AsSpan().CopyTo(nameSpan); nameBuffer[name.Length] = '\0'; IntPtr * typeInfos = stackalloc IntPtr[1]; MemberId *memberIds = stackalloc MemberId[1]; ushort found = 1; result = typeLib.FindName( nameBuffer, lHashVal: 0, typeInfos, memberIds, &found); result.Should().Be(HResult.S_OK); found.Should().Be(1); memberIds[0].Should().Be(new MemberId { Value = -1 }); typeInfos[0].Should().NotBe(IntPtr.Zero); Marshal.Release(typeInfos[0]); // Find gives back the right casing nameSpan.ToString().Should().Be("Picture"); }