MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Diagnostics;6using System.Reflection;7using System.Resources;8using System.Runtime.InteropServices;9using System.Text;10 11namespace Microsoft.PowerShell.Commands.Diagnostics.Common12{13 internal static class CommonUtilities14 {15 private const string LibraryLoadDllName = "api-ms-win-core-libraryloader-l1-2-0.dll";16 private const string LocalizationDllName = "api-ms-win-core-localization-l1-2-1.dll";17 18 private const uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;19 private const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;20 private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;21 private const uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;22 23 [DllImport(LocalizationDllName, SetLastError = true, CharSet = CharSet.Unicode)]24 private static extern uint FormatMessage(uint dwFlags, IntPtr lpSource,25 uint dwMessageId, uint dwLanguageId,26 [MarshalAs(UnmanagedType.LPWStr)]27 StringBuilder lpBuffer,28 uint nSize, IntPtr Arguments);29 30 [DllImport(LibraryLoadDllName, SetLastError = true, CharSet = CharSet.Unicode)]31 private static extern IntPtr LoadLibraryEx(32 [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,33 IntPtr hFile,34 uint dwFlags35 );36 37 [DllImport(LibraryLoadDllName)]38 private static extern bool FreeLibrary(IntPtr hModule);39 40 [DllImport(LocalizationDllName, EntryPoint = "GetUserDefaultLangID", CallingConvention = CallingConvention.Winapi, SetLastError = true)]41 private static extern ushort GetUserDefaultLangID();42 43 public static uint FormatMessageFromModule(uint lastError, string moduleName, out string msg)44 {45 Debug.Assert(!string.IsNullOrEmpty(moduleName));46 47 uint formatError = 0;48 msg = string.Empty;49 50 IntPtr moduleHandle = LoadLibraryEx(moduleName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);51 if (moduleHandle == IntPtr.Zero)52 {53 return (uint)Marshal.GetLastWin32Error();54 }55 56 try57 {58 uint LANGID = (uint)GetUserDefaultLangID();59 uint langError = (uint)Marshal.GetLastWin32Error();60 if (langError != 0)61 {62 LANGID = 0; // neutral63 }64 65 StringBuilder outStringBuilder = new(1024);66 uint nChars = FormatMessage(67 dwFlags: FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,68 lpSource: moduleHandle,69 dwMessageId: lastError,70 dwLanguageId: LANGID,71 lpBuffer: outStringBuilder,72 nSize: (uint)outStringBuilder.Capacity,73 Arguments: IntPtr.Zero);74 75 if (nChars == 0)76 {77 formatError = (uint)Marshal.GetLastWin32Error();78 }79 else80 {81 msg = outStringBuilder.ToString();82 if (msg.EndsWith(Environment.NewLine, StringComparison.Ordinal))83 {84 msg = msg.Substring(0, msg.Length - 2);85 }86 }87 }88 finally89 {90 FreeLibrary(moduleHandle);91 }92 return formatError;93 }94 95 public static ResourceManager GetResourceManager()96 {97 return new ResourceManager("Microsoft.PowerShell.Commands.Diagnostics.resources.GetEventResources", typeof(CommonUtilities).GetTypeInfo().Assembly);98 }99 }100}101 