Skip to content

New source generators mode does not generate working cocreateable classes (e.g. ShellLink) #1500

@Marv51

Description

@Marv51

Actual behavior

The new source-generator based COM (useComSourceGenerators: true) generates a class with a (traditional) "ComImport" attribute for a cocreateable class like ShellLink. This class is incompatible with the interfaces generated with the same configuration.

Expected behavior

The class and the interface are compatible with each other, both using the same technique.

Repro steps

  1. NativeMethods.txt content:
IPersistFile
IShellLinkW
ShellLink
  1. NativeMethods.json content (if present):
{
  "$schema": "https://aka.ms/CsWin32.schema.json",
  "comInterop": {
    "useComSourceGenerators": true
  }
}
  1. Any of your own code that should be shared?
var shellLink = new ShellLink();
var shellLinkW = (IShellLinkW)shellLink;

shellLinkW.SetPath("explorer.exe");
// some more calls
var persistFile = (IPersistFile)shellLink;
persistFile.Save(shortcut, true);

Both casts in that code snippet create this warning: SYSLIB1099 Casting between a ComImport type and a source-generated COM type is not supported

And the warning looks to be accurate the generated ShellLink:

[Guid("00021401-0000-0000-C000-000000000046"),ComImport()]
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Windows.CsWin32", "0.3.217+533aa1bddf.RR")]
internal partial class ShellLink
{
}

the generated IShellLinkW:

[Guid("000214F9-0000-0000-C000-000000000046"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown),GeneratedComInterface()]
[SupportedOSPlatform("windows5.1.2600")]
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Windows.CsWin32", "0.3.217+533aa1bddf.RR")]
internal unsafe partial interface IShellLinkW
{

I believe that this line is responsible:

.AddAttributeLists(AttributeList().AddAttributes(GUID(guid), ComImportAttributeSyntax));

But I do not know how to fix it.

For further context, I believe this issue created these classes: #453

Context

  • CsWin32 version: 0.3.217
  • Win32Metadata version (if explicitly set by project): not set
  • Target Framework: net9.0-windows10.0.26100.0
  • LangVersion (if explicitly set by project): [e.g. 9] not set

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions