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
1.99k stars 84 forks source link

The resulting SetProcessWorkingSetSize cannot be entered as -1 #1101

Closed wangfeijian closed 6 months ago

wangfeijian commented 6 months ago

Actual behavior

[DllImport("KERNEL32.dll", ExactSpelling = true, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern winmdroot.Foundation.BOOL SetProcessWorkingSetSize(winmdroot.Foundation.HANDLE hProcess, nuint dwMinimumWorkingSetSize, nuint dwMaximumWorkingSetSize);

Parameter dwMinimumWorkingSetSize and dwMaximumWorkingSetSize maybe -1, but nuint can't enter -1.

Expected behavior

[DllImport("KERNEL32.dll", ExactSpelling = true, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern winmdroot.Foundation.BOOL SetProcessWorkingSetSize(winmdroot.Foundation.HANDLE hProcess, nint dwMinimumWorkingSetSize, nint dwMaximumWorkingSetSize);

Repro steps

Context

riverar commented 6 months ago

You'll need to use unchecked((nuint)(-1)) for this one. Both metadata and CsWin32 are doing the right thing here, the API is just quirky.

The documentation hints at this, too:

If both dwMinimumWorkingSetSize and dwMaximumWorkingSetSize have the value (SIZE_T)–1, the function removes as many pages as possible from the working set of the specified process.

SIZE_T is a unsigned __int64 or ULONG_PTR on x64 targets.

wangfeijian commented 6 months ago

You'll need to use unchecked((nuint)(-1)) for this one. Both metadata and CsWin32 are doing the right thing here, the API is just quirky.

The documentation hints at this, too:

If both dwMinimumWorkingSetSize and dwMaximumWorkingSetSize have the value (SIZE_T)–1, the function removes as many pages as possible from the working set of the specified process.

SIZE_T is a unsigned __int64 or ULONG_PTR on x64 targets.

thank you! it works.