-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Proposal: Interop marshaling for Span<T> #9113
Comments
In general, we built a lot of "convenience magic" into the CLR 1.0 P/Invoke mechanism, but generally, I find myself preferring to limit reliance on the more advanced magic and making any "complicated" marshaling steps explicit in my code. |
So, can use this syntax now in .NET 5? [DllImport("kernel32.dll")]
static extern uint GetTempPath(uint nBufferLength, Span<char> lpBuffer); |
No |
There are no plans to update the built-in system to use |
One of the valuable aspects of Span is it keeps code safe and largely pointer-free. However, at the interop boundary, Span isn't supported, which leads to code like:
This both forces the method to become unsafe and is a lot of extra work. We should add marshaling support for Span to allow it to be used end-to-end.
For the most part, existing array marshaling semantics are a good match for Spans. For example:
would marshal the Span's underlying pointer (pinning as necessary). Reverse interop is a bit trickier. For example, the native signature of HeapAlloc is:
LPVOID WINAPI HeapAlloc(_In_ HANDLE hHeap, _In_ DWORD dwFlags, _In_ SIZE_T dwBytes);
If we wanted a span wrapping the returned pointer, an ideal P/Invoke would look like:
but that doesn't tell the Span its length. It may be enough to add MarshalAsAttribute with SizeParamIndex (or SizeConst):
Open questions:
The text was updated successfully, but these errors were encountered: