Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ internal static partial class Interop
internal static partial class NCrypt
{
[StructLayout(LayoutKind.Sequential)]
internal struct NCRYPT_UI_POLICY
internal unsafe struct NCRYPT_UI_POLICY
{
public int dwVersion;
public CngUIProtectionLevels dwFlags;
public IntPtr pszCreationTitle;
public IntPtr pszFriendlyName;
public IntPtr pszDescription;
public ushort* pszCreationTitle;
public ushort* pszFriendlyName;
public ushort* pszDescription;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ internal struct LSA_REFERENCED_DOMAIN_LIST
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct UNICODE_INTPTR_STRING
internal unsafe struct UNICODE_INTPTR_STRING
{
internal ushort Length;
internal ushort MaxLength;
internal IntPtr Buffer;
internal ushort* Buffer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Threading;

namespace Microsoft.Win32
Expand Down Expand Up @@ -516,17 +517,17 @@ private static void EnsureRegisteredSessionNotification()
}
}

private static UserPreferenceCategory GetUserPreferenceCategory(int msg, nint wParam, nint lParam)
private static unsafe UserPreferenceCategory GetUserPreferenceCategory(int msg, nint wParam, nint lParam)
{
UserPreferenceCategory pref = UserPreferenceCategory.General;

if (msg == Interop.User32.WM_SETTINGCHANGE)
{
if (lParam != 0 && Marshal.PtrToStringUni(lParam)!.Equals("Policy"))
if (lParam != 0 && Utf16StringMarshaller.ConvertToManaged((ushort*)lParam)!.Equals("Policy"))
{
pref = UserPreferenceCategory.Policy;
}
else if (lParam != 0 && Marshal.PtrToStringUni(lParam)!.Equals("intl"))
else if (lParam != 0 && Utf16StringMarshaller.ConvertToManaged((ushort*)lParam)!.Equals("intl"))
{
pref = UserPreferenceCategory.Locale;
}
Expand Down Expand Up @@ -1071,7 +1072,7 @@ private static void RemoveEventHandler(object key, Delegate? value)
/// <summary>
/// A standard Win32 window proc for our broadcast window.
/// </summary>
private IntPtr WindowProc(IntPtr hWnd, int msg, nint wParam, nint lParam)
private unsafe IntPtr WindowProc(IntPtr hWnd, int msg, nint wParam, nint lParam)
{
switch (msg)
{
Expand All @@ -1080,7 +1081,7 @@ private IntPtr WindowProc(IntPtr hWnd, int msg, nint wParam, nint lParam)
IntPtr newStringPtr = lParam;
if (lParam != 0)
{
newString = Marshal.PtrToStringUni(lParam);
newString = Utf16StringMarshaller.ConvertToManaged((ushort*)lParam);
if (newString != null)
{
newStringPtr = Marshal.StringToHGlobalUni(newString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace System.Data.ProviderBase
{
Expand Down Expand Up @@ -54,7 +55,7 @@ internal int Length
}
}

internal string PtrToStringUni(int offset)
internal unsafe string PtrToStringUni(int offset)
{
offset += BaseOffset;
Validate(offset, 2);
Expand All @@ -68,7 +69,7 @@ internal string PtrToStringUni(int offset)
DangerousAddRef(ref mustRelease);

IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
value = Marshal.PtrToStringUni(ptr);
value = Utf16StringMarshaller.ConvertToManaged((ushort*)ptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Data.Odbc compiles for .NET Standard, we can't use these APIs here.

Validate(offset, (2 * (value!.Length + 1)));
}
finally
Expand All @@ -82,7 +83,7 @@ internal string PtrToStringUni(int offset)
return value;
}

internal string PtrToStringUni(int offset, int length)
internal unsafe string PtrToStringUni(int offset, int length)
{
offset += BaseOffset;
Validate(offset, 2 * length);
Expand All @@ -96,7 +97,7 @@ internal string PtrToStringUni(int offset, int length)
DangerousAddRef(ref mustRelease);

IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
value = Marshal.PtrToStringUni(ptr, length);
value = new string((char*)ptr, 0, length);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Data.Common;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace System.Data.Odbc
{
Expand Down Expand Up @@ -278,7 +279,7 @@ internal ODBC32.SQLRETURN SpecialColumns(string quotedTable)
return retcode;
}

internal ODBC32.SQLRETURN Statistics(string? tableCatalog,
internal unsafe ODBC32.SQLRETURN Statistics(string? tableCatalog,
string? tableSchema,
string tableName,
short unique,
Expand All @@ -289,22 +290,22 @@ internal ODBC32.SQLRETURN Statistics(string? tableCatalog,
// MDAC Bug 75928 - SQLStatisticsW damages the string passed in
// To protect the tablename we need to pass in a copy of that string

IntPtr pwszTableName = Marshal.StringToCoTaskMemUni(tableName);
ushort* pwszTableName = Utf16StringMarshaller.ConvertToUnmanaged(tableName);
try
{
retcode = Interop.Odbc.SQLStatisticsW(this,
tableCatalog,
ODBC.ShortStringLength(tableCatalog),
tableSchema,
ODBC.ShortStringLength(tableSchema),
pwszTableName,
(IntPtr)pwszTableName,
ODBC.ShortStringLength(tableName),
unique,
accuracy);
}
finally
{
Marshal.FreeCoTaskMem(pwszTableName);
Utf16StringMarshaller.Free(pwszTableName);
}

ODBC.TraceODBC(3, "SQLStatisticsW", retcode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ private unsafe void TranslateSids(string target, IntPtr[] pSids)
SidListEntry entry = new SidListEntry();

Debug.Assert(name.Name.Length % 2 == 0);
entry.name = Marshal.PtrToStringUni(name.Name.Buffer, name.Name.Length / 2);
entry.name = new string((char*)name.Name.Buffer, 0, name.Name.Length / 2);

// Get the domain associated with this name
Debug.Assert(name.DomainIndex < domains.Length);
if (name.DomainIndex >= 0)
{
Interop.LSA_TRUST_INFORMATION domain = domains[name.DomainIndex];
Debug.Assert(domain.Name.Length % 2 == 0);
entry.sidIssuerName = Marshal.PtrToStringUni(domain.Name.Buffer, domain.Name.Length / 2);
entry.sidIssuerName = new string((char*)domain.Name.Buffer, 0, domain.Name.Length / 2);
}

entry.pSid = pSids[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,10 @@ public SidList(UnsafeNativeMethods.SID_AND_ATTR[] groupSidAndAttrs)
SidListEntry entry = new SidListEntry();

Debug.Assert(name.name.length % 2 == 0);
entry.name = Marshal.PtrToStringUni(name.name.buffer, name.name.length/2);
entry.name = new string((char*)name.name.buffer, 0, name.name.length/2);

Debug.Assert(domain.name.length % 2 == 0);
entry.sidIssuerName = Marshal.PtrToStringUni(domain.name.buffer, domain.name.length/2);
entry.sidIssuerName = new string((char*)domain.name.buffer, 0, domain.name.length/2);

entry.pSid = groupSidAndAttrs[i].pSid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Net.WebSockets;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Security;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Cryptography;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Security;

namespace System.Net
Expand Down Expand Up @@ -151,7 +152,7 @@ public NetworkCredential GetCredential(string? host, int port, string? authentic
return this;
}

private static string MarshalToString(SecureString sstr)
private static unsafe string MarshalToString(SecureString sstr)
{
if (sstr == null || sstr.Length == 0)
{
Expand All @@ -163,7 +164,7 @@ private static string MarshalToString(SecureString sstr)
try
{
ptr = Marshal.SecureStringToGlobalAllocUnicode(sstr);
result = Marshal.PtrToStringUni(ptr)!;
result = Utf16StringMarshaller.ConvertToManaged((ushort*)ptr)!;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Threading;

namespace System.Diagnostics.Tracing
Expand Down Expand Up @@ -86,28 +87,28 @@ internal static partial class EventPipeInternal
#if FEATURE_PERFTRACING
private unsafe struct EventPipeProviderConfigurationNative
{
private char* m_pProviderName;
private ushort* m_pProviderName;
private ulong m_keywords;
private uint m_loggingLevel;
private char* m_pFilterData;
private ushort* m_pFilterData;

internal static void MarshalToNative(EventPipeProviderConfiguration managed, ref EventPipeProviderConfigurationNative native)
{
native.m_pProviderName = (char*)Marshal.StringToCoTaskMemUni(managed.ProviderName);
native.m_pProviderName = Utf16StringMarshaller.ConvertToUnmanaged(managed.ProviderName);
native.m_keywords = managed.Keywords;
native.m_loggingLevel = managed.LoggingLevel;
native.m_pFilterData = (char*)Marshal.StringToCoTaskMemUni(managed.FilterData);
native.m_pFilterData = Utf16StringMarshaller.ConvertToUnmanaged(managed.FilterData);
}

internal void Release()
{
if (m_pProviderName != null)
{
Marshal.FreeCoTaskMem((IntPtr)m_pProviderName);
Utf16StringMarshaller.Free(m_pProviderName);
}
if (m_pFilterData != null)
{
Marshal.FreeCoTaskMem((IntPtr)m_pFilterData);
Utf16StringMarshaller.Free(m_pFilterData);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace System.Globalization
{
Expand Down Expand Up @@ -68,13 +69,13 @@ private string JSGetNativeDisplayName(string localeName, string cultureName)
}
}

internal static class Helper
internal static unsafe class Helper
{
internal static int MarshalAndThrowIfException(nint exceptionPtr, bool failOnlyDebug = false, string failureMessage = "")
{
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
string message = Utf16StringMarshaller.ConvertToManaged((ushort*)exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
if (failOnlyDebug)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.Versioning;
using System.Threading;

Expand Down Expand Up @@ -439,7 +440,7 @@ internal static unsafe JSFunctionBinding BindJSImportImpl(string functionName, s
nint exceptionPtr = Interop.Runtime.BindJSImportST(signature.Header);
if (exceptionPtr != IntPtr.Zero)
{
var message = Marshal.PtrToStringUni(exceptionPtr)!;
var message = Utf16StringMarshaller.ConvertToManaged((ushort*)exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new JSException(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public unsafe void ToManaged(out string? value)
return;
}
#if ENABLE_JS_INTEROP_BY_VALUE
value = Marshal.PtrToStringUni(slot.IntPtrValue, slot.Length);
value = new string((char*)slot.IntPtrValue, 0, slot.Length);
Marshal.FreeHGlobal(slot.IntPtrValue);
#else
fixed (void* argAsRoot = &slot.IntPtrValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Principal;
Expand All @@ -20,7 +21,7 @@ internal static class Win32
// Wrapper around advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW
//

internal static int ConvertSdToSddl(
internal static unsafe int ConvertSdToSddl(
byte[] binaryForm,
int requestedRevision,
SecurityInfos si,
Expand All @@ -40,7 +41,7 @@ internal static int ConvertSdToSddl(
// Extract data from the returned pointer
//

resultSddl = Marshal.PtrToStringUni(ByteArray)!;
resultSddl = Utf16StringMarshaller.ConvertToManaged((ushort*)ByteArray)!;

//
// Now is a good time to get rid of the returned pointer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Internal.Cryptography;
using Microsoft.Win32.SafeHandles;
using ErrorCode = Interop.NCrypt.ErrorCode;
Expand Down Expand Up @@ -365,9 +366,9 @@ public CngUIPolicy UIPolicy

NCRYPT_UI_POLICY* pNcryptUiPolicy = (NCRYPT_UI_POLICY*)pNcryptUiPolicyAndStrings;
uiProtectionLevel = pNcryptUiPolicy->dwFlags;
friendlyName = Marshal.PtrToStringUni(pNcryptUiPolicy->pszFriendlyName);
description = Marshal.PtrToStringUni(pNcryptUiPolicy->pszDescription);
creationTitle = Marshal.PtrToStringUni(pNcryptUiPolicy->pszCreationTitle);
friendlyName = Utf16StringMarshaller.ConvertToManaged(pNcryptUiPolicy->pszFriendlyName);
description = Utf16StringMarshaller.ConvertToManaged(pNcryptUiPolicy->pszDescription);
creationTitle = Utf16StringMarshaller.ConvertToManaged(pNcryptUiPolicy->pszCreationTitle);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Internal.Cryptography;
using Microsoft.Win32.SafeHandles;

Expand Down Expand Up @@ -76,7 +77,7 @@ public X509ChainElement[] ChainElements

X509Certificate2 certificate = new X509Certificate2((IntPtr)(pChainElement->pCertContext));
X509ChainStatus[] chainElementStatus = GetChainStatusInformation(pChainElement->TrustStatus.dwErrorStatus);
string information = Marshal.PtrToStringUni(pChainElement->pwszExtendedErrorInfo)!;
string information = Utf16StringMarshaller.ConvertToManaged(pChainElement->pwszExtendedErrorInfo)!;

X509ChainElement chainElement = new X509ChainElement(certificate, chainElementStatus, information);
chainElements[i] = chainElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ internal unsafe struct CERT_CHAIN_ELEMENT
public IntPtr pRevocationInfo;
public IntPtr pIssuanceUsage;
public IntPtr pApplicationUsage;
public IntPtr pwszExtendedErrorInfo;
public ushort* pwszExtendedErrorInfo;
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
Loading
Loading