Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 43 additions & 47 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -453,18 +453,18 @@ internal static int NonWindowsWaitPid(int pid, bool nohang)
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();
}
}

Expand All @@ -475,7 +475,7 @@ internal static class NativeMethods
// 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();
Expand Down Expand Up @@ -925,35 +925,35 @@ public static int GetProcFSParentPid(int pid)
}

/// <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);
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

[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)]
Expand Down Expand Up @@ -1003,29 +1003,25 @@ internal static UnixTm DateTimeToUnixTm(DateTime date)
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);
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

[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);
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

[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.
Expand Down Expand Up @@ -1103,17 +1099,17 @@ internal struct CommonStatStruct
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);
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

[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);
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions src/System.Management.Automation/engine/NativeCommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2154,15 +2154,15 @@ internal void Done()
/// 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;
Expand All @@ -2186,32 +2186,33 @@ internal static class ConsoleVisibility
/// <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();
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved

/// <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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -79,8 +79,8 @@ private struct PROCESS_BASIC_INFORMATION
public IntPtr InheritedFromUniqueProcessId;
}

[DllImport("ntdll.dll", SetLastError = true)]
private static extern int NtQueryInformationProcess(
[LibraryImport("ntdll.dll")]
private static partial int NtQueryInformationProcess(
Comment thread
SteveL-MSFT marked this conversation as resolved.
Resolved
IntPtr processHandle,
int processInformationClass,
out PROCESS_BASIC_INFORMATION processInformation,
Expand Down