Skip to content

Commit

Permalink
Fix marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley committed Feb 13, 2022
1 parent a1f0147 commit 4990310
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down

0 comments on commit 4990310

Please sign in to comment.