From 4990310ebdfcb2078cb83ac7f1b33767282d062c Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Sat, 12 Feb 2022 17:23:07 -0700 Subject: [PATCH] Fix marshaling --- .../ActiveDirectory/NativeMethods.cs | 4 +-- .../ActiveDirectory/Utils.cs | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/NativeMethods.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/NativeMethods.cs index aa2c34967bbd7..7edeb5cc32f82 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/NativeMethods.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/NativeMethods.cs @@ -484,14 +484,14 @@ internal static partial void DnsRecordListFree( LPTSTR* rpNames, PDS_NAME_RESULT* ppResult );*/ - internal delegate int DsCrackNames( + internal unsafe delegate int DsCrackNames( [In] IntPtr hDS, [In] int flags, [In] int formatOffered, [In] int formatDesired, [In] int nameCount, [In] IntPtr names, - [Out] out IntPtr results); + [Out] IntPtr* results); /*NTSTATUS LsaConnectUntrusted( PHANDLE LsaHandle diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs index 6898e76054307..7587da383a48e 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs @@ -99,11 +99,10 @@ internal sealed class Utils // To disable public/protected constructors for this class private Utils() { } - internal static string GetDnsNameFromDN(string distinguishedName) + internal static unsafe string GetDnsNameFromDN(string distinguishedName) { int result = 0; string? dnsName = null; - IntPtr results = IntPtr.Zero; Debug.Assert(distinguishedName != null); @@ -118,8 +117,14 @@ internal static string GetDnsNameFromDN(string distinguishedName) IntPtr name = Marshal.StringToHGlobalUni(distinguishedName); IntPtr ptr = Marshal.AllocHGlobal(IntPtr.Size); Marshal.WriteIntPtr(ptr, name); - result = dsCrackNames(IntPtr.Zero, NativeMethods.DS_NAME_FLAG_SYNTACTICAL_ONLY, - NativeMethods.DS_FQDN_1779_NAME, NativeMethods.DS_CANONICAL_NAME, 1, ptr, out results); + + IntPtr results; + fixed (IntPtr* resultsPtr = &IntPtr.Zero) + { + result = dsCrackNames(IntPtr.Zero, NativeMethods.DS_NAME_FLAG_SYNTACTICAL_ONLY, + NativeMethods.DS_FQDN_1779_NAME, NativeMethods.DS_CANONICAL_NAME, 1, ptr, resultsPtr); + results = *resultsPtr; + } if (result == 0) { try @@ -187,11 +192,10 @@ internal static string GetDnsNameFromDN(string distinguishedName) return dnsName!; } - internal static string GetDNFromDnsName(string dnsName) + internal static unsafe string GetDNFromDnsName(string dnsName) { int result = 0; string? dn = null; - IntPtr results = IntPtr.Zero; Debug.Assert(dnsName != null); @@ -205,8 +209,13 @@ internal static string GetDNFromDnsName(string dnsName) IntPtr name = Marshal.StringToHGlobalUni(dnsName + "/"); IntPtr ptr = Marshal.AllocHGlobal(IntPtr.Size); Marshal.WriteIntPtr(ptr, name); - result = dsCrackNames(IntPtr.Zero, NativeMethods.DS_NAME_FLAG_SYNTACTICAL_ONLY, - NativeMethods.DS_CANONICAL_NAME, NativeMethods.DS_FQDN_1779_NAME, 1, ptr, out results); + IntPtr results; + fixed (IntPtr* resultsPtr = &IntPtr.Zero) + { + result = dsCrackNames(IntPtr.Zero, NativeMethods.DS_NAME_FLAG_SYNTACTICAL_ONLY, + NativeMethods.DS_CANONICAL_NAME, NativeMethods.DS_FQDN_1779_NAME, 1, ptr, resultsPtr); + results = *resultsPtr; + } if (result == 0) { try