mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Replace DllImport with LibraryImport - 1 (#18603)
This commit is contained in:
@@ -163,19 +163,16 @@ namespace System.Management.Automation
|
||||
|
||||
private static readonly Lazy<bool> _isStaSupported = new Lazy<bool>(() =>
|
||||
{
|
||||
// See objbase.h
|
||||
const int COINIT_APARTMENTTHREADED = 0x2;
|
||||
const int E_NOTIMPL = unchecked((int)0X80004001);
|
||||
int result = Windows.NativeMethods.CoInitializeEx(IntPtr.Zero, COINIT_APARTMENTTHREADED);
|
||||
int result = Interop.Windows.CoInitializeEx(IntPtr.Zero, Interop.Windows.COINIT_APARTMENTTHREADED);
|
||||
|
||||
// If 0 is returned the thread has been initialized for the first time
|
||||
// as an STA and thus supported and needs to be uninitialized.
|
||||
if (result > 0)
|
||||
{
|
||||
Windows.NativeMethods.CoUninitialize();
|
||||
Interop.Windows.CoUninitialize();
|
||||
}
|
||||
|
||||
return result != E_NOTIMPL;
|
||||
return result != Interop.Windows.E_NOTIMPL;
|
||||
});
|
||||
|
||||
private static bool? _isNanoServer = null;
|
||||
@@ -453,21 +450,6 @@ namespace System.Management.Automation
|
||||
return Unix.NativeMethods.WaitPid(pid, nohang);
|
||||
}
|
||||
|
||||
internal static partial class Windows
|
||||
{
|
||||
/// <summary>The native methods class.</summary>
|
||||
internal static partial class NativeMethods
|
||||
{
|
||||
private const string ole32Lib = "api-ms-win-core-com-l1-1-0.dll";
|
||||
|
||||
[LibraryImport(ole32Lib)]
|
||||
internal static partial int CoInitializeEx(IntPtr reserve, int coinit);
|
||||
|
||||
[LibraryImport(ole32Lib)]
|
||||
internal static partial void CoUninitialize();
|
||||
}
|
||||
}
|
||||
|
||||
// Please note that `Win32Exception(Marshal.GetLastWin32Error())`
|
||||
// works *correctly* on Linux in that it creates an exception with
|
||||
// the string perror would give you for the last set value of errno.
|
||||
|
||||
@@ -86,10 +86,11 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(IsWindows)' != 'true' ">
|
||||
<Compile Remove="engine\ExtraAdapter.cs" />
|
||||
<Compile Remove="engine\ManagementObjectAdapter.cs" />
|
||||
<Compile Remove="engine\COM\**\*.cs" />
|
||||
<Compile Remove="engine\ComInterop\**\*.cs" />
|
||||
<Compile Remove="engine\ExtraAdapter.cs" />
|
||||
<Compile Remove="engine\Interop\Windows\**\*.cs" />
|
||||
<Compile Remove="engine\ManagementObjectAdapter.cs" />
|
||||
<Compile Remove="engine\remoting\commands\ConnectPSSession.cs" />
|
||||
<Compile Remove="engine\remoting\commands\CustomShellCommands.cs" />
|
||||
<Compile Remove="engine\remoting\commands\DisconnectPSSession.cs" />
|
||||
@@ -97,4 +98,7 @@
|
||||
<Compile Remove="engine\remoting\commands\TestPSSessionConfigurationFile.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(IsWindows)' == 'true' ">
|
||||
<Compile Remove="engine\Interop\Unix\**\*.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace System.Management.Automation
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
public static partial class CompletionCompleters
|
||||
public static class CompletionCompleters
|
||||
{
|
||||
static CompletionCompleters()
|
||||
{
|
||||
@@ -4739,47 +4739,48 @@ namespace System.Management.Automation
|
||||
public string remark;
|
||||
}
|
||||
|
||||
private const int MAX_PREFERRED_LENGTH = -1;
|
||||
private const int NERR_Success = 0;
|
||||
private const int ERROR_MORE_DATA = 234;
|
||||
private const int STYPE_DISKTREE = 0;
|
||||
private const int STYPE_MASK = 0x000000FF;
|
||||
|
||||
private static readonly System.IO.EnumerationOptions _enumerationOptions = new System.IO.EnumerationOptions
|
||||
{
|
||||
MatchCasing = MatchCasing.CaseInsensitive,
|
||||
AttributesToSkip = 0 // Default is to skip Hidden and System files, so we clear this to retain existing behavior
|
||||
};
|
||||
|
||||
[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<string> GetFileShares(string machine, bool ignoreHidden)
|
||||
{
|
||||
#if UNIX
|
||||
return new List<string>();
|
||||
#else
|
||||
IntPtr shBuf;
|
||||
uint numEntries;
|
||||
nint shBuf = nint.Zero;
|
||||
uint numEntries = 0;
|
||||
uint totalEntries;
|
||||
uint resumeHandle = 0;
|
||||
int result = NetShareEnum(machine, 1, out shBuf,
|
||||
MAX_PREFERRED_LENGTH, out numEntries, out totalEntries,
|
||||
ref resumeHandle);
|
||||
int result = Interop.Windows.NetShareEnum(
|
||||
machine,
|
||||
level: 1,
|
||||
out shBuf,
|
||||
Interop.Windows.MAX_PREFERRED_LENGTH,
|
||||
out numEntries,
|
||||
out totalEntries,
|
||||
ref resumeHandle);
|
||||
|
||||
var shares = new List<string>();
|
||||
if (result == NERR_Success || result == ERROR_MORE_DATA)
|
||||
if (result == Interop.Windows.NERR_Success || result == Interop.Windows.ERROR_MORE_DATA)
|
||||
{
|
||||
for (int i = 0; i < numEntries; ++i)
|
||||
{
|
||||
IntPtr curInfoPtr = (IntPtr)((long)shBuf + (Marshal.SizeOf<SHARE_INFO_1>() * i));
|
||||
nint curInfoPtr = shBuf + (Marshal.SizeOf<SHARE_INFO_1>() * i);
|
||||
SHARE_INFO_1 shareInfo = Marshal.PtrToStructure<SHARE_INFO_1>(curInfoPtr);
|
||||
|
||||
if ((shareInfo.type & STYPE_MASK) != STYPE_DISKTREE)
|
||||
if ((shareInfo.type & Interop.Windows.STYPE_MASK) != Interop.Windows.STYPE_DISKTREE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ignoreHidden && shareInfo.netname.EndsWith('$'))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
shares.Add(shareInfo.netname);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("kernel32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool AllocConsole();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class Windows
|
||||
{
|
||||
internal const int COINIT_APARTMENTTHREADED = 0x2;
|
||||
internal const int E_NOTIMPL = unchecked((int)0X80004001);
|
||||
|
||||
[LibraryImport("api-ms-win-core-com-l1-1-0.dll")]
|
||||
internal static partial int CoInitializeEx(nint reserve, int coinit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class Windows
|
||||
{
|
||||
[LibraryImport("api-ms-win-core-com-l1-1-0.dll")]
|
||||
internal static partial void CoUninitialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("api-ms-win-core-file-l2-1-0.dll", EntryPoint = "CreateHardLinkW", StringMarshalling = StringMarshalling.Utf16)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool CreateHardLink(string name, string existingFileName, nint securityAttributes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[Flags]
|
||||
internal enum SymbolicLinkFlags
|
||||
{
|
||||
File = 0,
|
||||
Directory = 1,
|
||||
AllowUnprivilegedCreate = 2,
|
||||
}
|
||||
|
||||
[LibraryImport("api-ms-win-core-file-l2-1-0.dll", EntryPoint = "CreateSymbolicLinkW", StringMarshalling = StringMarshalling.Utf16)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static partial bool CreateSymbolicLink(string name, string destination, SymbolicLinkFlags symbolicLinkFlags);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("api-ms-win-core-file-l1-1-0.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool FindClose(nint handle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Keep native struct names.")]
|
||||
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "Keep native struct names.")]
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
internal const int MAX_PATH = 260;
|
||||
|
||||
internal struct FILE_TIME
|
||||
{
|
||||
public uint dwLowDateTime;
|
||||
public uint dwHighDateTime;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
internal unsafe struct WIN32_FIND_DATA
|
||||
{
|
||||
internal uint dwFileAttributes;
|
||||
internal FILE_TIME ftCreationTime;
|
||||
internal FILE_TIME ftLastAccessTime;
|
||||
internal FILE_TIME ftLastWriteTime;
|
||||
internal uint nFileSizeHigh;
|
||||
internal uint nFileSizeLow;
|
||||
internal uint dwReserved0;
|
||||
internal uint dwReserved1;
|
||||
internal fixed char cFileName[MAX_PATH];
|
||||
internal fixed char cAlternateFileName[14];
|
||||
}
|
||||
|
||||
internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private SafeFindHandle() : base(true) { }
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
return Interop.Windows.FindClose(this.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
|
||||
[LibraryImport("api-ms-win-core-file-l1-1-0.dll", EntryPoint = "FindFirstFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
|
||||
internal static partial SafeFindHandle FindFirstFile(string lpFileName, ref WIN32_FIND_DATA lpFindFileData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("Kernel32.dll")]
|
||||
internal static partial nint GetConsoleWindow();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#nullable enable
|
||||
|
||||
#if !UNIX
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
@@ -14,4 +13,3 @@ internal static partial class Interop
|
||||
internal static partial uint GetCurrentThreadId();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("user32.dll")]
|
||||
internal static partial nint GetForegroundWindow();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("api-ms-win-core-localization-l1-2-0.dll")]
|
||||
internal static partial uint GetOEMCP();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
internal const int MAX_PREFERRED_LENGTH = -1;
|
||||
internal const int NERR_Success = 0;
|
||||
internal const int ERROR_MORE_DATA = 234;
|
||||
internal const int STYPE_DISKTREE = 0;
|
||||
internal const int STYPE_MASK = 0x000000FF;
|
||||
|
||||
[LibraryImport("Netapi32.dll", StringMarshalling = StringMarshalling.Utf16)]
|
||||
internal static partial int NetShareEnum(
|
||||
string serverName,
|
||||
int level,
|
||||
out nint bufptr,
|
||||
int prefMaxLen,
|
||||
out uint entriesRead,
|
||||
out uint totalEntries,
|
||||
ref uint resumeHandle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct PROCESS_BASIC_INFORMATION
|
||||
{
|
||||
public nint ExitStatus;
|
||||
public nint PebBaseAddress;
|
||||
public nint AffinityMask;
|
||||
public nint BasePriority;
|
||||
public nint UniqueProcessId;
|
||||
public nint InheritedFromUniqueProcessId;
|
||||
}
|
||||
|
||||
[LibraryImport("ntdll.dll")]
|
||||
internal static partial int NtQueryInformationProcess(
|
||||
nint processHandle,
|
||||
int processInformationClass,
|
||||
out PROCESS_BASIC_INFORMATION processInformation,
|
||||
int processInformationLength,
|
||||
out int returnLength);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#nullable enable
|
||||
|
||||
#if !UNIX
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.ComponentModel;
|
||||
@@ -123,4 +122,3 @@ internal static partial class Interop
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
internal const sbyte PHCM_APPLICATION_DEFAULT = 0;
|
||||
internal const sbyte PHCM_DISGUISE_PLACEHOLDER = 1;
|
||||
internal const sbyte PHCM_EXPOSE_PLACEHOLDERS = 2;
|
||||
internal const sbyte PHCM_MAX = 2;
|
||||
internal const sbyte PHCM_ERROR_INVALID_PARAMETER = -1;
|
||||
internal const sbyte PHCM_ERROR_NO_TEB = -2;
|
||||
|
||||
[LibraryImport("ntdll.dll")]
|
||||
internal static partial sbyte RtlQueryProcessPlaceholderCompatibilityMode();
|
||||
|
||||
[LibraryImport("ntdll.dll")]
|
||||
internal static partial sbyte RtlSetProcessPlaceholderCompatibilityMode(sbyte pcm);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#nullable enable
|
||||
|
||||
#if !UNIX
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@@ -37,4 +36,3 @@ internal static partial class Interop
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("user32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool SetForegroundWindow(nint hWnd);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
internal const int SW_HIDE = 0;
|
||||
internal const int SW_SHOWNORMAL = 1;
|
||||
internal const int SW_NORMAL = 1;
|
||||
internal const int SW_SHOWMINIMIZED = 2;
|
||||
internal const int SW_SHOWMAXIMIZED = 3;
|
||||
internal const int SW_MAXIMIZE = 3;
|
||||
internal const int SW_SHOWNOACTIVATE = 4;
|
||||
internal const int SW_SHOW = 5;
|
||||
internal const int SW_MINIMIZE = 6;
|
||||
internal const int SW_SHOWMINNOACTIVE = 7;
|
||||
internal const int SW_SHOWNA = 8;
|
||||
internal const int SW_RESTORE = 9;
|
||||
internal const int SW_SHOWDEFAULT = 10;
|
||||
internal const int SW_FORCEMINIMIZE = 11;
|
||||
internal const int SW_MAX = 11;
|
||||
|
||||
[LibraryImport("user32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool ShowWindow(nint hWnd, int nCmdShow);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#nullable enable
|
||||
|
||||
#if !UNIX
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
@@ -14,4 +13,3 @@ internal static partial class Interop
|
||||
internal static partial void VariantClear(nint pVariant);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static unsafe partial class Windows
|
||||
{
|
||||
[LibraryImport("mpr.dll", EntryPoint = "WNetCancelConnection2W", StringMarshalling = StringMarshalling.Utf16)]
|
||||
internal static partial int WNetCancelConnection2(string driveName, int flags, [MarshalAs(UnmanagedType.Bool)] bool force);
|
||||
}
|
||||
}
|
||||
@@ -2078,66 +2078,13 @@ namespace System.Management.Automation
|
||||
/// Static class that allows you to show and hide the console window
|
||||
/// associated with this process.
|
||||
/// </summary>
|
||||
internal static partial class ConsoleVisibility
|
||||
internal static class ConsoleVisibility
|
||||
{
|
||||
/// <summary>
|
||||
/// If set to true, then native commands will always be run redirected...
|
||||
/// </summary>
|
||||
public static bool AlwaysCaptureApplicationIO { get; set; }
|
||||
|
||||
[LibraryImport("Kernel32.dll")]
|
||||
internal static partial IntPtr GetConsoleWindow();
|
||||
|
||||
internal const int SW_HIDE = 0;
|
||||
internal const int SW_SHOWNORMAL = 1;
|
||||
internal const int SW_NORMAL = 1;
|
||||
internal const int SW_SHOWMINIMIZED = 2;
|
||||
internal const int SW_SHOWMAXIMIZED = 3;
|
||||
internal const int SW_MAXIMIZE = 3;
|
||||
internal const int SW_SHOWNOACTIVATE = 4;
|
||||
internal const int SW_SHOW = 5;
|
||||
internal const int SW_MINIMIZE = 6;
|
||||
internal const int SW_SHOWMINNOACTIVE = 7;
|
||||
internal const int SW_SHOWNA = 8;
|
||||
internal const int SW_RESTORE = 9;
|
||||
internal const int SW_SHOWDEFAULT = 10;
|
||||
internal const int SW_FORCEMINIMIZE = 11;
|
||||
internal const int SW_MAX = 11;
|
||||
|
||||
/// <summary>
|
||||
/// Code to control the display properties of the a window...
|
||||
/// </summary>
|
||||
/// <param name="hWnd">The window to show...</param>
|
||||
/// <param name="nCmdShow">The command to do.</param>
|
||||
/// <returns>True if it was successful.</returns>
|
||||
[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>
|
||||
[LibraryImport("kernel32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
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>
|
||||
[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>
|
||||
[LibraryImport("user32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static partial bool SetForegroundWindow(IntPtr hWnd);
|
||||
|
||||
/// <summary>
|
||||
/// If no console window is attached to this process, then allocate one,
|
||||
/// hide it and return true. If there's already a console window attached, then
|
||||
@@ -2146,77 +2093,44 @@ namespace System.Management.Automation
|
||||
/// <returns></returns>
|
||||
internal static bool AllocateHiddenConsole()
|
||||
{
|
||||
#if UNIX
|
||||
return false;
|
||||
#else
|
||||
// See if there is already a console attached.
|
||||
IntPtr hwnd = ConsoleVisibility.GetConsoleWindow();
|
||||
if (hwnd != IntPtr.Zero)
|
||||
IntPtr hwnd = Interop.Windows.GetConsoleWindow();
|
||||
if (hwnd != nint.Zero)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// save the foreground window since allocating a console window might remove focus from it
|
||||
IntPtr savedForeground = ConsoleVisibility.GetForegroundWindow();
|
||||
IntPtr savedForeground = Interop.Windows.GetForegroundWindow();
|
||||
|
||||
// Since there is no console window, allocate and then hide it...
|
||||
// Suppress the PreFAST warning about not using Marshal.GetLastWin32Error() to
|
||||
// get the error code.
|
||||
#pragma warning disable 56523
|
||||
ConsoleVisibility.AllocConsole();
|
||||
hwnd = ConsoleVisibility.GetConsoleWindow();
|
||||
Interop.Windows.AllocConsole();
|
||||
hwnd = Interop.Windows.GetConsoleWindow();
|
||||
|
||||
bool returnValue;
|
||||
if (hwnd == IntPtr.Zero)
|
||||
if (hwnd == nint.Zero)
|
||||
{
|
||||
returnValue = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = true;
|
||||
ConsoleVisibility.ShowWindow(hwnd, ConsoleVisibility.SW_HIDE);
|
||||
Interop.Windows.ShowWindow(hwnd, Interop.Windows.SW_HIDE);
|
||||
AlwaysCaptureApplicationIO = true;
|
||||
}
|
||||
|
||||
if (savedForeground != IntPtr.Zero && ConsoleVisibility.GetForegroundWindow() != savedForeground)
|
||||
if (savedForeground != nint.Zero && Interop.Windows.GetForegroundWindow() != savedForeground)
|
||||
{
|
||||
ConsoleVisibility.SetForegroundWindow(savedForeground);
|
||||
Interop.Windows.SetForegroundWindow(savedForeground);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If there is a console attached, then make it visible
|
||||
/// and allow interactive console applications to be run.
|
||||
/// </summary>
|
||||
public static void Show()
|
||||
{
|
||||
IntPtr hwnd = GetConsoleWindow();
|
||||
if (hwnd != IntPtr.Zero)
|
||||
{
|
||||
ShowWindow(hwnd, SW_SHOW);
|
||||
AlwaysCaptureApplicationIO = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw PSTraceSource.NewInvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If there is a console attached, then hide it and always capture
|
||||
/// output from the child process.
|
||||
/// </summary>
|
||||
public static void Hide()
|
||||
{
|
||||
IntPtr hwnd = GetConsoleWindow();
|
||||
if (hwnd != IntPtr.Zero)
|
||||
{
|
||||
ShowWindow(hwnd, SW_HIDE);
|
||||
AlwaysCaptureApplicationIO = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw PSTraceSource.NewInvalidOperationException();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Microsoft.PowerShell
|
||||
/// <summary>
|
||||
/// Helper functions for process info.
|
||||
/// </summary>
|
||||
public static partial class ProcessCodeMethods
|
||||
public static class ProcessCodeMethods
|
||||
{
|
||||
private const int InvalidProcessId = -1;
|
||||
|
||||
@@ -61,32 +61,12 @@ namespace Microsoft.PowerShell
|
||||
internal static int GetParentPid(Process process)
|
||||
{
|
||||
Diagnostics.Assert(process != null, "Ensure process is not null before calling");
|
||||
PROCESS_BASIC_INFORMATION pbi;
|
||||
Interop.Windows.PROCESS_BASIC_INFORMATION pbi;
|
||||
int size;
|
||||
var res = NtQueryInformationProcess(process.Handle, 0, out pbi, Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), out size);
|
||||
var res = Interop.Windows.NtQueryInformationProcess(process.Handle, 0, out pbi, Marshal.SizeOf<Interop.Windows.PROCESS_BASIC_INFORMATION>(), out size);
|
||||
|
||||
return res != 0 ? InvalidProcessId : pbi.InheritedFromUniqueProcessId.ToInt32();
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct PROCESS_BASIC_INFORMATION
|
||||
{
|
||||
public IntPtr ExitStatus;
|
||||
public IntPtr PebBaseAddress;
|
||||
public IntPtr AffinityMask;
|
||||
public IntPtr BasePriority;
|
||||
public IntPtr UniqueProcessId;
|
||||
public IntPtr InheritedFromUniqueProcessId;
|
||||
}
|
||||
|
||||
[LibraryImport("ntdll.dll")]
|
||||
private static partial int NtQueryInformationProcess(
|
||||
IntPtr processHandle,
|
||||
int processInformationClass,
|
||||
out PROCESS_BASIC_INFORMATION processInformation,
|
||||
int processInformationLength,
|
||||
out int returnLength);
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,9 +474,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134, 0))
|
||||
{
|
||||
// let's be safe, don't change the PlaceHolderCompatibilityMode if the current one is not what we expect
|
||||
if (NativeMethods.RtlQueryProcessPlaceholderCompatibilityMode() == NativeMethods.PHCM_DISGUISE_PLACEHOLDER)
|
||||
if (Interop.Windows.RtlQueryProcessPlaceholderCompatibilityMode() == Interop.Windows.PHCM_DISGUISE_PLACEHOLDER)
|
||||
{
|
||||
NativeMethods.RtlSetProcessPlaceholderCompatibilityMode(NativeMethods.PHCM_EXPOSE_PLACEHOLDERS);
|
||||
Interop.Windows.RtlSetProcessPlaceholderCompatibilityMode(Interop.Windows.PHCM_EXPOSE_PLACEHOLDERS);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -725,12 +725,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
#if UNIX
|
||||
return drive;
|
||||
#else
|
||||
return WinRemoveDrive(drive);
|
||||
#endif
|
||||
}
|
||||
|
||||
private PSDriveInfo WinRemoveDrive(PSDriveInfo drive)
|
||||
{
|
||||
if (IsNetworkMappedDrive(drive))
|
||||
{
|
||||
const int CONNECT_UPDATE_PROFILE = 0x00000001;
|
||||
@@ -758,7 +752,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
try
|
||||
{
|
||||
code = NativeMethods.WNetCancelConnection2(driveName, flags, true);
|
||||
code = Interop.Windows.WNetCancelConnection2(driveName, flags, true);
|
||||
}
|
||||
catch (System.DllNotFoundException)
|
||||
{
|
||||
@@ -774,6 +768,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
return drive;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -2446,7 +2441,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
#if UNIX
|
||||
success = Platform.NonWindowsCreateHardLink(path, strTargetPath);
|
||||
#else
|
||||
success = WinCreateHardLink(path, strTargetPath);
|
||||
success = Interop.Windows.CreateHardLink(path, strTargetPath, IntPtr.Zero);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -2649,25 +2644,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
}
|
||||
|
||||
#if !UNIX
|
||||
private static bool WinCreateSymbolicLink(string path, string strTargetPath, bool isDirectory)
|
||||
{
|
||||
// The new AllowUnprivilegedCreate is only available on Win10 build 14972 or newer
|
||||
var flags = isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File;
|
||||
var flags = isDirectory ? Interop.Windows.SymbolicLinkFlags.Directory : Interop.Windows.SymbolicLinkFlags.File;
|
||||
|
||||
if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 14972, 0))
|
||||
{
|
||||
flags |= NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate;
|
||||
flags |= Interop.Windows.SymbolicLinkFlags.AllowUnprivilegedCreate;
|
||||
}
|
||||
|
||||
var created = NativeMethods.CreateSymbolicLink(path, strTargetPath, flags);
|
||||
var created = Interop.Windows.CreateSymbolicLink(path, strTargetPath, flags);
|
||||
return created;
|
||||
}
|
||||
|
||||
private static bool WinCreateHardLink(string path, string strTargetPath)
|
||||
{
|
||||
bool success = NativeMethods.CreateHardLink(path, strTargetPath, IntPtr.Zero);
|
||||
return success;
|
||||
}
|
||||
#endif
|
||||
|
||||
private static bool WinCreateJunction(string path, string strTargetPath)
|
||||
{
|
||||
@@ -7114,27 +7105,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
|
||||
internal static extern int WNetAddConnection2(ref NetResource netResource, byte[] password, string username, int flags);
|
||||
|
||||
/// <summary>
|
||||
/// WNetCancelConnection2 function cancels an existing network connection.
|
||||
/// </summary>
|
||||
/// <param name="driveName">
|
||||
/// PSDrive Name.
|
||||
/// </param>
|
||||
/// <param name="flags">
|
||||
/// Connection Type.
|
||||
/// </param>
|
||||
/// <param name="force">
|
||||
/// Specifies whether the disconnection should occur if there are open files or jobs
|
||||
/// on the connection. If this parameter is FALSE, the function fails
|
||||
/// if there are open files or jobs.
|
||||
/// </param>
|
||||
/// <returns>If connection is removed then success is returned or
|
||||
/// else the error code describing the type of failure that occurred while
|
||||
/// trying to remove the connection is returned.
|
||||
/// </returns>
|
||||
[LibraryImport("mpr.dll", EntryPoint ="WNetCancelConnection2W", StringMarshalling = StringMarshalling.Utf16)]
|
||||
internal static partial int WNetCancelConnection2(string driveName, int flags, [MarshalAs(UnmanagedType.Bool)] bool force);
|
||||
|
||||
/// <summary>
|
||||
/// WNetGetConnection function retrieves the name of the network resource associated with a local device.
|
||||
/// </summary>
|
||||
@@ -7228,75 +7198,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool PathIsNetworkPath(string path);
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Creates a symbolic link using the native API.
|
||||
/// </summary>
|
||||
/// <param name="name">Path of the symbolic link.</param>
|
||||
/// <param name="destination">Path of the target of the symbolic link.</param>
|
||||
/// <param name="symbolicLinkFlags">Flag values from SymbolicLinkFlags enum.</param>
|
||||
/// <returns>1 on successful creation.</returns>
|
||||
[LibraryImport(PinvokeDllNames.CreateSymbolicLinkDllName, EntryPoint = "CreateSymbolicLinkW", StringMarshalling = StringMarshalling.Utf16)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static partial bool CreateSymbolicLink(string name, string destination, SymbolicLinkFlags symbolicLinkFlags);
|
||||
|
||||
/// <summary>
|
||||
/// Flags used when creating a symbolic link.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
internal enum SymbolicLinkFlags
|
||||
{
|
||||
/// <summary>
|
||||
/// Symbolic link is a file.
|
||||
/// </summary>
|
||||
File = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Symbolic link is a directory.
|
||||
/// </summary>
|
||||
Directory = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Allow creation of symbolic link without elevation. Requires Developer mode.
|
||||
/// </summary>
|
||||
AllowUnprivilegedCreate = 2,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a hard link using the native API.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the hard link.</param>
|
||||
/// <param name="existingFileName">Path to the target of the hard link.</param>
|
||||
/// <param name="SecurityAttributes"></param>
|
||||
/// <returns></returns>
|
||||
[LibraryImport(PinvokeDllNames.CreateHardLinkDllName, EntryPoint = "CreateHardLinkW", StringMarshalling = StringMarshalling.Utf16)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool CreateHardLink(string name, string existingFileName, IntPtr SecurityAttributes);
|
||||
|
||||
// OneDrive placeholder support
|
||||
#if !UNIX
|
||||
/// <summary>
|
||||
/// Returns the placeholder compatibility mode for the current process.
|
||||
/// </summary>
|
||||
/// <returns>The process's placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx).</returns>
|
||||
[LibraryImport("ntdll.dll")]
|
||||
internal static partial sbyte RtlQueryProcessPlaceholderCompatibilityMode();
|
||||
|
||||
/// <summary>
|
||||
/// Sets the placeholder compatibility mode for the current process.
|
||||
/// </summary>
|
||||
/// <param name="pcm">The placeholder compatibility mode to set.</param>
|
||||
/// <returns>The process's previous placeholder compatibily mode (PHCM_xxx), or a negative value on error (PCHM_ERROR_xxx).</returns>
|
||||
[LibraryImport("ntdll.dll")]
|
||||
internal static partial sbyte RtlSetProcessPlaceholderCompatibilityMode(sbyte pcm);
|
||||
|
||||
internal const sbyte PHCM_APPLICATION_DEFAULT = 0;
|
||||
internal const sbyte PHCM_DISGUISE_PLACEHOLDER = 1;
|
||||
internal const sbyte PHCM_EXPOSE_PLACEHOLDERS = 2;
|
||||
internal const sbyte PHCM_MAX = 2;
|
||||
internal const sbyte PHCM_ERROR_INVALID_PARAMETER = -1;
|
||||
internal const sbyte PHCM_ERROR_NO_TEB = -2;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -7963,40 +7864,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
FileAttributes dwFlagsAndAttributes,
|
||||
IntPtr hTemplateFile);
|
||||
|
||||
internal sealed partial class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private SafeFindHandle() : base(true) { }
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
return FindClose(this.handle);
|
||||
}
|
||||
|
||||
[LibraryImport(PinvokeDllNames.FindCloseDllName)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
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
|
||||
[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 FILE_TIME ftCreationTime;
|
||||
internal FILE_TIME ftLastAccessTime;
|
||||
internal FILE_TIME ftLastWriteTime;
|
||||
internal uint nFileSizeHigh;
|
||||
internal uint nFileSizeLow;
|
||||
internal uint dwReserved0;
|
||||
internal uint dwReserved1;
|
||||
internal fixed char cFileName[MAX_PATH];
|
||||
internal fixed char cAlternateFileName[14];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the target of the specified reparse point.
|
||||
/// </summary>
|
||||
@@ -8162,14 +8029,14 @@ namespace Microsoft.PowerShell.Commands
|
||||
return !InternalTestHooks.OneDriveTestRecurseOn;
|
||||
}
|
||||
|
||||
WIN32_FIND_DATA data = default;
|
||||
Interop.Windows.WIN32_FIND_DATA data = default;
|
||||
string fullPath = Path.TrimEndingDirectorySeparator(fileInfo.FullName);
|
||||
if (fullPath.Length >= MAX_PATH)
|
||||
{
|
||||
fullPath = PathUtils.EnsureExtendedPrefix(fullPath);
|
||||
}
|
||||
|
||||
using (SafeFindHandle handle = FindFirstFile(fullPath, ref data))
|
||||
using (Interop.Windows.SafeFindHandle handle = Interop.Windows.FindFirstFile(fullPath, ref data))
|
||||
{
|
||||
if (handle.IsInvalid)
|
||||
{
|
||||
|
||||
@@ -7,7 +7,6 @@ using System.IO;
|
||||
using System.Management.Automation.Internal;
|
||||
using System.Management.Automation.Language;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Loader;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
@@ -19,7 +18,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.
|
||||
/// </summary>
|
||||
internal static partial class ClrFacade
|
||||
internal static class ClrFacade
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialize powershell AssemblyLoadContext and register the 'Resolving' event, if it's not done already.
|
||||
@@ -113,7 +112,7 @@ namespace System.Management.Automation
|
||||
#if UNIX
|
||||
s_oemEncoding = new UTF8Encoding(false);
|
||||
#else
|
||||
uint oemCp = NativeMethods.GetOEMCP();
|
||||
uint oemCp = Interop.Windows.GetOEMCP();
|
||||
s_oemEncoding = Encoding.GetEncoding((int)oemCp);
|
||||
#endif
|
||||
}
|
||||
@@ -362,17 +361,5 @@ namespace System.Management.Automation
|
||||
}
|
||||
|
||||
#endregion Misc
|
||||
|
||||
/// <summary>
|
||||
/// Native methods that are used by facade methods.
|
||||
/// </summary>
|
||||
private static partial class NativeMethods
|
||||
{
|
||||
/// <summary>
|
||||
/// Pinvoke for GetOEMCP to get the OEM code page.
|
||||
/// </summary>
|
||||
[LibraryImport(PinvokeDllNames.GetOEMCPDllName)]
|
||||
internal static partial uint GetOEMCP();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user