Skip to content

Commit

Permalink
Split Variant marshalling tests (#53035)
Browse files Browse the repository at this point in the history
* Rework of variant tests split

* Fix testing of disabled built-in COM
  • Loading branch information
kant2002 authored May 30, 2021
1 parent ed92fa5 commit 8538a75
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 95 deletions.
15 changes: 13 additions & 2 deletions src/coreclr/vm/interopconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,29 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, ComIpType ReqIpType, ComIpType
if (ReqIpType & ComIpType_Dispatch)
{
hr = SafeQueryInterface(pUnk, IID_IDispatch, &pvObj);
pUnk->Release();
if (SUCCEEDED(hr))
{
pUnk->Release();
FetchedIpType = ComIpType_Dispatch;
}
else if (ReqIpType & ComIpType_Unknown)
{
hr = S_OK;
pvObj = pUnk;
FetchedIpType = ComIpType_Unknown;
}
}
else
{
pvObj = pUnk;
FetchedIpType = ComIpType_Unknown;
}

if (FAILED(hr))
COMPlusThrowHR(hr);

if (pFetchedIpType != NULL)
*pFetchedIpType = ReqIpType;
*pFetchedIpType = FetchedIpType;

RETURN pvObj;
}
Expand Down
37 changes: 37 additions & 0 deletions src/tests/Interop/PInvoke/Variant/VariantTest.BuiltInCom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using TestLibrary;
using static VariantNative;

#pragma warning disable CS0612, CS0618
partial class Test
{
public static int Main()
{
bool builtInComDisabled=false;
var comConfig = AppContext.GetData("System.Runtime.InteropServices.BuiltInComInterop.IsSupported");
if(comConfig != null && !bool.Parse(comConfig.ToString()))
{
builtInComDisabled=true;
}

Console.WriteLine($"Built-in COM Disabled?: {builtInComDisabled}");
try
{
TestByValue(!builtInComDisabled);
TestByRef(!builtInComDisabled);
TestOut();
TestFieldByValue(!builtInComDisabled);
TestFieldByRef(!builtInComDisabled);
}
catch (Exception e)
{
Console.WriteLine($"Test failed: {e}");
return 101;
}
return 100;
}
}
114 changes: 114 additions & 0 deletions src/tests/Interop/PInvoke/Variant/VariantTest.ComWrappers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using TestLibrary;
using static VariantNative;
using ComTypes = System.Runtime.InteropServices.ComTypes;

#pragma warning disable CS0612, CS0618
partial class Test
{
public static int Main()
{
bool testComMarshal=true;
ComWrappers.RegisterForMarshalling(new ComWrappersImpl());
try
{
TestByValue(testComMarshal);
TestByRef(testComMarshal);
TestOut();
TestFieldByValue(testComMarshal);
TestFieldByRef(testComMarshal);
}
catch (Exception e)
{
Console.WriteLine($"Test failed: {e}");
return 101;
}
return 100;
}
}

internal unsafe class ComWrappersImpl : ComWrappers
{
private static readonly ComInterfaceEntry* wrapperEntry;

static ComWrappersImpl()
{
var vtblRaw = (IntPtr*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDispatchVtbl), sizeof(IntPtr) * 7);
GetIUnknownImpl(out vtblRaw[0], out vtblRaw[1], out vtblRaw[2]);

vtblRaw[3] = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, int>)&IDispatchVtbl.GetTypeInfoCountInternal;
vtblRaw[4] = (IntPtr)(delegate* unmanaged<IntPtr, int, int, IntPtr, int>)&IDispatchVtbl.GetTypeInfoInternal;
vtblRaw[5] = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, IntPtr, int, int, IntPtr, int>)&IDispatchVtbl.GetIDsOfNamesInternal;
vtblRaw[6] = (IntPtr)(delegate* unmanaged<IntPtr, int, IntPtr, int, ComTypes.INVOKEKIND, IntPtr, IntPtr, IntPtr, IntPtr, int>)&IDispatchVtbl.InvokeInternal;

wrapperEntry = (ComInterfaceEntry*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDispatchVtbl), sizeof(ComInterfaceEntry));
wrapperEntry->IID = IDispatchVtbl.IID_IDispatch;
wrapperEntry->Vtable = (IntPtr)vtblRaw;
}

protected override unsafe ComInterfaceEntry* ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count)
{
// Always return the same table mappings.
count = 1;
return wrapperEntry;
}

protected override object CreateObject(IntPtr externalComObject, CreateObjectFlags flags)
{
throw new NotImplementedException();
}

protected override void ReleaseObjects(IEnumerable objects)
{
throw new NotImplementedException();
}
}
public struct IDispatchVtbl
{
internal static readonly Guid IID_IDispatch = new Guid("00020400-0000-0000-C000-000000000046");

[UnmanagedCallersOnly]
public static int GetTypeInfoCountInternal(IntPtr thisPtr, IntPtr i)
{
return 0; // S_OK;
}

[UnmanagedCallersOnly]
public static int GetTypeInfoInternal(IntPtr thisPtr, int itinfo, int lcid, IntPtr i)
{
return 0; // S_OK;
}

[UnmanagedCallersOnly]
public static int GetIDsOfNamesInternal(
IntPtr thisPtr,
IntPtr iid,
IntPtr names,
int namesCount,
int lcid,
IntPtr dispIds)
{
return 0; // S_OK;
}

[UnmanagedCallersOnly]
public static int InvokeInternal(
IntPtr thisPtr,
int dispIdMember,
IntPtr riid,
int lcid,
ComTypes.INVOKEKIND wFlags,
IntPtr pDispParams,
IntPtr VarResult,
IntPtr pExcepInfo,
IntPtr puArgErr)
{
return 0; // S_OK;
}
}
Loading

0 comments on commit 8538a75

Please sign in to comment.