Closed chucker closed 4 months ago
Thanks for your feedback.
The only safe time to initialize that field is when the struct is created. Various methods may take pointers to those structs, at which point those methods don't actually know the length of the struct and that's why win32 needs you to specify the size of it.
Furthermore, based on the header files / metadata that CsWin32 has to work with, there is just no way to know that a ref
parameter only needed one size-related field to be initialized and thus turn it to out
and initialize it itself.
Thanks for explaining!
https://github.com/microsoft/CsWin32/issues/108 seems to capture the potentially actionable part of what you're asking for.
That does look like an improvement. :)
Is your feature request related to a problem? Please describe. I'm trying to replace custom PInvoke wrappers with this library to reduce brittle code I have to maintain. As an example call, I picked
GlobalMemoryStatusEx
, and found that it didn't initially work as expected.It turned out that, similarly to here, the root cause was that the metadata was incorrect. By explicitly (rather than transitively) referencing a newer
Microsoft.Windows.SDK.Win32Metadata
, the source generator created a slightly different method. Before, it took anout
parameter, suggesting I didn't have to pass a struct; now, it takes aref
.However, this now means I have to externally initialize the struct's
dwLength
:This strikes me as unnecessary boilerplate.
Windows.Win32.System.SystemInformation.MEMORYSTATUSEX
is a source generator-emitted type, andWindows.Win32.PInvoke.GlobalMemoryStatusEx
a source generator-emitted method, so why pass it information it can already know?Describe the solution you'd like With
friendlyOverloads
enabled, an overload should be offered that skips this boilerplate. In that case, the parameter should revert to beingout
, since there's really no useful information to pass in, something like:…so that I can do:
More than that, given that
Windows.Win32.System.SystemInformation.MEMORYSTATUSEX
is emitted by the source generator in the first place, I'm not sure the call toMarshal.SizeOf
should be even necessary.I apologize if this has been discussed before; I did try to look for similar issues.