You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 "";
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.
Same patch as zipped up .diff file: patch.zip.
PS: this should apply cleanly to 4428263.
The text was updated successfully, but these errors were encountered: