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
83 changes: 82 additions & 1 deletion src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,90 @@ namespace Microsoft.PowerShell.Commands.Diagnostics.Common
{
internal static class CommonUtilities
{
private const string LibraryLoadDllName = "api-ms-win-core-libraryloader-l1-2-0.dll";
private const string LocalizationDllName = "api-ms-win-core-localization-l1-2-1.dll";

private const uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
private const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
private const uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;

[DllImport(LocalizationDllName, SetLastError = true, CharSet = CharSet.Unicode)]
private static extern uint FormatMessage(uint dwFlags, IntPtr lpSource,
uint dwMessageId, uint dwLanguageId,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder lpBuffer,
uint nSize, IntPtr Arguments);

[DllImport(LibraryLoadDllName, SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr LoadLibraryEx(
[MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
IntPtr hFile,
uint dwFlags
);

[DllImport(LibraryLoadDllName)]
private static extern bool FreeLibrary(IntPtr hModule);


[DllImport(LocalizationDllName, EntryPoint = "GetUserDefaultLangID", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
private static extern ushort GetUserDefaultLangID();


public static uint FormatMessageFromModule(uint lastError, string moduleName, out String msg)
{
Debug.Assert(!string.IsNullOrEmpty(moduleName));

uint formatError = 0;
msg = String.Empty;

IntPtr moduleHandle = LoadLibraryEx(moduleName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
if (moduleHandle == IntPtr.Zero)
{
return (uint)Marshal.GetLastWin32Error();
}

try
{
uint dwFormatFlags = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE;
uint LANGID = (uint)GetUserDefaultLangID();
uint langError = (uint)Marshal.GetLastWin32Error();
if (langError != 0)
{
LANGID = 0; // neutral
}

StringBuilder outStringBuilder = new StringBuilder(1024);
uint nChars = FormatMessage(dwFormatFlags,
moduleHandle,
lastError,
LANGID,
outStringBuilder,
(uint)outStringBuilder.Capacity,
IntPtr.Zero);

if (nChars == 0)
{
formatError = (uint)Marshal.GetLastWin32Error();
}
else
{
msg = outStringBuilder.ToString();
if (msg.EndsWith(Environment.NewLine, StringComparison.Ordinal))
{
msg = msg.Substring(0, msg.Length - 2);
}
}
}
finally
{
FreeLibrary(moduleHandle);
}
return formatError;
}

public static ResourceManager GetResourceManager()
{
// this naming pattern is dictated by the dotnet cli
return new ResourceManager("Microsoft.PowerShell.Commands.Diagnostics.resources.GetEventResources", typeof(CommonUtilities).GetTypeInfo().Assembly);
}
}
Expand Down
19 changes: 2 additions & 17 deletions src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Microsoft.PowerShell.Commands
///
/// Class that implements the Get-Counter cmdlet.
///
[Cmdlet(VerbsCommon.Get, "Counter", DefaultParameterSetName = "GetCounterSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=138335")]
[Cmdlet(VerbsCommon.Get, "Counter", DefaultParameterSetName = "GetCounterSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2109647")]
public sealed class GetCounterCommand : PSCmdlet
{
//
Expand Down Expand Up @@ -211,19 +211,13 @@ public string[] ComputerName
protected override void BeginProcessing()
{

#if CORECLR
if (Platform.IsIoT)
{
// IoT does not have the '$env:windir\System32\pdh.dll' assembly which is required by this cmdlet.
throw new PlatformNotSupportedException();
}

// PowerShell 7 requires at least Windows 7,
// so no version test is needed
_pdhHelper = new PdhHelper(false);
#else
_pdhHelper = new PdhHelper(System.Environment.OSVersion.Version.Major < 6);
#endif
_pdhHelper = new PdhHelper();
_resourceMgr = Microsoft.PowerShell.Commands.Diagnostics.Common.CommonUtilities.GetResourceManager();

uint res = _pdhHelper.ConnectToDataSource();
Expand Down Expand Up @@ -570,12 +564,7 @@ private void ProcessGetCounter()
break;
}

#if CORECLR
// CoreCLR has no overload of WaitOne with (interval, exitContext)
bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000);
#else
bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000, true);
#endif
if (cancelled)
{
break;
Expand Down Expand Up @@ -666,11 +655,7 @@ private void WriteSampleSetObject(PerformanceCounterSampleSet set)

private static CultureInfo GetCurrentCulture()
{
#if CORECLR
return CultureInfo.CurrentCulture;
#else
return Thread.CurrentThread.CurrentUICulture;
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="GetEventSnapin.cs" />
<Compile Remove="ExportCounterCommand.cs" />
<Compile Remove="ImportCounterCommand.cs" />
<Compile Remove="CounterFileInfo.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(IsWindows)' != 'true' ">
<Compile Remove="GetCounterCommand.cs" />
<Compile Remove="CounterSample.cs" />
<Compile Remove="CounterSet.cs" />
<Compile Remove="ExportCounterCommand.cs" />
<Compile Remove="GetCounterCommand.cs" />
<Compile Remove="GetEventSnapin.cs" />
<Compile Remove="ImportCounterCommand.cs" />
<Compile Remove="PdhHelper.cs" />
<Compile Remove="PdhSafeHandle.cs" />
<Compile Remove="gen\GetEventResources.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(IsWindows)' != 'true' ">
<Compile Remove="GetEventCommand.cs" />
<Compile Remove="NewWinEventCommand.cs" />
</ItemGroup>
Expand Down
Loading