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
24 changes: 3 additions & 21 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,16 @@ public static bool IsStaSupported

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;
Expand Down Expand Up @@ -453,21 +450,6 @@ internal static int NonWindowsWaitPid(int pid, bool nohang)
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@
</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" />
<Compile Remove="engine\remoting\commands\ReceivePSSession.cs" />
<Compile Remove="engine\remoting\commands\TestPSSessionConfigurationFile.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(IsWindows)' == 'true' ">
<Compile Remove="engine\Interop\Unix\**\*.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace System.Management.Automation
{
/// <summary>
/// </summary>
public static partial class CompletionCompleters
public static class CompletionCompleters
{
static CompletionCompleters()
{
Expand Down Expand Up @@ -4739,47 +4739,48 @@ private struct SHARE_INFO_1
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#nullable enable

#if !UNIX
using System.Runtime.InteropServices;

internal static partial class Interop
Expand All @@ -14,4 +13,3 @@ internal static partial class Windows
internal static partial uint GetCurrentThreadId();
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading