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.06k stars 87 forks source link

Regression for `WriteFile` friendly overload #913

Closed alexrp closed 1 year ago

alexrp commented 1 year ago

Actual behavior

image

Expected behavior

This overload used to take a byte*. byte? is definitely not right.

Repro steps

  1. NativeMethods.txt content:

    WriteFile
  2. NativeMethods.json content (if present):

    {
    "className": "WindowsPInvoke",
    "allowMarshaling": false,
    "wideCharOnly": false,
    "emitSingleFile": true
    }

Context

AArnott commented 1 year ago

Thank you for the report. I see in the metadata that this API is declared as:

public unsafe static extern BOOL WriteFile([In] HANDLE hFile, [Optional][In][Const][MemorySize(BytesParamIndex = 2)] byte* lpBuffer, [In] uint nNumberOfBytesToWrite, [Optional][Out] uint* lpNumberOfBytesWritten, [Optional][In][Out] OVERLAPPED* lpOverlapped);

My guess is because lpBuffer isn't using the NativeArrayInfoAttribute that we decide it isn't an array. I'm not sure if the metadata should be using one or the other, but CsWin32 can be updated to recognize MemorySize as well.