From 9f459c05f617fe530dac340aa546d198724d85da Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 9 Nov 2022 19:24:11 -0800 Subject: [PATCH] Replace DllImport with LibraryImport in engine (#18496) --- .../CoreCLR/CorePsPlatform.cs | 90 +++++++++---------- .../engine/NativeCommandProcessor.cs | 23 ++--- .../engine/ProcessCodeMethods.cs | 6 +- 3 files changed, 58 insertions(+), 61 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 28b02a6d70..8f379acc71 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -13,7 +13,7 @@ namespace System.Management.Automation /// /// These are platform abstractions and platform specific implementations. /// - public static class Platform + public static partial class Platform { /// /// True if the current platform is Linux. @@ -453,18 +453,18 @@ namespace System.Management.Automation return Unix.NativeMethods.WaitPid(pid, nohang); } - internal static class Windows + internal static partial class Windows { /// The native methods class. - internal static class NativeMethods + internal static partial class NativeMethods { private const string ole32Lib = "api-ms-win-core-com-l1-1-0.dll"; - [DllImport(ole32Lib)] - internal static extern int CoInitializeEx(IntPtr reserve, int coinit); + [LibraryImport(ole32Lib)] + internal static partial int CoInitializeEx(IntPtr reserve, int coinit); - [DllImport(ole32Lib)] - internal static extern void CoUninitialize(); + [LibraryImport(ole32Lib)] + internal static partial void CoUninitialize(); } } @@ -475,7 +475,7 @@ namespace System.Management.Automation // to a PAL value and calls strerror_r underneath to generate the message. /// Unix specific implementations of required functionality. - internal static class Unix + internal static partial class Unix { private static readonly Dictionary usernameCache = new(); private static readonly Dictionary groupnameCache = new(); @@ -925,35 +925,35 @@ namespace System.Management.Automation } /// The native methods class. - internal static class NativeMethods + internal static partial class NativeMethods { private const string psLib = "libpsl-native"; // Ansi is a misnomer, it is hardcoded to UTF-8 on Linux and macOS // C bools are 1 byte and so must be marshaled as I1 - [DllImport(psLib, CharSet = CharSet.Ansi)] - internal static extern int GetErrorCategory(int errno); + [LibraryImport(psLib)] + internal static partial int GetErrorCategory(int errno); - [DllImport(psLib)] - internal static extern int GetPPid(int pid); + [LibraryImport(psLib)] + internal static partial int GetPPid(int pid); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)] string filePath, out int linkCount); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8, SetLastError = true)] + internal static partial int GetLinkCount(string filePath, out int linkCount); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] [return: MarshalAs(UnmanagedType.I1)] - internal static extern bool IsExecutable([MarshalAs(UnmanagedType.LPStr)] string filePath); + internal static partial bool IsExecutable(string filePath); - [DllImport(psLib, CharSet = CharSet.Ansi)] - internal static extern uint GetCurrentThreadId(); + [LibraryImport(psLib)] + internal static partial uint GetCurrentThreadId(); - [DllImport(psLib)] + [LibraryImport(psLib)] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool KillProcess(int pid); + internal static partial bool KillProcess(int pid); - [DllImport(psLib)] - internal static extern int WaitPid(int pid, bool nohang); + [LibraryImport(psLib)] + internal static partial int WaitPid(int pid, [MarshalAs(UnmanagedType.Bool)] bool nohang); // This is a struct tm from . [StructLayout(LayoutKind.Sequential)] @@ -1003,29 +1003,25 @@ namespace System.Management.Automation return tm; } - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern unsafe int SetDate(UnixTm* tm); + [LibraryImport(psLib)] + internal static unsafe partial int SetDate(UnixTm* tm); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern int CreateSymLink([MarshalAs(UnmanagedType.LPStr)] string filePath, - [MarshalAs(UnmanagedType.LPStr)] string target); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] + internal static partial int CreateSymLink(string filePath, string target); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)] string filePath, - [MarshalAs(UnmanagedType.LPStr)] string target); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] + internal static partial int CreateHardLink(string filePath, string target); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] + [LibraryImport(psLib)] [return: MarshalAs(UnmanagedType.LPStr)] - internal static extern string GetUserFromPid(int pid); + internal static partial string GetUserFromPid(int pid); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] [return: MarshalAs(UnmanagedType.I1)] - internal static extern bool IsSameFileSystemItem([MarshalAs(UnmanagedType.LPStr)] string filePathOne, - [MarshalAs(UnmanagedType.LPStr)] string filePathTwo); + internal static partial bool IsSameFileSystemItem(string filePathOne, string filePathTwo); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern int GetInodeData([MarshalAs(UnmanagedType.LPStr)] string path, - out ulong device, out ulong inode); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] + internal static partial int GetInodeData(string path, out ulong device, out ulong inode); /// /// This is a struct from getcommonstat.h in the native library. @@ -1103,17 +1099,17 @@ namespace System.Management.Automation internal int IsSticky; } - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern unsafe int GetCommonLStat(string filePath, [Out] out CommonStatStruct cs); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8, SetLastError = true)] + internal static unsafe partial int GetCommonLStat(string filePath, out CommonStatStruct cs); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern unsafe int GetCommonStat(string filePath, [Out] out CommonStatStruct cs); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8, SetLastError = true)] + internal static unsafe partial int GetCommonStat(string filePath, out CommonStatStruct cs); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern string GetPwUid(int id); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] + internal static partial string GetPwUid(int id); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - internal static extern string GetGrGid(int id); + [LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)] + internal static partial string GetGrGid(int id); } } } diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index e9a1a13e23..06283ac2a3 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -2154,15 +2154,15 @@ namespace System.Management.Automation /// Static class that allows you to show and hide the console window /// associated with this process. /// - internal static class ConsoleVisibility + internal static partial class ConsoleVisibility { /// /// If set to true, then native commands will always be run redirected... /// public static bool AlwaysCaptureApplicationIO { get; set; } - [DllImport("Kernel32.dll")] - internal static extern IntPtr GetConsoleWindow(); + [LibraryImport("Kernel32.dll")] + internal static partial IntPtr GetConsoleWindow(); internal const int SW_HIDE = 0; internal const int SW_SHOWNORMAL = 1; @@ -2186,32 +2186,33 @@ namespace System.Management.Automation /// The window to show... /// The command to do. /// True if it was successful. - [DllImport("user32.dll")] - internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + [LibraryImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + internal static partial bool ShowWindow(IntPtr hWnd, int nCmdShow); /// /// Code to allocate a console... /// /// True if a console was created... - [DllImport("kernel32.dll", SetLastError = true)] + [LibraryImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool AllocConsole(); + internal static partial bool AllocConsole(); /// /// Called to save the foreground window before allocating a hidden console window. /// /// A handle to the foreground window. - [DllImport("user32.dll")] - private static extern IntPtr GetForegroundWindow(); + [LibraryImport("user32.dll")] + private static partial IntPtr GetForegroundWindow(); /// /// Called to restore the foreground window after allocating a hidden console window. /// /// A handle to the window that should be activated and brought to the foreground. /// True if the window was brought to the foreground. - [DllImport("user32.dll")] + [LibraryImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool SetForegroundWindow(IntPtr hWnd); + private static partial bool SetForegroundWindow(IntPtr hWnd); /// /// If no console window is attached to this process, then allocate one, diff --git a/src/System.Management.Automation/engine/ProcessCodeMethods.cs b/src/System.Management.Automation/engine/ProcessCodeMethods.cs index 604ac3787a..c54d992989 100644 --- a/src/System.Management.Automation/engine/ProcessCodeMethods.cs +++ b/src/System.Management.Automation/engine/ProcessCodeMethods.cs @@ -11,7 +11,7 @@ namespace Microsoft.PowerShell /// /// Helper functions for process info. /// - public static class ProcessCodeMethods + public static partial class ProcessCodeMethods { private const int InvalidProcessId = -1; @@ -79,8 +79,8 @@ namespace Microsoft.PowerShell public IntPtr InheritedFromUniqueProcessId; } - [DllImport("ntdll.dll", SetLastError = true)] - private static extern int NtQueryInformationProcess( + [LibraryImport("ntdll.dll")] + private static partial int NtQueryInformationProcess( IntPtr processHandle, int processInformationClass, out PROCESS_BASIC_INFORMATION processInformation,