Skip to content

Commit 8538a75

Browse files
authored
Split Variant marshalling tests (#53035)
* Rework of variant tests split * Fix testing of disabled built-in COM
1 parent ed92fa5 commit 8538a75

File tree

7 files changed

+292
-95
lines changed

7 files changed

+292
-95
lines changed

src/coreclr/vm/interopconverter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,29 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, ComIpType ReqIpType, ComIpType
188188
if (ReqIpType & ComIpType_Dispatch)
189189
{
190190
hr = SafeQueryInterface(pUnk, IID_IDispatch, &pvObj);
191-
pUnk->Release();
191+
if (SUCCEEDED(hr))
192+
{
193+
pUnk->Release();
194+
FetchedIpType = ComIpType_Dispatch;
195+
}
196+
else if (ReqIpType & ComIpType_Unknown)
197+
{
198+
hr = S_OK;
199+
pvObj = pUnk;
200+
FetchedIpType = ComIpType_Unknown;
201+
}
192202
}
193203
else
194204
{
195205
pvObj = pUnk;
206+
FetchedIpType = ComIpType_Unknown;
196207
}
197208

198209
if (FAILED(hr))
199210
COMPlusThrowHR(hr);
200211

201212
if (pFetchedIpType != NULL)
202-
*pFetchedIpType = ReqIpType;
213+
*pFetchedIpType = FetchedIpType;
203214

204215
RETURN pvObj;
205216
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using TestLibrary;
7+
using static VariantNative;
8+
9+
#pragma warning disable CS0612, CS0618
10+
partial class Test
11+
{
12+
public static int Main()
13+
{
14+
bool builtInComDisabled=false;
15+
var comConfig = AppContext.GetData("System.Runtime.InteropServices.BuiltInComInterop.IsSupported");
16+
if(comConfig != null && !bool.Parse(comConfig.ToString()))
17+
{
18+
builtInComDisabled=true;
19+
}
20+
21+
Console.WriteLine($"Built-in COM Disabled?: {builtInComDisabled}");
22+
try
23+
{
24+
TestByValue(!builtInComDisabled);
25+
TestByRef(!builtInComDisabled);
26+
TestOut();
27+
TestFieldByValue(!builtInComDisabled);
28+
TestFieldByRef(!builtInComDisabled);
29+
}
30+
catch (Exception e)
31+
{
32+
Console.WriteLine($"Test failed: {e}");
33+
return 101;
34+
}
35+
return 100;
36+
}
37+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
using TestLibrary;
9+
using static VariantNative;
10+
using ComTypes = System.Runtime.InteropServices.ComTypes;
11+
12+
#pragma warning disable CS0612, CS0618
13+
partial class Test
14+
{
15+
public static int Main()
16+
{
17+
bool testComMarshal=true;
18+
ComWrappers.RegisterForMarshalling(new ComWrappersImpl());
19+
try
20+
{
21+
TestByValue(testComMarshal);
22+
TestByRef(testComMarshal);
23+
TestOut();
24+
TestFieldByValue(testComMarshal);
25+
TestFieldByRef(testComMarshal);
26+
}
27+
catch (Exception e)
28+
{
29+
Console.WriteLine($"Test failed: {e}");
30+
return 101;
31+
}
32+
return 100;
33+
}
34+
}
35+
36+
internal unsafe class ComWrappersImpl : ComWrappers
37+
{
38+
private static readonly ComInterfaceEntry* wrapperEntry;
39+
40+
static ComWrappersImpl()
41+
{
42+
var vtblRaw = (IntPtr*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDispatchVtbl), sizeof(IntPtr) * 7);
43+
GetIUnknownImpl(out vtblRaw[0], out vtblRaw[1], out vtblRaw[2]);
44+
45+
vtblRaw[3] = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, int>)&IDispatchVtbl.GetTypeInfoCountInternal;
46+
vtblRaw[4] = (IntPtr)(delegate* unmanaged<IntPtr, int, int, IntPtr, int>)&IDispatchVtbl.GetTypeInfoInternal;
47+
vtblRaw[5] = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, IntPtr, int, int, IntPtr, int>)&IDispatchVtbl.GetIDsOfNamesInternal;
48+
vtblRaw[6] = (IntPtr)(delegate* unmanaged<IntPtr, int, IntPtr, int, ComTypes.INVOKEKIND, IntPtr, IntPtr, IntPtr, IntPtr, int>)&IDispatchVtbl.InvokeInternal;
49+
50+
wrapperEntry = (ComInterfaceEntry*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDispatchVtbl), sizeof(ComInterfaceEntry));
51+
wrapperEntry->IID = IDispatchVtbl.IID_IDispatch;
52+
wrapperEntry->Vtable = (IntPtr)vtblRaw;
53+
}
54+
55+
protected override unsafe ComInterfaceEntry* ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count)
56+
{
57+
// Always return the same table mappings.
58+
count = 1;
59+
return wrapperEntry;
60+
}
61+
62+
protected override object CreateObject(IntPtr externalComObject, CreateObjectFlags flags)
63+
{
64+
throw new NotImplementedException();
65+
}
66+
67+
protected override void ReleaseObjects(IEnumerable objects)
68+
{
69+
throw new NotImplementedException();
70+
}
71+
}
72+
public struct IDispatchVtbl
73+
{
74+
internal static readonly Guid IID_IDispatch = new Guid("00020400-0000-0000-C000-000000000046");
75+
76+
[UnmanagedCallersOnly]
77+
public static int GetTypeInfoCountInternal(IntPtr thisPtr, IntPtr i)
78+
{
79+
return 0; // S_OK;
80+
}
81+
82+
[UnmanagedCallersOnly]
83+
public static int GetTypeInfoInternal(IntPtr thisPtr, int itinfo, int lcid, IntPtr i)
84+
{
85+
return 0; // S_OK;
86+
}
87+
88+
[UnmanagedCallersOnly]
89+
public static int GetIDsOfNamesInternal(
90+
IntPtr thisPtr,
91+
IntPtr iid,
92+
IntPtr names,
93+
int namesCount,
94+
int lcid,
95+
IntPtr dispIds)
96+
{
97+
return 0; // S_OK;
98+
}
99+
100+
[UnmanagedCallersOnly]
101+
public static int InvokeInternal(
102+
IntPtr thisPtr,
103+
int dispIdMember,
104+
IntPtr riid,
105+
int lcid,
106+
ComTypes.INVOKEKIND wFlags,
107+
IntPtr pDispParams,
108+
IntPtr VarResult,
109+
IntPtr pExcepInfo,
110+
IntPtr puArgErr)
111+
{
112+
return 0; // S_OK;
113+
}
114+
}

0 commit comments

Comments
 (0)