Skip to content

Commit 9f459c0

Browse files
authored
Replace DllImport with LibraryImport in engine (PowerShell#18496)
1 parent 1809328 commit 9f459c0

File tree

3 files changed

+58
-61
lines changed

3 files changed

+58
-61
lines changed

src/System.Management.Automation/CoreCLR/CorePsPlatform.cs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace System.Management.Automation
1313
/// <summary>
1414
/// These are platform abstractions and platform specific implementations.
1515
/// </summary>
16-
public static class Platform
16+
public static partial class Platform
1717
{
1818
/// <summary>
1919
/// True if the current platform is Linux.
@@ -453,18 +453,18 @@ internal static int NonWindowsWaitPid(int pid, bool nohang)
453453
return Unix.NativeMethods.WaitPid(pid, nohang);
454454
}
455455

456-
internal static class Windows
456+
internal static partial class Windows
457457
{
458458
/// <summary>The native methods class.</summary>
459-
internal static class NativeMethods
459+
internal static partial class NativeMethods
460460
{
461461
private const string ole32Lib = "api-ms-win-core-com-l1-1-0.dll";
462462

463-
[DllImport(ole32Lib)]
464-
internal static extern int CoInitializeEx(IntPtr reserve, int coinit);
463+
[LibraryImport(ole32Lib)]
464+
internal static partial int CoInitializeEx(IntPtr reserve, int coinit);
465465

466-
[DllImport(ole32Lib)]
467-
internal static extern void CoUninitialize();
466+
[LibraryImport(ole32Lib)]
467+
internal static partial void CoUninitialize();
468468
}
469469
}
470470

@@ -475,7 +475,7 @@ internal static class NativeMethods
475475
// to a PAL value and calls strerror_r underneath to generate the message.
476476

477477
/// <summary>Unix specific implementations of required functionality.</summary>
478-
internal static class Unix
478+
internal static partial class Unix
479479
{
480480
private static readonly Dictionary<int, string> usernameCache = new();
481481
private static readonly Dictionary<int, string> groupnameCache = new();
@@ -925,35 +925,35 @@ public static int GetProcFSParentPid(int pid)
925925
}
926926

927927
/// <summary>The native methods class.</summary>
928-
internal static class NativeMethods
928+
internal static partial class NativeMethods
929929
{
930930
private const string psLib = "libpsl-native";
931931

932932
// Ansi is a misnomer, it is hardcoded to UTF-8 on Linux and macOS
933933
// C bools are 1 byte and so must be marshaled as I1
934934

935-
[DllImport(psLib, CharSet = CharSet.Ansi)]
936-
internal static extern int GetErrorCategory(int errno);
935+
[LibraryImport(psLib)]
936+
internal static partial int GetErrorCategory(int errno);
937937

938-
[DllImport(psLib)]
939-
internal static extern int GetPPid(int pid);
938+
[LibraryImport(psLib)]
939+
internal static partial int GetPPid(int pid);
940940

941-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
942-
internal static extern int GetLinkCount([MarshalAs(UnmanagedType.LPStr)] string filePath, out int linkCount);
941+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8, SetLastError = true)]
942+
internal static partial int GetLinkCount(string filePath, out int linkCount);
943943

944-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
944+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
945945
[return: MarshalAs(UnmanagedType.I1)]
946-
internal static extern bool IsExecutable([MarshalAs(UnmanagedType.LPStr)] string filePath);
946+
internal static partial bool IsExecutable(string filePath);
947947

948-
[DllImport(psLib, CharSet = CharSet.Ansi)]
949-
internal static extern uint GetCurrentThreadId();
948+
[LibraryImport(psLib)]
949+
internal static partial uint GetCurrentThreadId();
950950

951-
[DllImport(psLib)]
951+
[LibraryImport(psLib)]
952952
[return: MarshalAs(UnmanagedType.Bool)]
953-
internal static extern bool KillProcess(int pid);
953+
internal static partial bool KillProcess(int pid);
954954

955-
[DllImport(psLib)]
956-
internal static extern int WaitPid(int pid, bool nohang);
955+
[LibraryImport(psLib)]
956+
internal static partial int WaitPid(int pid, [MarshalAs(UnmanagedType.Bool)] bool nohang);
957957

958958
// This is a struct tm from <time.h>.
959959
[StructLayout(LayoutKind.Sequential)]
@@ -1003,29 +1003,25 @@ internal static UnixTm DateTimeToUnixTm(DateTime date)
10031003
return tm;
10041004
}
10051005

1006-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1007-
internal static extern unsafe int SetDate(UnixTm* tm);
1006+
[LibraryImport(psLib)]
1007+
internal static unsafe partial int SetDate(UnixTm* tm);
10081008

1009-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1010-
internal static extern int CreateSymLink([MarshalAs(UnmanagedType.LPStr)] string filePath,
1011-
[MarshalAs(UnmanagedType.LPStr)] string target);
1009+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
1010+
internal static partial int CreateSymLink(string filePath, string target);
10121011

1013-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1014-
internal static extern int CreateHardLink([MarshalAs(UnmanagedType.LPStr)] string filePath,
1015-
[MarshalAs(UnmanagedType.LPStr)] string target);
1012+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
1013+
internal static partial int CreateHardLink(string filePath, string target);
10161014

1017-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1015+
[LibraryImport(psLib)]
10181016
[return: MarshalAs(UnmanagedType.LPStr)]
1019-
internal static extern string GetUserFromPid(int pid);
1017+
internal static partial string GetUserFromPid(int pid);
10201018

