Skip to content

[ComInterfaceGenerator] Guid Marshalling Support #87894

Closed

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.

Activity

  1. ghost added
    untriaged
    on Jun 21, 2023
  2. jkoritzinsky commented on Jun 21, 2023

    @jkoritzinsky
    Member

    We do not support LPStruct in source generated marshalling at all. It's a confusing option that makes the unmanaged signature differ in ways from the managed signature that we dislike and there's already a good alternative using existing C# syntax with in Guid that we already support.

    Automatically changing LPStruct marshalling to use the in keyword when the user uses the code-fix to convert to source-generated COM is an interesting idea.

    We have considered treating some built-in types as "well known to be always blittable" to avoid requiring DisableRuntimeMarshalling (see #73975). Guid is another interesting one to consider adding to the list.

  3. removed
    untriaged
    on Jun 25, 2023
  4. added this to the 8.0.0 milestone on Jun 25, 2023
  5. ghost added
    in-pr
    on Jun 29, 2023
  6. ghost removed
    in-pr
    on Jun 30, 2023
  7. ghost locked as resolved and limited conversation to collaborators on Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions