Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Management.Automation;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;

using Microsoft.Management.Infrastructure;
using Microsoft.Win32;
Expand Down Expand Up @@ -887,9 +888,8 @@ private static ComputerInfo CreateFullOutputObject(SystemInfoGroup systemInfo, O
// we display info for only one

string layout = otherInfo.keyboards[0].Layout;
var culture = Conversion.MakeLocale(layout);

output.KeyboardLayout = culture == null ? layout : culture.Name;
output.KeyboardLayout = Conversion.GetLocaleName(layout);
}

if (otherInfo.hyperV != null)
Expand Down Expand Up @@ -1097,29 +1097,6 @@ internal static bool TryParseHex(string hexString, out uint value)
}
}

public static string LocaleIdToLocaleName(uint localeID)
{
// CoreCLR's System.Globalization.Culture does not appear to have a constructor
// that accepts an integer LocalID (LCID) value, so we'll PInvoke native code
// to get a locale name from an LCID value

try
{
var sbName = new System.Text.StringBuilder(Native.LOCALE_NAME_MAX_LENGTH);
var len = Native.LCIDToLocaleName(localeID, sbName, sbName.Capacity, 0);

if (len > 0 && sbName.Length > 0)
return sbName.ToString();
}
catch (Exception)
{
// Probably failed to load the DLL or to file the function entry point.
// Fail silently
}

return null;
}

/// <summary>
/// Attempt to create a <see cref="System.Globalization.CultureInfo"/>
/// object from a locale string as retrieved from WMI.
Expand All @@ -1136,38 +1113,36 @@ public static string LocaleIdToLocaleName(uint localeID)
/// Failing that it attempts to retrieve the CultureInfo object
/// using the locale string as passed.
/// </remarks>
internal static System.Globalization.CultureInfo MakeLocale(string locale)
internal static string GetLocaleName(string locale)
{
System.Globalization.CultureInfo culture = null;
CultureInfo culture = null;

if (locale != null)
{
try
{
uint localeNum;

if (TryParseHex(locale, out localeNum))
// The "locale" must contain a hexadecimal value, with no
// base-indication prefix. For example, the string "0409" will be
// parsed into the base-10 integer value 1033, while the string "0x0409"
// will fail to parse due to the "0x" base-indication prefix.
if (UInt32.TryParse(locale, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint localeNum))
{
string localeName = LocaleIdToLocaleName(localeNum);

if (localeName != null)
culture = new System.Globalization.CultureInfo(localeName);
culture = CultureInfo.GetCultureInfo((int)localeNum);
}

if (culture == null)
{
// either the TryParseHex failed, or the LocaleIdToLocaleName
// failed, so we'll try using the original string
culture = new System.Globalization.CultureInfo(locale);
// If TryParse failed we'll try using the original string as culture name
culture = CultureInfo.GetCultureInfo(locale);
}
}
catch (Exception/* ex*/)
catch (Exception)
{
culture = null;
}
}

return culture;
return culture == null ? null : culture.Name;
}

/// <summary>
Expand Down Expand Up @@ -1336,18 +1311,26 @@ internal abstract class WmiClassBase
/// <summary>
/// Get a language name from a language identifier.
/// </summary>
/// <param name="language">
/// <param name="lcid">
/// A nullable integer containing the language ID for the desired language.
/// </param>
/// <returns>
/// A string containing the display name of the language identified by
/// the language parameter. If the language parameter is null or has a
/// value that is not a valid language ID, the method returns null.
/// </returns>
protected static string GetLanguageName(uint? language)
protected static string GetLanguageName(uint? lcid)
{
if (language != null)
return Conversion.LocaleIdToLocaleName(language.Value);
if (lcid != null && lcid >= 0)
{
try
{
return CultureInfo.GetCultureInfo((int)lcid.Value).Name;
}
catch
{
}
}

return null;
}
Expand Down Expand Up @@ -1896,12 +1879,7 @@ public OSProductSuite[] Suites
#region Public Methods
public string GetLocale()
{
System.Globalization.CultureInfo culture = null;

if (Locale != null)
culture = Conversion.MakeLocale(Locale);

return culture == null ? null : culture.Name;
return Conversion.GetLocaleName(Locale);
}
#endregion Public Methods

Expand Down Expand Up @@ -5112,7 +5090,6 @@ internal static class Native
private static class PInvokeDllNames
{
public const string GetPhysicallyInstalledSystemMemoryDllName = "api-ms-win-core-sysinfo-l1-2-1.dll";
public const string LCIDToLocaleNameDllName = "kernelbase.dll";
public const string PowerDeterminePlatformRoleExDllName = "api-ms-win-power-base-l1-1-0.dll";
public const string GetFirmwareTypeDllName = "api-ms-win-core-kernel32-legacy-l1-1-1";
}
Expand Down Expand Up @@ -5152,17 +5129,6 @@ private static class PInvokeDllNames
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetFirmwareType(out FirmwareType firmwareType);

/// <summary>
/// Convert a Local Identifier to a Locale name.
/// </summary>
/// <param name="localeID">The Locale ID (LCID) to be converted.</param>
/// <param name="localeName">Destination of the Locale name.</param>
/// <param name="localeNameSize">Capacity of <paramref name="localeName"/></param>
/// <param name="flags"></param>
/// <returns></returns>
[DllImport(PInvokeDllNames.LCIDToLocaleNameDllName, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int LCIDToLocaleName(uint localeID, System.Text.StringBuilder localeName, int localeNameSize, int flags);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also remove the definition of PInvokeDllNames.LCIDToLocaleNameDllName at line 5115.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


/// <summary>
/// Gets the data specified for the passed in property name from the
/// Software Licensing API.
Expand Down