Repository navigation
Description
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
jkoritzinsky commentedon Jun 21, 2023
We do not support
LPStructin 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 within Guidthat we already support.Automatically changing
LPStructmarshalling to use theinkeyword 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).
Guidis another interesting one to consider adding to the list.