Skip to content

Commit

Permalink
Remove generated VftblPtr type (#1397)
Browse files Browse the repository at this point in the history
* Remove generated VftblPtr type

* Completely remove VftblPtr

* Remove two Marshal.SizeOf calls
  • Loading branch information
Sergio0694 authored Jan 25, 2024
1 parent 69b8ff6 commit 81f365e
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 71 deletions.
12 changes: 4 additions & 8 deletions src/WinRT.Runtime/ObjectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,17 @@ public unsafe ObjectReference<T> As<

public unsafe TInterface AsInterface<TInterface>()
{
if (typeof(TInterface).IsDefined(typeof(System.Runtime.InteropServices.ComImportAttribute)))
if (typeof(TInterface).IsDefined(typeof(ComImportAttribute)))
{
Guid iid = typeof(TInterface).GUID;
IntPtr comPtr = IntPtr.Zero;
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(ThisPtr, ref iid, out comPtr));
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(ThisPtr, ref iid, out IntPtr comPtr));
try
{
return (TInterface)Marshal.GetObjectForIUnknown(comPtr);
}
finally
{
var vftblPtr = Unsafe.AsRef<WinRT.VftblPtr>(comPtr.ToPointer());
var vftblIUnknown = Marshal.PtrToStructure<WinRT.Interop.IUnknownVftbl>(vftblPtr.Vftbl);
vftblIUnknown.Release(comPtr);
_ = Marshal.Release(comPtr);
}
}

Expand Down Expand Up @@ -562,7 +559,6 @@ public static ObjectReference<T> FromAbi(IntPtr thisPtr, Guid iid)

private static unsafe T GetVtable(IntPtr thisPtr)
{
var vftblPtr = Unsafe.AsRef<VftblPtr>(thisPtr.ToPointer());
T vftblT;
// With our vtable types, the generic vtables will have System.Delegate fields
// and the non-generic types will have only void* fields.
Expand All @@ -581,7 +577,7 @@ private static unsafe T GetVtable(IntPtr thisPtr)
}
else
{
vftblT = Unsafe.AsRef<T>(vftblPtr.Vftbl.ToPointer());
vftblT = Unsafe.Read<T>(*(void***)thisPtr);
}
return vftblT;
}
Expand Down
6 changes: 3 additions & 3 deletions src/WinRT.Runtime/Projections/IDictionary.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
Lookup_0 = Marshal.GetDelegateForFunctionPointer(vftbl[6], Lookup_0_Type);
get_Size_1 = Marshal.GetDelegateForFunctionPointer<_get_PropertyAsUInt32>(vftbl[7]);
HasKey_2 = Marshal.GetDelegateForFunctionPointer(vftbl[8], HasKey_2_Type);
Expand Down
12 changes: 6 additions & 6 deletions src/WinRT.Runtime/Projections/IEnumerable.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
First_0 = Marshal.GetDelegateForFunctionPointer<IEnumerable_Delegates.First_0>(vftbl[6]);
}

Expand Down Expand Up @@ -458,9 +458,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
get_Current_0 = Marshal.GetDelegateForFunctionPointer(vftbl[6], get_Current_0_Type);
get_HasCurrent_1 = Marshal.GetDelegateForFunctionPointer<_get_PropertyAsBoolean>(vftbl[7]);
MoveNext_2 = Marshal.GetDelegateForFunctionPointer<IEnumerator_Delegates.MoveNext_2>(vftbl[8]);
Expand Down
6 changes: 3 additions & 3 deletions src/WinRT.Runtime/Projections/IList.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
GetAt_0 = Marshal.GetDelegateForFunctionPointer(vftbl[6], GetAt_0_Type);
get_Size_1 = Marshal.GetDelegateForFunctionPointer<_get_PropertyAsUInt32>(vftbl[7]);
GetView_2 = Marshal.GetDelegateForFunctionPointer<IList_Delegates.GetView_2>(vftbl[8]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
Lookup_0 = Marshal.GetDelegateForFunctionPointer(vftbl[6], Lookup_0_Type);
get_Size_1 = Marshal.GetDelegateForFunctionPointer<_get_PropertyAsUInt32>(vftbl[7]);
HasKey_2 = Marshal.GetDelegateForFunctionPointer(vftbl[8], HasKey_2_Type);
Expand Down
6 changes: 3 additions & 3 deletions src/WinRT.Runtime/Projections/IReadOnlyList.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
GetAt_0 = Marshal.GetDelegateForFunctionPointer(vftbl[6], GetAt_0_Type);
get_Size_1 = Marshal.GetDelegateForFunctionPointer<_get_PropertyAsUInt32>(vftbl[7]);
IndexOf_2 = Marshal.GetDelegateForFunctionPointer(vftbl[8], IndexOf_2_Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
get_Value_0 = Marshal.GetDelegateForFunctionPointer<IReferenceArray_Delegates.get_Value_0>(vftbl[6]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/WinRT.Runtime/Projections/Nullable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ public struct Vftbl

internal unsafe Vftbl(IntPtr thisPtr)
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
get_Value_0 = GetValueDelegateForFunctionPointer(vftbl[6]);
}

Expand Down
10 changes: 5 additions & 5 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6915,9 +6915,9 @@ internal IInspectable.Vftbl IInspectableVftbl;
w.write(R"(%
internal unsafe Vftbl(IntPtr thisPtr) : this()
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
var vftblPtr = *(void***)thisPtr;
var vftbl = (IntPtr*)vftblPtr;
IInspectableVftbl = *(IInspectable.Vftbl*)vftblPtr;
%}
)",
bind_each([&](writer& w, MethodDef const& method)
Expand Down Expand Up @@ -6955,7 +6955,7 @@ AbiToProjectionVftable = new Vftbl
IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,
%
};
var nativeVftbl = (IntPtr*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), Marshal.SizeOf<global::WinRT.IInspectable.Vftbl>() + sizeof(IntPtr) * %);
var nativeVftbl = (IntPtr*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(global::WinRT.IInspectable.Vftbl) + sizeof(IntPtr) * %);
%
AbiToProjectionVftablePtr = (IntPtr)nativeVftbl;
}
Expand Down Expand Up @@ -6983,7 +6983,7 @@ public static readonly IntPtr AbiToProjectionVftablePtr;
%
static unsafe Vftbl()
{
AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), Marshal.SizeOf<global::WinRT.IInspectable.Vftbl>() + sizeof(IntPtr) * %);
AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(global::WinRT.IInspectable.Vftbl) + sizeof(IntPtr) * %);
(*(Vftbl*)AbiToProjectionVftablePtr) = new Vftbl
{
IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,
Expand Down
63 changes: 29 additions & 34 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ internal static unsafe int CoCreateInstance(ref Guid clsid, IntPtr outer, uint c
internal static extern int CoDecrementMTAUsage(IntPtr cookie);

[DllImport("api-ms-win-core-com-l1-1-0.dll")]
internal static extern unsafe int CoIncrementMTAUsage(IntPtr* cookie);

internal static extern unsafe int CoIncrementMTAUsage(IntPtr* cookie);

#if NET6_0_OR_GREATER
internal static bool FreeLibrary(IntPtr moduleHandle)
{
Expand All @@ -69,8 +69,8 @@ internal static bool FreeLibrary(IntPtr moduleHandle)
// Local P/Invoke
[DllImportAttribute("kernel32.dll", EntryPoint = "FreeLibrary", ExactSpelling = true)]
static extern unsafe int PInvoke(IntPtr nativeModuleHandle);
}

}

internal static unsafe void* TryGetProcAddress(IntPtr moduleHandle, sbyte* functionName)
{
int lastError;
Expand All @@ -79,15 +79,15 @@ internal static bool FreeLibrary(IntPtr moduleHandle)
Marshal.SetLastSystemError(0);
returnValue = PInvoke(moduleHandle, functionName);
lastError = Marshal.GetLastSystemError();
}

}

