microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2k stars 84 forks source link

`struct*` return types on extern methods generated without pointer when marshaling is allowed #992

Open Nuklon opened 1 year ago

Nuklon commented 1 year ago

Actual behavior

It's generated like this:

        [DllImport("WINTRUST.dll", ExactSpelling = true)]
        [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
        [SupportedOSPlatform("windows5.1.2600")]
        internal static extern winmdroot.Security.WinTrust.CRYPT_PROVIDER_DATA WTHelperProvDataFromStateData(winmdroot.Foundation.HANDLE hStateData);

But it should return a pointer to CRYPT_PROVIDER_DATA.

In the metadata this is OK:

        [SupportedOSPlatform("windows5.1.2600")]
        [Documentation("https://docs.microsoft.com/windows/win32/api/wintrust/nf-wintrust-wthelperprovdatafromstatedata")]
        [DllImport("WINTRUST.dll", ExactSpelling = true, PreserveSig = false)]
        public unsafe static extern CRYPT_PROVIDER_DATA* WTHelperProvDataFromStateData([In] HANDLE hStateData);

Expected behavior

A clear and concise description of what you expected to happen.

Repro steps

  1. NativeMethods.txt content:
    WTHelperProvDataFromStateData

Context

Nuklon commented 1 year ago

WTHelperGetProvSignerFromChain should also take a pointer.

Nuklon commented 1 year ago

A workaround is to set allowMarshaling to false. It seems there's some logic wrong there as this shouldn't be done here.

AArnott commented 1 year ago

Thanks for the excellent bug report. I was able to repro.

Nuklon commented 6 months ago

@AArnott, what would be needed to get this fixed somehow? I'm not entirely sure where to fix this in the code.

AArnott commented 6 months ago

Fixes always start with a test case added to the repo that fails. Then the challenge is to understand the problem enough to change the source generator to recognize the scenario and fix it -- without breaking any other tests. That's usually not too hard. I just haven't had time to take this one on yet. Anything you can do toward it though would be most welcome.