-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
External methods with Span<T> overload need the correct buffer size passed #90
Comments
I think some APIs need the byte length and others need the element count, and bad information was gathered about which way VirtualQueryEx is (and maybe other APIs). |
Great report. I have filed a new bug against the metadata that must first be fixed, then we can update CsWin32 to adjust code gen based on their update. |
Ironically, it never came up till now that the 3rd parameter isn't even supposed to be an array. Go figure. But you still caught a bug in our handling of length by count vs length in bytes, so I'm finalizing a fix for that. |
But our own bug is implicitly fixed by the metadata change, so I'll close this with no further changes. |
In Use:
When adding VirtualQueryEx to NativeMethods.txt the following is produced:
The overload with
Span<MEMORY_BASIC_INFORMATION>
is not working as expected.After executing, the returned int is 0 and the buffer contains only default values.
I am totaly new to P/Invoke, Span and so on, so i had to lookup some stuff and experiment.
I hope my understanding is somewhat correct.
I have manually created an external method and used the types from Microsoft.Windows.Sdk.PInvoke to build this working example:
The parameter dwLength needs the size of the buffer.
Span.Length returns the number of elements of type T.
So the length will be 1 for
Span<MEMORY_BASIC_INFORMATION> span = new MEMORY_BASIC_INFORMATION[1];
.But a single element of type
MEMORY_BASIC_INFORMATION
does not have the size of 1 byte.So the actual size of the buffer-size has to be queried
Marshal.SizeOf<MEMORY_BASIC_INFORMATION>()
.Passing the size of the element type should be sufficient in this case, because VirtualQueryEx will only provide a single element anyway.
Not sure if it is a good pattern in this case, but i have also multiplied the size of the element type with the buffer length.
I am also not sure of other cases, but if there are any cases that handle multiple elements, it might be a good practice to multiply the element size with the buffer length.
The text was updated successfully, but these errors were encountered: