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
2.12k stars 90 forks source link

WinRTCustomMarshaler is not always generated when needed #397

Open reflectronic opened 3 years ago

reflectronic commented 3 years ago

Actual behavior

Create a new application that references CsWin32 as well as the Windows Runtime APIs. Add this to Program.cs:

using Windows.System;
using Windows.Win32.System.WinRT;

unsafe
{
    Windows.Win32.PInvoke.CreateDispatcherQueueController(new DispatcherQueueOptions()
    {
        dwSize = (uint)sizeof(DispatcherQueueOptions),
        apartmentType = DISPATCHERQUEUE_THREAD_APARTMENTTYPE.DQTAT_COM_NONE,
        threadType = DISPATCHERQUEUE_THREAD_TYPE.DQTYPE_THREAD_DEDICATED
    }, out DispatcherQueueController dpc);
}

The application will crash when calling CreateDispatcherQueueController with the following exception:

An unhandled exception of type 'System.TypeLoadException' occurred in ConsoleApp1.dll Could not load type 'Windows.Win32.CsWin32.InteropServices.WinRTCustomMarshaler' from assembly 'ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Manually copying the code for WinRTCustomMarshaler into the application seems to resolve the issue.

Repro steps

NativeMethods.txt content:

CreateDispatcherQueueController

Context

jnm2 commented 3 years ago

Same as https://github.com/microsoft/CsWin32/issues/246#issuecomment-916934416?

DHancock commented 2 years ago

It's still a problem in 0.2.104-beta

brandon3343 commented 1 year ago

the same problem, temporarily use manual generation

ghost1372 commented 1 week ago

same problem! and there is no fix after4/5 years @AArnott