Skip to content

[ComInterfaceGenerator] Guid Marshalling Support #87894

@lordmilko

Description

@lordmilko

The new ComInterfaceGenerator does not appear to natively support marshalling parameters of type Guid.

Methods that take a Guid as an input parameter such as the following

int ResolveTypeRef(
    int tr,
    [MarshalAs(UnmanagedType.LPStruct)] Guid riid,
    [MarshalAs(UnmanagedType.Interface)] out object ppIScope,
    out int ptd);

give an error

The specified 'MarshalAsAttribute' configuration for parameter 'riid' is not supported by source-generated COM. If the specified configuration is required, use `ComImport` instead.

It appears this error can be circumvented by applying [assembly: DisableRuntimeMarshalling] and changing the parameter to in Guid riid

Methods that emit a Guid as an out parameter, such as

int GetScopeProps(
    [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U2, SizeParamIndex = 1)] char[] szName,
    int cchName,
    out int pchName,
    out Guid pmvid);

Emit an error

Runtime marshalling must be disabled in this project by applying the 'System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute' to the assembly to enable marshalling this type. The generated source will not handle marshalling of parameter 'pmvid'.

which does go away when [assembly: DisableRuntimeMarshalling] is applied.

Having apply DisableRuntimeMarshallingAttribute seems like a somewhat heavy-handed way of facilitating Guid marshalling, and is problematic if you need to retain runtime marshalling for other purposes. I imagine in order to marshal Guid without disabling runtime marshalling, a custom marshaller needs to be defined. As Guid is a type provided by the runtime, I would present any custom Guid marshaller should be provided by the runtime as well.

For parameters that already have a MarshalAsAttribute it does seem that ComInterfaceGenerator may apply a custom marshaller based on the UnmanagedType that has been defined. Ostensibly this could be extended to apply to LPStruct as well, however despite the fact LPStruct states it is only supported with Guid, people do sometimes erroneously apply it to other parameter types as well, which may create challenges for the potential goal of automatic Guid marshalling support.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions