From b84652745c83a4693f2ca5860daad5f557fb4e0d Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 12 Nov 2022 16:48:27 +0500 Subject: [PATCH] Replace DllImport with LibraryImport in SMA 1 (#18520) --- .../CommandCompletion/CompletionCompleters.cs | 6 +- .../namespaces/FileSystemProvider.cs | 87 ++++++++++--------- .../utils/ClrFacade.cs | 8 +- 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 3e9f16bc4b..60a2c31124 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -30,7 +30,7 @@ namespace System.Management.Automation { /// /// - 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 GetFileShares(string machine, bool ignoreHidden) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 79ed698f45..e528a9bb5a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -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 { /// /// 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. /// - [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); /// /// 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 /// /// Returns 0 through 25 (corresponding to 'A' through 'Z') if the path has a drive letter, or -1 otherwise. - [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. /// /// True if the path is a network path or else returns false. - [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 /// @@ -7320,9 +7320,9 @@ namespace Microsoft.PowerShell.Commands /// Path of the target of the symbolic link. /// Flag values from SymbolicLinkFlags enum. /// 1 on successful creation. - [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); /// /// Flags used when creating a symbolic link. @@ -7353,9 +7353,9 @@ namespace Microsoft.PowerShell.Commands /// Path to the target of the hard link. /// /// - [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. /// /// The process's placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx). - [DllImport("ntdll.dll")] - internal static extern sbyte RtlQueryProcessPlaceholderCompatibilityMode(); + [LibraryImport("ntdll.dll")] + internal static partial sbyte RtlQueryProcessPlaceholderCompatibilityMode(); /// /// Sets the placeholder compatibility mode for the current process. /// /// The placeholder compatibility mode to set. /// The process's previous placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx). - [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 /// /// Class to find the symbolic link target. /// - 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 /// [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 { /// /// 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); } /// diff --git a/src/System.Management.Automation/utils/ClrFacade.cs b/src/System.Management.Automation/utils/ClrFacade.cs index 2331eb15ce..0bccb456e6 100644 --- a/src/System.Management.Automation/utils/ClrFacade.cs +++ b/src/System.Management.Automation/utils/ClrFacade.cs @@ -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. /// - internal static class ClrFacade + internal static partial class ClrFacade { /// /// Initialize powershell AssemblyLoadContext and register the 'Resolving' event, if it's not done already. @@ -366,13 +366,13 @@ namespace System.Management.Automation /// /// Native methods that are used by facade methods. /// - private static class NativeMethods + private static partial class NativeMethods { /// /// Pinvoke for GetOEMCP to get the OEM code page. /// - [DllImport(PinvokeDllNames.GetOEMCPDllName, SetLastError = false, CharSet = CharSet.Unicode)] - internal static extern uint GetOEMCP(); + [LibraryImport(PinvokeDllNames.GetOEMCPDllName)] + internal static partial uint GetOEMCP(); } } }