Skip to content
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

Interop.cs minor changes #41

Open
exoosh opened this issue Oct 29, 2024 · 0 comments
Open

Interop.cs minor changes #41

exoosh opened this issue Oct 29, 2024 · 0 comments

Comments

@exoosh
Copy link

exoosh commented Oct 29, 2024

Hi,

I just wanted to leave this here in case anyone is interested in adding this. It may not be suitable due to compatibility requirements on the project, but the changes get rid of some warnings for newer C# and .NET.

diff --git a/Interop.cs b/Interop.cs
index 99544ba..c864c8c 100644
--- a/Interop.cs
+++ b/Interop.cs
@@ -1,16 +1,10 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Linq;
+using System.ComponentModel;
 using System.Runtime.InteropServices;
-using System.Security.Principal;
 using System.Text;
-using System.Text.RegularExpressions;
 
 namespace SharpDPAPI
 {
     public class Interop
     {
         public enum CryptAlgClass : uint
         {
@@ -164,15 +158,15 @@ namespace SharpDPAPI
             {
                 Marshal.FreeHGlobal(buffer);
                 buffer = IntPtr.Zero;
             }
 
             public override string ToString()
             {
-                return Marshal.PtrToStringUni(buffer);
+                return Marshal.PtrToStringUni(buffer) ?? "";
             }
         }
 
         // From Vincent LE TOUX' "MakeMeEnterpriseAdmin"
         //  https://github.com/vletoux/MakeMeEnterpriseAdmin/blob/master/MakeMeEnterpriseAdmin.ps1#L1773-L1794
         [StructLayout(LayoutKind.Sequential)]
         public struct KERB_ECRYPT
@@ -280,15 +274,15 @@ namespace SharpDPAPI
             {
                 Marshal.FreeHGlobal(buffer);
                 buffer = IntPtr.Zero;
             }
 
             public override string ToString()
             {
-                return Marshal.PtrToStringUni(buffer);
+                return Marshal.PtrToStringUni(buffer) ?? "";
             }
         }
 
         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
         public struct DOMAIN_CONTROLLER_INFO
         {
             [MarshalAs(UnmanagedType.LPTStr)]
@@ -545,29 +539,33 @@ namespace SharpDPAPI
         public  static extern int NetApiBufferFree(IntPtr Buffer);
 
 
         public static string GetDCName()
         {
             // retrieves the current domain controller name
             // adapted from https://www.pinvoke.net/default.aspx/netapi32.dsgetdcname
-            DOMAIN_CONTROLLER_INFO domainInfo;
+            DOMAIN_CONTROLLER_INFO? domainInfo;
             const int ERROR_SUCCESS = 0;
             IntPtr pDCI = IntPtr.Zero;
 
             int val = DsGetDcName("", "", 0, "",
             DSGETDCNAME_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED |
             DSGETDCNAME_FLAGS.DS_RETURN_DNS_NAME |
             DSGETDCNAME_FLAGS.DS_IP_REQUIRED, out pDCI);
 
             if (ERROR_SUCCESS == val)
             {
-                domainInfo = (DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(pDCI, typeof(DOMAIN_CONTROLLER_INFO));
-                string dcName = domainInfo.DomainControllerName;
+                domainInfo = (DOMAIN_CONTROLLER_INFO?)Marshal.PtrToStructure(pDCI, typeof(DOMAIN_CONTROLLER_INFO));
+                if (domainInfo is null)
+                {
+                    return "";
+                }
+                string? dcName = domainInfo?.DomainControllerName;
                 NetApiBufferFree(pDCI);
-                return dcName.Trim('\\');
+                return dcName?.Trim('\\') ?? "";
             }
             else
             {
                 string errorMessage = new Win32Exception((int)val).Message;
                 Console.WriteLine("\r\n  [X] Error {0} retrieving domain controller : {1}", val, errorMessage);
                 NetApiBufferFree(pDCI);
                 return "";

Same patch as zipped up .diff file: patch.zip.

PS: this should apply cleanly to 4428263.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant