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

Use GeneratedDllImport in System.DirectoryServices #61975

Merged
merged 6 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static partial class Interop
internal static partial class Activeds
{
[DllImport(Interop.Libraries.Activeds, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int ADsOpenObject(string path, string userName, string password, int flags, [In, Out] ref Guid iid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppObject);
internal static extern int ADsOpenObject(string path, string? userName, string? password, int flags, [In, Out] ref Guid iid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static partial class Interop
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "ConvertSidToStringSidW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial BOOL ConvertSidToStringSid(IntPtr sid, ref string stringSid);
internal static partial BOOL ConvertSidToStringSid(IntPtr sid, out string stringSid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal static partial class Advapi32
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "LogonUserW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial int LogonUser(
string username,
string domain,
string password,
string? domain,
string? password,
int logonType,
int logonProvider,
ref IntPtr token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static class StatusOptions
internal const uint STATUS_NO_MEMORY = 0xC0000017;
internal const uint STATUS_ACCESS_DENIED = 0xC0000022;
internal const uint STATUS_OBJECT_NAME_NOT_FOUND = 0xC0000034;
internal const uint STATUS_QUOTA_EXCEEDED = 0xC0000044;
internal const uint STATUS_ACCOUNT_RESTRICTION = 0xC000006E;
internal const uint STATUS_NONE_MAPPED = 0xC0000073;
internal const uint STATUS_INSUFFICIENT_RESOURCES = 0xC000009A;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.Win32.SafeHandles
{
internal sealed class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public SafeLibraryHandle() : base(true) { }

internal SafeLibraryHandle(IntPtr value) : base(true)
{
SetHandle(value);
}

protected override bool ReleaseHandle()
{
return Interop.Kernel32.FreeLibrary(handle);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ internal static byte[] StringToByteArray(string s)

internal static string ConvertSidToSDDL(byte[] sid)
{
string sddlSid = null;

// To put the byte[] SID into SDDL, we use ConvertSidToStringSid.
// Calling that requires we first copy the SID into native memory.
IntPtr pSid = IntPtr.Zero;
Expand All @@ -125,7 +123,7 @@ internal static string ConvertSidToSDDL(byte[] sid)
{
pSid = ConvertByteArrayToIntPtr(sid);

if (Interop.Advapi32.ConvertSidToStringSid(pSid, ref sddlSid) != Interop.BOOL.FALSE)
if (Interop.Advapi32.ConvertSidToStringSid(pSid, out string sddlSid) != Interop.BOOL.FALSE)
{
return sddlSid;
}
Expand Down Expand Up @@ -502,12 +500,12 @@ internal static IntPtr GetMachineDomainSid()
UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO info = (UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO)
Marshal.PtrToStructure(pBuffer, typeof(UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO));

Debug.Assert(Interop.Advapi32.IsValidSid(info.domainSid));
Debug.Assert(Interop.Advapi32.IsValidSid(info.DomainSid));

// Now we make a copy of the SID to return
int sidLength = Interop.Advapi32.GetLengthSid(info.domainSid);
int sidLength = Interop.Advapi32.GetLengthSid(info.DomainSid);
IntPtr pCopyOfSid = Marshal.AllocHGlobal(sidLength);
bool success = Interop.Advapi32.CopySid(sidLength, pCopyOfSid, info.domainSid);
bool success = Interop.Advapi32.CopySid(sidLength, pCopyOfSid, info.DomainSid);
if (!success)
{
int lastError = Marshal.GetLastWin32Error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ public sealed class WKSTA_INFO_100
public int wki100_ver_minor;
};

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public sealed class POLICY_ACCOUNT_DOMAIN_INFO
[StructLayout(LayoutKind.Sequential)]
public struct POLICY_ACCOUNT_DOMAIN_INFO
{
public Interop.UNICODE_INTPTR_STRING domainName;
public IntPtr domainSid = IntPtr.Zero;
public Interop.UNICODE_INTPTR_STRING DomainName;
public IntPtr DomainSid;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@

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

[DllImport(global::Interop.Libraries.OleAut32)]
public static extern 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 @@ -25,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 Expand Up @@ -99,7 +96,7 @@ private void Advance()
}
finally
{
VariantClear(addr);
global::Interop.OleAut32.VariantClear(addr);
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ internal struct Variant

internal static class UnsafeNativeMethods
{
[DllImport(global::Interop.Libraries.Activeds, ExactSpelling = true, EntryPoint = "ADsOpenObject", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern int IntADsOpenObject(string path, string? userName, string? password, int flags, [In, Out] ref Guid iid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppObject);

public static int ADsOpenObject(string path, string? userName, string? password, int flags, [In, Out] ref Guid iid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppObject)
{
try
{
return IntADsOpenObject(path, userName, password, flags, ref iid, out ppObject);
return global::Interop.Activeds.ADsOpenObject(path, userName, password, flags, ref iid, out ppObject);
}
catch (EntryPointNotFoundException)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<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 @@ -149,9 +153,88 @@ System.DirectoryServices.ActiveDirectory.DomainController</PackageDescription>
<Compile Include="Interop\NativeMethods.cs" />
<Compile Include="Interop\SafeNativeMethods.cs" />
<Compile Include="Interop\UnsafeNativeMethods.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs"
Link="Common\Interop\Windows\Interop.BOOL.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOLEAN.cs"
Link="Common\Interop\Windows\Interop.BOOLEAN.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs" Link="System\Obsoletions.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs"
Link="Common\Interop\Windows\Interop.OBJECT_ATTRIBUTES.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.SECURITY_QUALITY_OF_SERVICE.cs"
Link="Common\Interop\Windows\Interop.SECURITY_QUALITY_OF_SERVICE.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.UNICODE_STRING.cs"
Link="Common\Interop\Windows\Interop.UNICODE_STRING.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Activeds\Interop.ADsOpenObject.cs"
Link="Common\Interop\Windows\Activeds\Interop.ADsOpenObject.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.ConvertSidToStringSid.cs"
Link="Common\Interop\Windows\Advapi32\Interop.ConvertSidToStringSid.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.ConvertStringSidToSid.cs"
Link="Common\Interop\Windows\Advapi32\Interop.ConvertStringSidToSid.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.CopySid.cs"
Link="Common\Interop\Windows\Advapi32\Interop.CopySid.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.EqualDomainSid.cs"
Link="Common\Interop\Windows\Advapi32\Interop.EqualDomainSid.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.GetLengthSid.cs"
Link="Common\Interop\Windows\Advapi32\Interop.GetLengthSid.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.GetSidIdentifierAuthority.cs"
Link="Common\Interop\Windows\Advapi32\Interop.GetSidIdentifierAuthority.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.GetTokenInformation.cs"
Link="Common\Interop\Windows\Advapi32\Interop.GetTokenInformation.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.ImpersonateLoggedOnUser_IntPtr.cs"
Link="Common\Interop\Windows\Advapi32\Interop.ImpersonateLoggedOnUser_IntPtr.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.IsValidSid.cs"
Link="Common\Interop\Windows\Advapi32\Interop.IsValidSid.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.LogonUser.cs"
Link="Common\Interop\Windows\Advapi32\Interop.LogonUser.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.LsaClose.cs"
Link="Common\Interop\Windows\Advapi32\Interop.LsaClose.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.LsaFreeMemory.cs"
Link="Common\Interop\Windows\Advapi32\Interop.LsaFreeMemory.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.LsaNtStatusToWinError.cs"
Link="Common\Interop\Windows\Advapi32\Interop.LsaNtStatusToWinError.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.LsaOpenPolicy.cs"
Link="Common\Interop\Windows\Advapi32\Interop.LsaOpenPolicy.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.LsaQueryInformationPolicy.cs"
Link="Common\Interop\Windows\Advapi32\Interop.LsaQueryInformationPolicy.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.OpenThreadToken_SafeTokenHandle.cs"
Link="Common\Interop\Windows\Advapi32\Interop.OpenThreadToken_SafeTokenHandle.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.OpenProcessToken.cs"
Link="Common\Interop\Windows\Advapi32\Interop.OpenProcessToken.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.RevertToSelf.cs"
Link="Common\Interop\Windows\Advapi32\Interop.RevertToSelf.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.TOKEN_INFORMATION_CLASS.cs"
Link="Common\Interop\Windows\Advapi32\Interop.TOKEN_INFORMATION_CLASS.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.CloseHandle.cs"
Link="Common\Interop\Windows\Kernel32\Interop.CloseHandle.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.FreeLibrary.cs"
Link="Common\Interop\Windows\Kernel32\Interop.FreeLibrary.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentProcess.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetCurrentProcess.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentThread.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetCurrentThread.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentThreadId.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetCurrentThreadId.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetProcAddress.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetProcAddress.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.LoadLibrary.cs"
Link="Common\Interop\Windows\Kernel32\Interop.LoadLibrary.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.LocalAlloc.cs"
Link="Common\Interop\Windows\Kernel32\Interop.LocalAlloc.cs" />
<Compile Include="$(CommonPath)Interop\Windows\OleAut32\Interop.VariantClear.cs"
Link="Common\Interop\Windows\OleAut32\Interop.VariantClear.cs" />
<Compile Include="$(CommonPath)Interop\Windows\NtDll\Interop.NtStatus.cs"
Link="Common\Interop\Windows\NtDll\Interop.NtStatus.cs" />
<Compile Include="$(CommonPath)Interop\Windows\SspiCli\Interop.TOKENS.cs"
Link="Common\Interop\Windows\SspiCli\Interop.TOKENS.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeLsaPolicyHandle.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeLsaPolicyHandle.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeTokenHandle.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeTokenHandle.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="System\Obsoletions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Security.Permissions\src\System.Security.Permissions.csproj" />
Expand All @@ -167,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 @@ -1306,7 +1306,7 @@ private void GetDomains()

IntPtr info = (IntPtr)0;
// call DsReplicaSyncAllW
IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListDomainsInSiteW");
IntPtr functionPtr = global::Interop.Kernel32.GetProcAddress(DirectoryContext.ADHandle, "DsListDomainsInSiteW");
if (functionPtr == (IntPtr)0)
{
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
Expand Down Expand Up @@ -1348,7 +1348,7 @@ private void GetDomains()
finally
{
// call DsFreeNameResultW
functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
functionPtr = global::Interop.Kernel32.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
if (functionPtr == (IntPtr)0)
{
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
Expand Down
Loading