Replace DllImport with LibraryImport in engine (#18496)

This commit is contained in:
Steve Lee
2022-11-10 08:24:11 +05:00
committed by GitHub
parent 18093283ed
commit 9f459c05f6
3 changed files with 58 additions and 61 deletions
@@ -13,7 +13,7 @@ namespace System.Management.Automation
/// <summary>
/// These are platform abstractions and platform specific implementations.
/// </summary>
public static class Platform
public static partial class Platform
{
/// <summary>
/// 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
{
/// <summary>The native methods class.</summary>
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.
/// <summary>Unix specific implementations of required functionality.</summary>
internal static class Unix
internal static partial class Unix
{
private static readonly Dictionary<int, string> usernameCache = new();
private static readonly Dictionary<int, string> groupnameCache = new();
@@ -925,35 +925,35 @@ namespace System.Management.Automation
}
/// <summary>The native methods class.</summary>
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 <time.h>.
[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);
/// <summary>
/// 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);
}
}
}
@@ -2154,15 +2154,15 @@ namespace System.Management.Automation
/// Static class that allows you to show and hide the console window
/// associated with this process.
/// </summary>
internal static class ConsoleVisibility
internal static partial class ConsoleVisibility
{
/// <summary>
/// If set to true, then native commands will always be run redirected...
/// </summary>
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
/// <param name="hWnd">The window to show...</param>
/// <param name="nCmdShow">The command to do.</param>
/// <returns>True if it was successful.</returns>
[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);
/// <summary>
/// Code to allocate a console...
/// </summary>
/// <returns>True if a console was created...</returns>
[DllImport("kernel32.dll", SetLastError = true)]
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool AllocConsole();
internal static partial bool AllocConsole();
/// <summary>
/// Called to save the foreground window before allocating a hidden console window.
/// </summary>
/// <returns>A handle to the foreground window.</returns>
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[LibraryImport("user32.dll")]
private static partial IntPtr GetForegroundWindow();
/// <summary>
/// Called to restore the foreground window after allocating a hidden console window.
/// </summary>
/// <param name="hWnd">A handle to the window that should be activated and brought to the foreground.</param>
/// <returns>True if the window was brought to the foreground.</returns>
[DllImport("user32.dll")]
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private static partial bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// If no console window is attached to this process, then allocate one,
@@ -11,7 +11,7 @@ namespace Microsoft.PowerShell
/// <summary>
/// Helper functions for process info.
/// </summary>
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,