-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Replace Marshal.PtrToStringUni and Marshal.StringToCoTaskMemUni with Utf16StringMarshaller #118997
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: AaronRobinsonMSFT <30635565+AaronRobinsonMSFT@users.noreply.github.com>
FWIW, I do not expect copilot to have good success on this one. This is Window specific code for the most port. Github Copilot builds on Linux only currently. It makes it to not do well on Windows specific code since it cannot see the build breaks and fix them automatically. It makes it require a lot of handholding on Windows specific code. The workaround is to run a local Copilot on your Windows machine. |
Co-authored-by: AaronRobinsonMSFT <30635565+AaronRobinsonMSFT@users.noreply.github.com>
Co-authored-by: AaronRobinsonMSFT <30635565+AaronRobinsonMSFT@users.noreply.github.com>
Co-authored-by: AaronRobinsonMSFT <30635565+AaronRobinsonMSFT@users.noreply.github.com>
Marshal.PtrToStringUni
methods with Utf16StringMarshaller
Boo. I didn't realize that at all. Okay. |
@@ -338,7 +339,7 @@ private static bool HasExplicitParameters(SafeBCryptKeyHandle bcryptHandle) | |||
{ | |||
fixed (byte* pValue = &value[0]) | |||
{ | |||
string? valueAsString = Marshal.PtrToStringUni((IntPtr)pValue); | |||
string? valueAsString = Utf16StringMarshaller.ConvertToManaged((ushort*)pValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use Encoding.Unicode.GetString(Byte[])
?
This PR systematically replaces direct calls to
Marshal.PtrToStringUni
andMarshal.StringToCoTaskMemUni
with equivalent methods fromUtf16StringMarshaller
throughout the codebase, improving type consistency and centralizing UTF-16 string marshalling logic.Changes Made
Utf16StringMarshaller.ConvertToManaged()
andUtf16StringMarshaller.ConvertToUnmanaged()
IntPtr
/char*
toushort*
for UTF-16 string pointers, eliminating unnecessary castsunsafe
modifiers to methods and structs where needed for unmanaged pointer usageushort*
for string fieldsKey Files Updated
Pattern Examples
Before:
After:
For cases requiring length specification, the string constructor is used:
All changes maintain backward compatibility while improving type safety and consistency. Interop tests were excluded from modifications as specified. The original
Utf16StringMarshaller.cs
implementation remains unchanged, serving as the centralized marshalling implementation.Fixes #118988.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.