Skip to content

Marshal.GetObjectForNativeVariant with IRecordInfo doesn't match netfx behaviour #27015

@hughbe

Description

@hughbe

From dotnet/corefx#31461

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[ActiveIssue()]
public void GetObjectForNativeVariant_Record_ReturnsExpected()
{
    int record = 10;
    var recordInfo = new RecordInfo { Guid = typeof(int).GUID };
    IntPtr pRecord = Marshal.AllocHGlobal(Marshal.SizeOf<int>());
    IntPtr pRecordInfo = Marshal.GetComInterfaceForObject<RecordInfo, IRecordInfo>(recordInfo);
    try
    {
        Marshal.StructureToPtr(record, pRecord, fDeleteOld: false);

        Variant variant = CreateVariant(VT_RECORD, new UnionTypes
        {
            _record = new Record
            {
                _record = pRecord,
                _recordInfo = pRecordInfo
            }
        });
        Assert.Equal(10, GetObjectForNativeVariant(variant));
        GetObjectForNativeVariant_NestedVariant_ReturnsExpected(variant, record);

        variant.m_Variant.vt |= VT_BYREF;
        Assert.Equal(10, GetObjectForNativeVariant(variant));
    }
    finally
    {
        Marshal.DestroyStructure<int>(pRecord);
        Marshal.FreeHGlobal(pRecord);
        Marshal.Release(pRecordInfo);
    }
}

This passes with netfx

On netcoreapp:

Discovering: System.Runtime.InteropServices.Tests
  Discovered:  System.Runtime.InteropServices.Tests
  Starting:    System.Runtime.InteropServices.Tests
     System.Runtime.InteropServices.Tests.GetObjectForNativeVariantTests.GetObjectForNativeVariant_Record_ReturnsExpect
  ed [FAIL]
        System.ArgumentException : The specified record cannot be mapped to a managed value class.
        Stack Trace:
              at System.Runtime.InteropServices.Marshal.GetObjectForNativeVariant(IntPtr pSrcNativeVariant)
           C:\Users\hughb\Documents\GitHub\corefx\src\System.Runtime.InteropServices\tests\System\Runtime\InteropServic
  es\Marshal\GetObjectForNativeVariantTests.cs(1027,0): at System.Runtime.InteropServices.Tests.GetObjectForNativeVaria
  ntTests.GetObjectForNativeVariant(Variant variant)
           C:\Users\hughb\Documents\GitHub\corefx\src\System.Runtime.InteropServices\tests\System\Runtime\InteropServic
  es\Marshal\GetObjectForNativeVariantTests.cs(700,0): at System.Runtime.InteropServices.Tests.GetObjectForNativeVarian
  tTests.GetObjectForNativeVariant_Record_ReturnsExpected()
  Finished:    System.Runtime.InteropServices.Tests

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions