Replace DllImport with LibraryImport in SMA 1 (#18520)

This commit is contained in:
Ilya
2022-11-12 16:48:27 +05:00
committed by GitHub
parent 9f459c05f6
commit b84652745c
3 changed files with 54 additions and 47 deletions
@@ -30,7 +30,7 @@ namespace System.Management.Automation
{
/// <summary>
/// </summary>
public static class CompletionCompleters
public static partial class CompletionCompleters
{
static CompletionCompleters()
{
@@ -4751,8 +4751,8 @@ namespace System.Management.Automation
AttributesToSkip = 0 // Default is to skip Hidden and System files, so we clear this to retain existing behavior
};
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
private static extern int NetShareEnum(string serverName, int level, out IntPtr bufptr, int prefMaxLen,
[LibraryImport("Netapi32.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial int NetShareEnum(string serverName, int level, out IntPtr bufptr, int prefMaxLen,
out uint entriesRead, out uint totalEntries, ref uint resumeHandle);
internal static List<string> GetFileShares(string machine, bool ignoreHidden)
@@ -7150,7 +7150,7 @@ namespace Microsoft.PowerShell.Commands
return NativeMethods.PathIsNetworkPath(path); // call the native method
}
private static class NativeMethods
private static partial class NativeMethods
{
/// <summary>
/// WNetAddConnection2 API makes a connection to a network resource
@@ -7196,8 +7196,8 @@ namespace Microsoft.PowerShell.Commands
/// else the error code describing the type of failure that occurred while
/// trying to remove the connection is returned.
/// </returns>
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
internal static extern int WNetCancelConnection2(string driveName, int flags, bool force);
[LibraryImport("mpr.dll", EntryPoint ="WNetCancelConnection2W", StringMarshalling = StringMarshalling.Utf16)]
internal static partial int WNetCancelConnection2(string driveName, int flags, [MarshalAs(UnmanagedType.Bool)] bool force);
/// <summary>
/// WNetGetConnection function retrieves the name of the network resource associated with a local device.
@@ -7223,8 +7223,8 @@ namespace Microsoft.PowerShell.Commands
/// Path of the file being executed
/// </param>
/// <returns>Returns 0 through 25 (corresponding to 'A' through 'Z') if the path has a drive letter, or -1 otherwise.</returns>
[DllImport("api-ms-win-core-shlwapi-legacy-l1-1-0.dll", CharSet = CharSet.Unicode)]
internal static extern int PathGetDriveNumber(string path);
[LibraryImport("api-ms-win-core-shlwapi-legacy-l1-1-0.dll", EntryPoint ="PathGetDriveNumberW", StringMarshalling = StringMarshalling.Utf16)]
internal static partial int PathGetDriveNumber(string path);
private static bool _WNetApiAvailable = true;
@@ -7288,9 +7288,9 @@ namespace Microsoft.PowerShell.Commands
/// Path of the file being executed.
/// </param>
/// <returns>True if the path is a network path or else returns false.</returns>
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
[LibraryImport("shlwapi.dll", EntryPoint = "PathIsNetworkPathW", StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PathIsNetworkPath(string path);
internal static partial bool PathIsNetworkPath(string path);
#endif
/// <summary>
@@ -7320,9 +7320,9 @@ namespace Microsoft.PowerShell.Commands
/// <param name="destination">Path of the target of the symbolic link.</param>
/// <param name="symbolicLinkFlags">Flag values from SymbolicLinkFlags enum.</param>
/// <returns>1 on successful creation.</returns>
[DllImport(PinvokeDllNames.CreateSymbolicLinkDllName, CharSet = CharSet.Unicode, SetLastError = true)]
[LibraryImport(PinvokeDllNames.CreateSymbolicLinkDllName, EntryPoint = "CreateSymbolicLinkW", StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.I1)]
internal static extern bool CreateSymbolicLink(string name, string destination, SymbolicLinkFlags symbolicLinkFlags);
internal static partial bool CreateSymbolicLink(string name, string destination, SymbolicLinkFlags symbolicLinkFlags);
/// <summary>
/// Flags used when creating a symbolic link.
@@ -7353,9 +7353,9 @@ namespace Microsoft.PowerShell.Commands
/// <param name="existingFileName">Path to the target of the hard link.</param>
/// <param name="SecurityAttributes"></param>
/// <returns></returns>
[DllImport(PinvokeDllNames.CreateHardLinkDllName, CharSet = CharSet.Unicode, SetLastError = true)]
[LibraryImport(PinvokeDllNames.CreateHardLinkDllName, EntryPoint = "CreateHardLinkW", StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CreateHardLink(string name, string existingFileName, IntPtr SecurityAttributes);
internal static partial bool CreateHardLink(string name, string existingFileName, IntPtr SecurityAttributes);
// OneDrive placeholder support
#if !UNIX
@@ -7363,16 +7363,16 @@ namespace Microsoft.PowerShell.Commands
/// Returns the placeholder compatibility mode for the current process.
/// </summary>
/// <returns>The process's placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx).</returns>
[DllImport("ntdll.dll")]
internal static extern sbyte RtlQueryProcessPlaceholderCompatibilityMode();
[LibraryImport("ntdll.dll")]
internal static partial sbyte RtlQueryProcessPlaceholderCompatibilityMode();
/// <summary>
/// Sets the placeholder compatibility mode for the current process.
/// </summary>
/// <param name="pcm">The placeholder compatibility mode to set.</param>
/// <returns>The process's previous placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx).</returns>
[DllImport("ntdll.dll")]
internal static extern sbyte RtlSetProcessPlaceholderCompatibilityMode(sbyte pcm);
[LibraryImport("ntdll.dll")]
internal static partial sbyte RtlSetProcessPlaceholderCompatibilityMode(sbyte pcm);
internal const sbyte PHCM_APPLICATION_DEFAULT = 0;
internal const sbyte PHCM_DISGUISE_PLACEHOLDER = 1;
@@ -7870,7 +7870,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Class to find the symbolic link target.
/// </summary>
public static class InternalSymbolicLinkLinkCodeMethods
public static partial class InternalSymbolicLinkLinkCodeMethods
{
// This size comes from measuring the size of the header of REPARSE_GUID_DATA_BUFFER
private const int REPARSE_GUID_DATA_BUFFER_HEADER_SIZE = 24;
@@ -7984,9 +7984,9 @@ namespace Microsoft.PowerShell.Commands
private struct BY_HANDLE_FILE_INFORMATION
{
public uint FileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
public FILE_TIME CreationTime;
public FILE_TIME LastAccessTime;
public FILE_TIME LastWriteTime;
public uint VolumeSerialNumber;
public uint FileSizeHigh;
public uint FileSizeLow;
@@ -7995,6 +7995,12 @@ namespace Microsoft.PowerShell.Commands
public uint FileIndexLow;
}
internal struct FILE_TIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}
[StructLayout(LayoutKind.Sequential)]
private struct GUID
{
@@ -8018,20 +8024,21 @@ namespace Microsoft.PowerShell.Commands
public char[] DataBuffer;
}
[DllImport(PinvokeDllNames.DeviceIoControlDllName, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
[LibraryImport(PinvokeDllNames.DeviceIoControlDllName, StringMarshalling = StringMarshalling.Utf16, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
IntPtr InBuffer, int nInBufferSize,
IntPtr OutBuffer, int nOutBufferSize,
out int pBytesReturned, IntPtr lpOverlapped);
[DllImport(PinvokeDllNames.GetFileInformationByHandleDllName, SetLastError = true, CharSet = CharSet.Unicode)]
[LibraryImport(PinvokeDllNames.GetFileInformationByHandleDllName)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetFileInformationByHandle(
private static partial bool GetFileInformationByHandle(
IntPtr hFile,
out BY_HANDLE_FILE_INFORMATION lpFileInformation);
[DllImport(PinvokeDllNames.CreateFileDllName, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr CreateFile(
[LibraryImport(PinvokeDllNames.CreateFileDllName, EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
internal static partial IntPtr CreateFile(
string lpFileName,
FileDesiredAccess dwDesiredAccess,
FileShareMode dwShareMode,
@@ -8040,7 +8047,7 @@ namespace Microsoft.PowerShell.Commands
FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
internal sealed partial class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeFindHandle() : base(true) { }
@@ -8049,23 +8056,23 @@ namespace Microsoft.PowerShell.Commands
return FindClose(this.handle);
}
[DllImport(PinvokeDllNames.FindCloseDllName)]
[LibraryImport(PinvokeDllNames.FindCloseDllName)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FindClose(IntPtr handle);
private static partial bool FindClose(IntPtr handle);
}
// We use 'FindFirstFileW' instead of 'FindFirstFileExW' because the latter doesn't work correctly with Unicode file names on FAT32.
// See https://github.com/PowerShell/PowerShell/issues/16804
[DllImport(PinvokeDllNames.FindFirstFileDllName, EntryPoint = "FindFirstFileW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern SafeFindHandle FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);
[LibraryImport(PinvokeDllNames.FindFirstFileDllName, EntryPoint = "FindFirstFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial SafeFindHandle FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal unsafe struct WIN32_FIND_DATA
{
internal uint dwFileAttributes;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
internal System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
internal FILE_TIME ftCreationTime;
internal FILE_TIME ftLastAccessTime;
internal FILE_TIME ftLastWriteTime;
internal uint nFileSizeHigh;
internal uint nFileSizeLow;
internal uint dwReserved0;
@@ -8539,7 +8546,7 @@ namespace System.Management.Automation.Internal
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes",
Justification = "Needed by both the FileSystem provider and Unblock-File cmdlet.")]
public static class AlternateDataStreamUtilities
public static partial class AlternateDataStreamUtilities
{
/// <summary>
/// List all of the streams on a file.
@@ -8704,15 +8711,15 @@ namespace System.Management.Automation.Internal
// the code above seems cleaner and more robust than the IAttachmentExecute approach
}
internal static class NativeMethods
internal static partial class NativeMethods
{
internal const int ERROR_HANDLE_EOF = 38;
internal const int ERROR_INVALID_PARAMETER = 87;
internal enum StreamInfoLevels { FindStreamInfoStandard = 0 }
[DllImport(PinvokeDllNames.CreateFileDllName, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeFileHandle CreateFile(string lpFileName,
[LibraryImport(PinvokeDllNames.CreateFileDllName, EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
internal static partial SafeFileHandle CreateFile(string lpFileName,
FileAccess dwDesiredAccess, FileShare dwShareMode,
IntPtr lpSecurityAttributes, FileMode dwCreationDisposition,
int dwFlagsAndAttributes, IntPtr hTemplateFile);
@@ -8733,7 +8740,7 @@ namespace System.Management.Automation.Internal
AlternateStreamNativeData lpFindStreamData);
}
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
internal sealed partial class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeFindHandle() : base(true) { }
@@ -8742,9 +8749,9 @@ namespace System.Management.Automation.Internal
return FindClose(this.handle);
}
[DllImport(PinvokeDllNames.FindCloseDllName)]
[LibraryImport(PinvokeDllNames.FindCloseDllName)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FindClose(IntPtr handle);
private static partial bool FindClose(IntPtr handle);
}
/// <summary>
@@ -19,7 +19,7 @@ namespace System.Management.Automation
/// ClrFacade contains all diverging code (different implementation for FullCLR and CoreCLR using if/def).
/// It exposes common APIs that can be used by the rest of the code base.
/// </summary>
internal static class ClrFacade
internal static partial class ClrFacade
{
/// <summary>
/// Initialize powershell AssemblyLoadContext and register the 'Resolving' event, if it's not done already.
@@ -366,13 +366,13 @@ namespace System.Management.Automation
/// <summary>
/// Native methods that are used by facade methods.
/// </summary>
private static class NativeMethods
private static partial class NativeMethods
{
/// <summary>
/// Pinvoke for GetOEMCP to get the OEM code page.
/// </summary>
[DllImport(PinvokeDllNames.GetOEMCPDllName, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern uint GetOEMCP();
[LibraryImport(PinvokeDllNames.GetOEMCPDllName)]
internal static partial uint GetOEMCP();
}
}
}