Skip to content

Commit

Permalink
Use GeneratedDllImport
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Nov 23, 2021
1 parent f72e73b commit 8b3815e
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace System.DirectoryServices.Interop
{
internal static class SafeNativeMethods
internal static partial class SafeNativeMethods
{
[DllImport(global::Interop.Libraries.OleAut32)]
public static extern void VariantInit(IntPtr pObject);
[GeneratedDllImport(global::Interop.Libraries.OleAut32)]
public static partial void VariantInit(IntPtr pObject);

[DllImport(global::Interop.Libraries.Activeds)]
public static extern bool FreeADsMem(IntPtr pVoid);
[GeneratedDllImport(global::Interop.Libraries.Activeds)]
public static partial bool FreeADsMem(IntPtr pVoid);

public const int
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200,
Expand All @@ -22,11 +22,11 @@ public const int
ERROR_MORE_DATA = 234,
ERROR_SUCCESS = 0;

[DllImport(global::Interop.Libraries.Activeds, CharSet = CharSet.Unicode)]
public static extern unsafe int ADsGetLastError(out int error, char* errorBuffer, int errorBufferLength, char* nameBuffer, int nameBufferLength);
[GeneratedDllImport(global::Interop.Libraries.Activeds, CharSet = CharSet.Unicode)]
public static unsafe partial int ADsGetLastError(out int error, char* errorBuffer, int errorBufferLength, char* nameBuffer, int nameBufferLength);

[DllImport(global::Interop.Libraries.Activeds, CharSet = CharSet.Unicode)]
public static extern int ADsSetLastError(int error, string? errorString, string? provider);
[GeneratedDllImport(global::Interop.Libraries.Activeds, CharSet = CharSet.Unicode)]
public static partial int ADsSetLastError(int error, string? errorString, string? provider);

public class EnumVariant
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0</TargetFrameworks>
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
<!-- Suppressions to avoid ifdefs:
CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring'
CA1846: Prefer 'AsSpan' over 'Substring' when span-based overloads are available -->
<NoWarn>$(NoWarn);CA1845;CA1846</NoWarn>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
Expand Down Expand Up @@ -246,13 +250,15 @@ System.DirectoryServices.ActiveDirectory.DomainController</PackageDescription>
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.IO.FileSystem" />
<Reference Include="System.IO.FileSystem.AccessControl" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.NameResolution" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.Net.Security" />
<Reference Include="System.ObjectModel" />
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Security.AccessControl" />
<Reference Include="System.Security.Claims" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ internal static string GetLoggedOnDomain()
{
string? domainName = null;

NegotiateCallerNameRequest requestBuffer = new NegotiateCallerNameRequest();
NegotiateCallerNameRequest requestBuffer = default;
int requestBufferLength = (int)Marshal.SizeOf(requestBuffer);

IntPtr pResponseBuffer = IntPtr.Zero;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ public OSVersionInfoEx()
LUID LogonId ;
} NEGOTIATE_CALLER_NAME_REQUEST, *PNEGOTIATE_CALLER_NAME_REQUEST ;*/
[StructLayout(LayoutKind.Sequential)]
internal sealed class NegotiateCallerNameRequest
internal struct NegotiateCallerNameRequest
{
public int messageType;
public global::Interop.LUID? logonId;
public global::Interop.LUID logonId;
}

/*typedef struct _NEGOTIATE_CALLER_NAME_RESPONSE {
Expand All @@ -278,7 +278,7 @@ internal sealed class NegotiateCallerNameResponse
public string? callerName;
}

internal sealed class NativeMethods
internal sealed partial class NativeMethods
{
// disable public constructor
private NativeMethods() { }
Expand Down Expand Up @@ -309,14 +309,14 @@ private NativeMethods() { }
ULONG Flags,
PDOMAIN_CONTROLLER_INFO* DomainControllerInfo
);*/
[DllImport(global::Interop.Libraries.Netapi32, CallingConvention = CallingConvention.StdCall, EntryPoint = "DsGetDcNameW", CharSet = CharSet.Unicode)]
internal static extern int DsGetDcName(
[In] string? computerName,
[In] string? domainName,
[In] IntPtr domainGuid,
[In] string? siteName,
[In] int flags,
[Out] out IntPtr domainControllerInfo);
[GeneratedDllImport(global::Interop.Libraries.Netapi32, EntryPoint = "DsGetDcNameW", CharSet = CharSet.Unicode)]
internal static partial int DsGetDcName(
string? computerName,
string? domainName,
IntPtr domainGuid,
string? siteName,
int flags,
out IntPtr domainControllerInfo);

/* DWORD WINAPI DsGetDcOpen(
LPCTSTR DnsName,
Expand All @@ -327,42 +327,42 @@ internal static extern int DsGetDcName(
ULONG DcFlags,
PHANDLE RetGetDcContext
);*/
[DllImport(global::Interop.Libraries.Netapi32, CallingConvention = CallingConvention.StdCall, EntryPoint = "DsGetDcOpenW", CharSet = CharSet.Unicode)]
internal static extern int DsGetDcOpen(
[In] string? dnsName,
[In] int optionFlags,
[In] string? siteName,
[In] IntPtr domainGuid,
[In] string? dnsForestName,
[In] int dcFlags,
[Out] out IntPtr retGetDcContext);
[GeneratedDllImport(global::Interop.Libraries.Netapi32, EntryPoint = "DsGetDcOpenW", CharSet = CharSet.Unicode)]
internal static partial int DsGetDcOpen(
string? dnsName,
int optionFlags,
string? siteName,
IntPtr domainGuid,
string? dnsForestName,
int dcFlags,
out IntPtr retGetDcContext);

/*DWORD WINAPI DsGetDcNext(
HANDLE GetDcContextHandle,
PULONG SockAddressCount,
LPSOCKET_ADDRESS* SockAddresses,
LPTSTR* DnsHostName
);*/
[DllImport(global::Interop.Libraries.Netapi32, CallingConvention = CallingConvention.StdCall, EntryPoint = "DsGetDcNextW", CharSet = CharSet.Unicode)]
internal static extern int DsGetDcNext(
[In] IntPtr getDcContextHandle,
[In, Out] ref IntPtr sockAddressCount,
[Out] out IntPtr sockAdresses,
[Out] out IntPtr dnsHostName);
[GeneratedDllImport(global::Interop.Libraries.Netapi32, EntryPoint = "DsGetDcNextW", CharSet = CharSet.Unicode)]
internal static partial int DsGetDcNext(
IntPtr getDcContextHandle,
ref IntPtr sockAddressCount,
out IntPtr sockAdresses,
out IntPtr dnsHostName);

/*void WINAPI DsGetDcClose(
HANDLE GetDcContextHandle
);*/
[DllImport(global::Interop.Libraries.Netapi32, CallingConvention = CallingConvention.StdCall, EntryPoint = "DsGetDcCloseW", CharSet = CharSet.Unicode)]
internal static extern void DsGetDcClose(
[In] IntPtr getDcContextHandle);
[GeneratedDllImport(global::Interop.Libraries.Netapi32, EntryPoint = "DsGetDcCloseW", CharSet = CharSet.Unicode)]
internal static partial void DsGetDcClose(
IntPtr getDcContextHandle);

/*NET_API_STATUS NetApiBufferFree(
LPVOID Buffer
);*/
[DllImport(global::Interop.Libraries.Netapi32)]
internal static extern int NetApiBufferFree(
[In] IntPtr buffer);
[GeneratedDllImport(global::Interop.Libraries.Netapi32)]
internal static partial int NetApiBufferFree(
IntPtr buffer);

/*DWORD DsMakePasswordCredentials(
LPTSTR User,
Expand Down Expand Up @@ -457,23 +457,23 @@ internal delegate int DsListRoles(
PDNS_RECORD *ppQueryResultsSet,
PVOID *pReserved
);*/
[DllImport(global::Interop.Libraries.Dnsapi, EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode)]
internal static extern int DnsQuery(
[In] string recordName,
[In] short recordType,
[In] int options,
[In] IntPtr servers,
[Out] out IntPtr dnsResultList,
[Out] IntPtr reserved);
[GeneratedDllImport(global::Interop.Libraries.Dnsapi, EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode)]
internal static partial int DnsQuery(
string recordName,
short recordType,
int options,
IntPtr servers,
out IntPtr dnsResultList,
IntPtr reserved);

/*VOID WINAPI DnsRecordListFree(
PDNS_RECORD pRecordList,
DNS_FREE_TYPE FreeType
);*/
[DllImport(global::Interop.Libraries.Dnsapi, CharSet = CharSet.Unicode)]
internal static extern void DnsRecordListFree(
[In] IntPtr dnsResultList,
[In] bool dnsFreeType);
[GeneratedDllImport(global::Interop.Libraries.Dnsapi, CharSet = CharSet.Unicode)]
internal static partial void DnsRecordListFree(
IntPtr dnsResultList,
bool dnsFreeType);

/*DWORD DsCrackNames(
HANDLE hDS,
Expand All @@ -496,9 +496,9 @@ internal delegate int DsCrackNames(
/*NTSTATUS LsaConnectUntrusted(
PHANDLE LsaHandle
);*/
[DllImport(global::Interop.Libraries.Secur32)]
internal static extern uint LsaConnectUntrusted(
[Out] out LsaLogonProcessSafeHandle lsaHandle);
[GeneratedDllImport(global::Interop.Libraries.Secur32)]
internal static partial uint LsaConnectUntrusted(
out LsaLogonProcessSafeHandle lsaHandle);

internal const int NegGetCallerName = 1;

Expand All @@ -511,29 +511,29 @@ internal static extern uint LsaConnectUntrusted(
PULONG ReturnBufferLength,
PNTSTATUS ProtocolStatus
);*/
[DllImport(global::Interop.Libraries.Secur32)]
internal static extern uint LsaCallAuthenticationPackage(
[In] LsaLogonProcessSafeHandle lsaHandle,
[In] int authenticationPackage,
[In] NegotiateCallerNameRequest protocolSubmitBuffer,
[In] int submitBufferLength,
[Out] out IntPtr protocolReturnBuffer,
[Out] out int returnBufferLength,
[Out] out uint protocolStatus);
[GeneratedDllImport(global::Interop.Libraries.Secur32)]
internal static partial uint LsaCallAuthenticationPackage(
LsaLogonProcessSafeHandle lsaHandle,
int authenticationPackage,
in NegotiateCallerNameRequest protocolSubmitBuffer,
int submitBufferLength,
out IntPtr protocolReturnBuffer,
out int returnBufferLength,
out uint protocolStatus);

/*NTSTATUS LsaFreeReturnBuffer(
PVOID Buffer
);*/
[DllImport(global::Interop.Libraries.Secur32)]
internal static extern uint LsaFreeReturnBuffer(
[In] IntPtr buffer);
[GeneratedDllImport(global::Interop.Libraries.Secur32)]
internal static partial uint LsaFreeReturnBuffer(
IntPtr buffer);

/*NTSTATUS LsaDeregisterLogonProcess(
HANDLE LsaHandle
);*/
[DllImport(global::Interop.Libraries.Secur32)]
internal static extern int LsaDeregisterLogonProcess(
[In] IntPtr lsaHandle);
[GeneratedDllImport(global::Interop.Libraries.Secur32)]
internal static partial int LsaDeregisterLogonProcess(
IntPtr lsaHandle);

/*int CompareString(LCID Locale,
DWORD dwCmpFlags,
Expand All @@ -542,14 +542,14 @@ internal static extern int LsaDeregisterLogonProcess(
DWORD lpString2,
DWORD cchCount2
);*/
[DllImport(global::Interop.Libraries.Kernel32, EntryPoint = "CompareStringW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int CompareString(
[In] uint locale,
[In] uint dwCmpFlags,
[In] IntPtr lpString1,
[In] int cchCount1,
[In] IntPtr lpString2,
[In] int cchCount2);
[GeneratedDllImport(global::Interop.Libraries.Kernel32, EntryPoint = "CompareStringW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial int CompareString(
uint locale,
uint dwCmpFlags,
IntPtr lpString1,
int cchCount1,
IntPtr lpString2,
int cchCount2);
}

internal sealed class NativeComInterfaces
Expand Down
Loading

0 comments on commit 8b3815e

Please sign in to comment.