Skip to content
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

Split Variant marshalling tests #53035

Merged
merged 9 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/coreclr/vm/interopconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,19 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, ComIpType ReqIpType, ComIpType
{
hr = SafeQueryInterface(pUnk, IID_IDispatch, &pvObj);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
pUnk->Release();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Release shouldn't be called indiscriminately here. We are checking for IDispatch now and it could fail, but ComIpType_Unknown must still be returned. Move this release into the success path below.

FetchedIpType = ComIpType_Dispatch;
}
else
{
pvObj = pUnk;
FetchedIpType = ComIpType_Unknown;
}

if (FAILED(hr))
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
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