Why can I not mix Guid and string members in a ComVisible struct?

2

Consider the following interface (registered with regasm /tlb with a c# out of process server and client):

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IComTestTarget
{
    InterestingRecord[] GetInterestingRecords();
}

[ComVisible(true)]
public struct InterestingRecord
{
    public Guid guid;

    [MarshalAs(UnmanagedType.BStr)]
    public string ChatText;
}

If I try to call GetInterestingRecords, I receive an exception in the server process:

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at GetRecordInfoFromTypeInfo
at OleVariant::CreateSafeArrayDescriptorForArrayRef
at System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToNative

If I comment out the line public Guid guid; or if I comment the line [MarshalAs(UnmanagedType.BStr)] public string ChatText;, the call completes as expected. If I change the interface to return a single value rather than an array, the call completes as expected. What am I doing wrong?

Runnable minable example at GitHub

c#
com
marshalling
asked on Stack Overflow Oct 19, 2017 by Mitch

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0