Marshal.SetLastPInvokeError(lastError);
return returnValue;

// Local P/Invoke
[DllImportAttribute("kernel32.dll", EntryPoint = "GetProcAddress", ExactSpelling = true)]
static extern unsafe void* PInvoke(IntPtr nativeModuleHandle, sbyte* nativeFunctionName);
}
}
#else
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
Expand Down Expand Up @@ -171,9 +171,9 @@ internal static bool FreeLibrary(IntPtr moduleHandle)
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error(), new IntPtr(-1));
}
return functionPtr;
}

#if NET6_0_OR_GREATER
}

#if NET6_0_OR_GREATER
internal static unsafe IntPtr LoadLibraryExW(ushort* fileName, IntPtr fileHandle, uint flags)
{
int lastError;
Expand All @@ -182,15 +182,15 @@ internal static unsafe IntPtr LoadLibraryExW(ushort* fileName, IntPtr fileHandle
Marshal.SetLastSystemError(0);
returnValue = PInvoke(fileName, fileHandle, flags);
lastError = Marshal.GetLastSystemError();
}

}

Marshal.SetLastPInvokeError(lastError);
return returnValue;

// Local P/Invoke
[DllImportAttribute("kernel32.dll", EntryPoint = "LoadLibraryExW", ExactSpelling = true)]
static extern unsafe IntPtr PInvoke(ushort* nativeFileName, IntPtr nativeFileHandle, uint nativeFlags);
}
}
#else
[DllImport("kernel32.dll", SetLastError = true)]
internal static unsafe extern IntPtr LoadLibraryExW(ushort* fileName, IntPtr fileHandle, uint flags);
Expand All @@ -200,7 +200,7 @@ internal static unsafe IntPtr LoadLibraryExW(string fileName, IntPtr fileHandle,
fixed (char* lpFileName = fileName)
return LoadLibraryExW((ushort*)lpFileName, fileHandle, flags);
}


[DllImport("api-ms-win-core-winrt-l1-1-0.dll")]
internal static extern unsafe int RoGetActivationFactory(IntPtr runtimeClassId, Guid* iid, IntPtr* factory);

Expand Down Expand Up @@ -258,26 +258,21 @@ internal static unsafe int RoGetAgileReference(uint options, ref Guid iid, IntPt
}
}

internal struct VftblPtr
internal static class IActivationFactoryMethods
{
public IntPtr Vftbl;
}

internal static class IActivationFactoryMethods
{
public static unsafe ObjectReference<I> ActivateInstance<I>(IObjectReference obj)
{
IntPtr instancePtr;
global::WinRT.ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)obj.ThisPtr)[6](obj.ThisPtr, &instancePtr));
try
{
return ComWrappersSupport.GetObjectReferenceForInterface<I>(instancePtr);
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
public static unsafe ObjectReference<I> ActivateInstance<I>(IObjectReference obj)
{
IntPtr instancePtr;
global::WinRT.ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)obj.ThisPtr)[6](obj.ThisPtr, &instancePtr));
try
{
return ComWrappersSupport.GetObjectReferenceForInterface<I>(instancePtr);
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
}

internal class ComponentActivationFactory : global::WinRT.Interop.IActivationFactory
Expand Down Expand Up @@ -690,7 +685,7 @@ protected override Delegate GetEventInvoke()
#pragma warning restore CA2002

internal static class InterfaceIIDs
{
{
#if NET
internal static readonly Guid IInspectable_IID = new Guid(new global::System.ReadOnlySpan<byte>(new byte[] { 0xE0, 0xE2, 0x86, 0xAF, 0x2D, 0xB1, 0x6A, 0x4C, 0x9C, 0x5A, 0xD7, 0xAA, 0x65, 0x10, 0x1E, 0x90 }));
internal static readonly Guid IUnknown_IID = new Guid(new global::System.ReadOnlySpan<byte>(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }));
Expand Down

0 comments on commit 81f365e

Please sign in to comment.