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

Seems a `MONITORINFOF_PRIMARY` const/enum is not defined/generated when using `PInvoke.GetMonitorInfo()` and `MONITORINFO` struct #1004

Closed mbodm closed 11 months ago

mbodm commented 11 months ago

Actual behavior

Using this code, including the following generated PInvoke stuff:

var hWnd = PInvoke.GetDesktopWindow();
var hMonitor = PInvoke.MonitorFromWindow(hWnd, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTOPRIMARY);

var mi = new MONITORINFO();
mi.cbSize = (uint)Marshal.SizeOf(mi);
PInvoke.GetMonitorInfo(hMonitor, ref mi);

Console.WriteLine(mi.dwFlags); // Prints "1" to the console.

if (mi.dwFlags == MONITORINFOF_PRIMARY) // Not possible at the moment, in any form.
{
    Console.WriteLine("Hooray!");
}

The MONITORINFO struct contains a dwFlags field. There should be a const/enum or something named MONITORINFOF_PRIMARY, to compare the dwFlags value with.

Some other PInvoke generated functions have those. Example given:

Windows.Win32.Graphics.Gdi.MONITOR_FROM_FLAGS.MONITOR_DEFAULTTOPRIMARY

exists along with

PInvoke.MonitorFromWindow()

function.

In short: There is no MONITORINFOF_PRIMARY const/enum.

Expected behavior

There should be a MONITORINFOF_PRIMARY const/enum.

Repro steps

Context

AArnott commented 11 months ago

The metadata does declare a MONITORINFOF_PRIMARY constant, so if you add that specifically to your NativeMethods.txt file, you'll get what you need. CsWin32 doesn't auto-generate it because the APIs you're asking for don't directly reference constants -- only enums, structs, and the like. If you feel we should be using an enum here instead of a uint though, you can open an issue over in the win32metadata repo.

mbodm commented 11 months ago

Oh, then i´m sorry!

I didn´t know you can also put in types, structs or consts into the NativeMethods.txt file. If it´s mentioned in the documentation, i maybe missed that part. Cause of the file name of the.txt file, i assumed you can only put in function names in there. And everyting else, related to that function(s), is resolved/created automatically. My bad!

Thanks a lot for clarifying this and sorry when i wasted your sparse time!

AArnott commented 11 months ago

No worries. FYI the docs on NativeMethods.txt and what it can contain are here: https://github.com/microsoft/CsWin32#usage

mbodm commented 11 months ago

No worries. FYI the docs on NativeMethods.txt and what it can contain are here: https://github.com/microsoft/CsWin32#usage

Yeah, it seems i missed that part:

The name of a struct, enum, constant or interface to generate. This may be qualified with a namespace but is only recommended in cases of ambiguity, which CsWin32 will prompt where appropriate.

The name „NativeMethods“ missleaded me to „methods/functions only“.

Anyway, all fine and thx for clarifying!