1021-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1019+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
10221020
[return: MarshalAs(UnmanagedType.I1)]
1023-
internal static extern bool IsSameFileSystemItem([MarshalAs(UnmanagedType.LPStr)] string filePathOne,
1024-
[MarshalAs(UnmanagedType.LPStr)] string filePathTwo);
1021+
internal static partial bool IsSameFileSystemItem(string filePathOne, string filePathTwo);
10251022

1026-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1027-
internal static extern int GetInodeData([MarshalAs(UnmanagedType.LPStr)] string path,
1028-
out ulong device, out ulong inode);
1023+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
1024+
internal static partial int GetInodeData(string path, out ulong device, out ulong inode);
10291025

10301026
/// <summary>
10311027
/// This is a struct from getcommonstat.h in the native library.
@@ -1103,17 +1099,17 @@ internal struct CommonStatStruct
11031099
internal int IsSticky;
11041100
}
11051101

1106-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1107-
internal static extern unsafe int GetCommonLStat(string filePath, [Out] out CommonStatStruct cs);
1102+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8, SetLastError = true)]
1103+
internal static unsafe partial int GetCommonLStat(string filePath, out CommonStatStruct cs);
11081104

1109-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1110-
internal static extern unsafe int GetCommonStat(string filePath, [Out] out CommonStatStruct cs);
1105+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8, SetLastError = true)]
1106+
internal static unsafe partial int GetCommonStat(string filePath, out CommonStatStruct cs);
11111107

1112-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1113-
internal static extern string GetPwUid(int id);
1108+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
1109+
internal static partial string GetPwUid(int id);
11141110

1115-
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
1116-
internal static extern string GetGrGid(int id);
1111+
[LibraryImport(psLib, StringMarshalling = StringMarshalling.Utf8)]
1112+
internal static partial string GetGrGid(int id);
11171113
}
11181114
}
11191115
}

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,15 +2154,15 @@ internal void Done()
21542154
/// Static class that allows you to show and hide the console window
21552155
/// associated with this process.
21562156
/// </summary>
2157-
internal static class ConsoleVisibility
2157+
internal static partial class ConsoleVisibility
21582158
{
21592159
/// <summary>
21602160
/// If set to true, then native commands will always be run redirected...
21612161
/// </summary>
21622162
public static bool AlwaysCaptureApplicationIO { get; set; }
21632163

2164-
[DllImport("Kernel32.dll")]
2165-
internal static extern IntPtr GetConsoleWindow();
2164+
[LibraryImport("Kernel32.dll")]
2165+
internal static partial IntPtr GetConsoleWindow();
21662166

21672167
internal const int SW_HIDE = 0;
21682168
internal const int SW_SHOWNORMAL = 1;
@@ -2186,32 +2186,33 @@ internal static class ConsoleVisibility
21862186
/// <param name="hWnd">The window to show...</param>
21872187
/// <param name="nCmdShow">The command to do.</param>
21882188
/// <returns>True if it was successful.</returns>
2189-
[DllImport("user32.dll")]
2190-
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
2189+
[LibraryImport("user32.dll")]
2190+
[return: MarshalAs(UnmanagedType.Bool)]
2191+
internal static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
21912192

21922193
/// <summary>
21932194
/// Code to allocate a console...
21942195
/// </summary>
21952196
/// <returns>True if a console was created...</returns>
2196-
[DllImport("kernel32.dll", SetLastError = true)]
2197+
[LibraryImport("kernel32.dll")]
21972198
[return: MarshalAs(UnmanagedType.Bool)]
2198-
internal static extern bool AllocConsole();
2199+
internal static partial bool AllocConsole();
21992200

22002201
/// <summary>
22012202
/// Called to save the foreground window before allocating a hidden console window.
22022203
/// </summary>
22032204
/// <returns>A handle to the foreground window.</returns>
2204-
[DllImport("user32.dll")]
2205-
private static extern IntPtr GetForegroundWindow();
2205+
[LibraryImport("user32.dll")]
2206+
private static partial IntPtr GetForegroundWindow();
22062207

22072208
/// <summary>
22082209
/// Called to restore the foreground window after allocating a hidden console window.
22092210
/// </summary>
22102211
/// <param name="hWnd">A handle to the window that should be activated and brought to the foreground.</param>
22112212
/// <returns>True if the window was brought to the foreground.</returns>
2212-
[DllImport("user32.dll")]
2213+
[LibraryImport("user32.dll")]
22132214
[return: MarshalAs(UnmanagedType.Bool)]
2214-
private static extern bool SetForegroundWindow(IntPtr hWnd);
2215+
private static partial bool SetForegroundWindow(IntPtr hWnd);
22152216

22162217
/// <summary>
22172218
/// If no console window is attached to this process, then allocate one,

src/System.Management.Automation/engine/ProcessCodeMethods.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.PowerShell
1111
/// <summary>
1212
/// Helper functions for process info.
1313
/// </summary>
14-
public static class ProcessCodeMethods
14+
public static partial class ProcessCodeMethods
1515
{
1616
private const int InvalidProcessId = -1;
1717

@@ -79,8 +79,8 @@ private struct PROCESS_BASIC_INFORMATION
7979
public IntPtr InheritedFromUniqueProcessId;
8080
}
8181

82-
[DllImport("ntdll.dll", SetLastError = true)]
83-
private static extern int NtQueryInformationProcess(
82+
[LibraryImport("ntdll.dll")]
83+
private static partial int NtQueryInformationProcess(
8484
IntPtr processHandle,
8585
int processInformationClass,
8686
out PROCESS_BASIC_INFORMATION processInformation,

0 commit comments

Comments
 (0)