-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Convert Marshal test Variant usage to ComVariant #126968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Drawing; | ||
| using System.Reflection; | ||
| using System.Reflection.Emit; | ||
| using System.Runtime.InteropServices.Marshalling; | ||
| using System.Runtime.InteropServices.Tests.Common; | ||
| using Xunit; | ||
|
|
||
|
|
@@ -109,22 +110,21 @@ public static IEnumerable<object[]> GetNativeVariantForObject_NonRoundtrippingPr | |
| [MemberData(nameof(GetNativeVariantForObject_NonRoundtrippingPrimitives_TestData))] | ||
| public void GetNativeVariantForObject_ValidObject_Success(object primitive, VarEnum expectedVarType, IntPtr expectedValue, object expectedRoundtripValue) | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| Marshal.GetNativeVariantForObject(primitive, pNative); | ||
|
Comment on lines
+113
to
116
|
||
|
|
||
| Variant result = Marshal.PtrToStructure<Variant>(pNative); | ||
| Assert.Equal(expectedVarType, (VarEnum)result.vt); | ||
| ComVariant result = Marshal.PtrToStructure<ComVariant>(pNative); | ||
| Assert.Equal(expectedVarType, result.VarType); | ||
| if (expectedValue != (IntPtr)(-1)) | ||
| { | ||
| Assert.Equal(expectedValue, result.bstrVal); | ||
| Assert.Equal(expectedValue, result.GetRawDataRef<IntPtr>()); | ||
| } | ||
| else | ||
| { | ||
| Assert.NotEqual((IntPtr)(-1), result.bstrVal); | ||
| Assert.NotEqual(IntPtr.Zero, result.bstrVal); | ||
| Assert.NotEqual((IntPtr)(-1), result.GetRawDataRef<IntPtr>()); | ||
| Assert.NotEqual(IntPtr.Zero, result.GetRawDataRef<IntPtr>()); | ||
| } | ||
|
|
||
| // Make sure it roundtrips. | ||
|
|
@@ -141,24 +141,23 @@ public void GetNativeVariantForObject_ValidObject_Success(object primitive, VarE | |
| [InlineData("99")] | ||
| public void GetNativeVariantForObject_String_Success(string obj) | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| Marshal.GetNativeVariantForObject(obj, pNative); | ||
|
|
||
| Variant result = Marshal.PtrToStructure<Variant>(pNative); | ||
| ComVariant result = Marshal.PtrToStructure<ComVariant>(pNative); | ||
| try | ||
| { | ||
| Assert.Equal(VarEnum.VT_BSTR, (VarEnum)result.vt); | ||
| Assert.Equal(obj, Marshal.PtrToStringBSTR(result.bstrVal)); | ||
| Assert.Equal(VarEnum.VT_BSTR, result.VarType); | ||
| Assert.Equal(obj, Marshal.PtrToStringBSTR(result.GetRawDataRef<IntPtr>())); | ||
|
|
||
| object o = Marshal.GetObjectForNativeVariant(pNative); | ||
| Assert.Equal(obj, o); | ||
| } | ||
| finally | ||
| { | ||
| Marshal.FreeBSTR(result.bstrVal); | ||
| Marshal.FreeBSTR(result.GetRawDataRef<IntPtr>()); | ||
| } | ||
| } | ||
| finally | ||
|
|
@@ -171,8 +170,7 @@ public void GetNativeVariantForObject_String_Success(string obj) | |
| public unsafe void GetNativeVariantForObject_Guid_Success() | ||
| { | ||
| var guid = new Guid("0DD3E51B-3162-4D13-B906-030F402C5BA2"); | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| if (PlatformDetection.IsWindowsNanoServer) | ||
|
|
@@ -183,12 +181,12 @@ public unsafe void GetNativeVariantForObject_Guid_Success() | |
| { | ||
| Marshal.GetNativeVariantForObject(guid, pNative); | ||
|
|
||
| Variant result = Marshal.PtrToStructure<Variant>(pNative); | ||
| Assert.Equal(VarEnum.VT_RECORD, (VarEnum)result.vt); | ||
| Assert.NotEqual(nint.Zero, result.pRecInfo); // We should have an IRecordInfo instance. | ||
| ComVariant result = Marshal.PtrToStructure<ComVariant>(pNative); | ||
| Assert.Equal(VarEnum.VT_RECORD, result.VarType); | ||
| Assert.NotEqual(nint.Zero, result.GetRawDataRef<Record>()._recordInfo); // We should have an IRecordInfo instance. | ||
|
Comment on lines
182
to
+186
|
||
|
|
||
| var expectedBytes = new ReadOnlySpan<byte>(guid.ToByteArray()); | ||
| var actualBytes = new ReadOnlySpan<byte>((void*)result.bstrVal, expectedBytes.Length); | ||
| var actualBytes = new ReadOnlySpan<byte>((void*)result.GetRawDataRef<Record>()._record, expectedBytes.Length); | ||
| Assert.Equal(expectedBytes, actualBytes); | ||
|
|
||
| object o = Marshal.GetObjectForNativeVariant(pNative); | ||
|
|
@@ -205,15 +203,14 @@ public unsafe void GetNativeVariantForObject_Guid_Success() | |
| [InlineData(3.14)] | ||
| public unsafe void GetNativeVariantForObject_Double_Success(double obj) | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| Marshal.GetNativeVariantForObject(obj, pNative); | ||
|
|
||
| Variant result = Marshal.PtrToStructure<Variant>(pNative); | ||
| Assert.Equal(VarEnum.VT_R8, (VarEnum)result.vt); | ||
| Assert.Equal(*((ulong*)&obj), *((ulong*)&result.bstrVal)); | ||
| ComVariant result = Marshal.PtrToStructure<ComVariant>(pNative); | ||
| Assert.Equal(VarEnum.VT_R8, result.VarType); | ||
| Assert.Equal(*((ulong*)&obj), result.GetRawDataRef<ulong>()); | ||
|
|
||
| object o = Marshal.GetObjectForNativeVariant(pNative); | ||
| Assert.Equal(obj, o); | ||
|
|
@@ -228,15 +225,14 @@ public unsafe void GetNativeVariantForObject_Double_Success(double obj) | |
| [InlineData(3.14f)] | ||
| public unsafe void GetNativeVariantForObject_Float_Success(float obj) | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| Marshal.GetNativeVariantForObject(obj, pNative); | ||
|
|
||
| Variant result = Marshal.PtrToStructure<Variant>(pNative); | ||
| Assert.Equal(VarEnum.VT_R4, (VarEnum)result.vt); | ||
| Assert.Equal(*((uint*)&obj), *((uint*)&result.bstrVal)); | ||
| ComVariant result = Marshal.PtrToStructure<ComVariant>(pNative); | ||
| Assert.Equal(VarEnum.VT_R4, result.VarType); | ||
| Assert.Equal(*((uint*)&obj), result.GetRawDataRef<uint>()); | ||
|
|
||
| object o = Marshal.GetObjectForNativeVariant(pNative); | ||
| Assert.Equal(obj, o); | ||
|
|
@@ -279,8 +275,7 @@ public void GetNativeVariantForObject_GenericObject_ThrowsArgumentException(obje | |
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))] | ||
| public void GetNativeVariant_InvalidArray_ThrowsSafeArrayTypeMismatchException() | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| Assert.Throws<SafeArrayTypeMismatchException>(() => Marshal.GetNativeVariantForObject(new int[][] { }, pNative)); | ||
|
|
@@ -302,8 +297,7 @@ public static IEnumerable<object[]> GetNativeVariant_VariantWrapper_TestData() | |
| [MemberData(nameof(GetNativeVariant_VariantWrapper_TestData))] | ||
| public void GetNativeVariant_VariantWrapper_ThrowsArgumentException(object obj) | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| AssertExtensions.Throws<ArgumentException>(null, () => Marshal.GetNativeVariantForObject(obj, pNative)); | ||
|
|
@@ -328,8 +322,7 @@ public static IEnumerable<object[]> GetNativeVariant_HandleObject_TestData() | |
| [MemberData(nameof(GetNativeVariant_HandleObject_TestData))] | ||
| public void GetNativeVariant_HandleObject_ThrowsArgumentException(object obj) | ||
| { | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| AssertExtensions.Throws<ArgumentException>(null, () => Marshal.GetNativeVariantForObject(obj, pNative)); | ||
|
|
@@ -346,8 +339,7 @@ public static void GetNativeVariantForObject_CantCastToObject_ThrowsInvalidCastE | |
| { | ||
| // While GetNativeVariantForObject supports taking chars, GetObjectForNativeVariant will | ||
| // never return a char. The internal type is ushort, as mentioned above. | ||
| var v = new Variant(); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf(v)); | ||
| IntPtr pNative = Marshal.AllocHGlobal(Marshal.SizeOf<ComVariant>()); | ||
| try | ||
| { | ||
| Marshal.GetNativeVariantForObject<char>('a', pNative); | ||
|
|
@@ -372,6 +364,13 @@ public enum UInt16Enum : ushort { Value1, Value2 } | |
| public enum UInt32Enum : uint { Value1, Value2 } | ||
| public enum UInt64Enum : ulong { Value1, Value2 } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| private struct Record | ||
| { | ||
| public nint _record; | ||
| public nint _recordInfo; | ||
| } | ||
|
|
||
| public class FakeSafeHandle : SafeHandle | ||
| { | ||
| public FakeSafeHandle() : base(IntPtr.Zero, false) { } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong. I don't think there is any requirement that
AllocHGlobalreturns zero'd memory. I don't think we really need an allocation at all. Why not just pass a pointer to the variant on the stack?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. @copilot please use a ComVariant local here instead of allocating with
AllocHGlobal/FreeHGlobal