Add wrappers around native API calls to catch exceptions.
Exceptions might be thrown if the referenced DLL is not present or if the native function is not found.
This commit is contained in:
parent
27c819ce2e
commit
10f6e2fa6e
1 changed files with 63 additions and 11 deletions
|
@ -557,15 +557,11 @@ namespace Microsoft.PowerShell.Commands
|
||||||
|
|
||||||
// get secure-boot info
|
// get secure-boot info
|
||||||
//TODO: Local machine only? Check for that?
|
//TODO: Local machine only? Check for that?
|
||||||
FirmwareType fwType = FirmwareType.Unknown;
|
rv.firmwareType = GetFirmwareType();
|
||||||
if (Native.GetFirmwareType(ref fwType))
|
|
||||||
rv.firmwareType = fwType;
|
|
||||||
|
|
||||||
// get amount of memory physically installed
|
// get amount of memory physically installed
|
||||||
//TODO: Local machine only. Check for that?
|
//TODO: Local machine only. Check for that?
|
||||||
UInt64 memory;
|
rv.physicallyInstalledMemory = GetPhysicallyInstalledSystemMemory();
|
||||||
if (Native.GetPhysicallyInstalledSystemMemory(out memory))
|
|
||||||
rv.physicallyInstalledMemory = memory;
|
|
||||||
|
|
||||||
|
|
||||||
// get time zone
|
// get time zone
|
||||||
|
@ -605,6 +601,54 @@ namespace Microsoft.PowerShell.Commands
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wrapper around the native GetFirmwareType function.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// null if unsuccessful, otherwise FirmwareType enum specifying
|
||||||
|
/// the firmware type.
|
||||||
|
/// </returns>
|
||||||
|
private static Nullable<FirmwareType> GetFirmwareType()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FirmwareType firmwareType;
|
||||||
|
|
||||||
|
if (Native.GetFirmwareType(out firmwareType))
|
||||||
|
return firmwareType;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Probably failed to load the DLL or to file the function entry point.
|
||||||
|
// Fail silently
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wrapper around the native GetPhysicallyInstalledSystemMemory function.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// null if unsuccessful, otherwise the amount of physically installed memory.
|
||||||
|
/// </returns>
|
||||||
|
private static Nullable<UInt64> GetPhysicallyInstalledSystemMemory()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UInt64 memory;
|
||||||
|
if (Native.GetPhysicallyInstalledSystemMemory(out memory))
|
||||||
|
return memory;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Probably failed to load the DLL or to file the function entry point.
|
||||||
|
// Fail silently
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new ComputerInfo object populated with the specified data objects.
|
/// Create a new ComputerInfo object populated with the specified data objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -1058,11 +1102,19 @@ namespace Microsoft.PowerShell.Commands
|
||||||
// that accepts an integer LocalID (LCID) value, so we'll PInvoke native code
|
// that accepts an integer LocalID (LCID) value, so we'll PInvoke native code
|
||||||
// to get a locale name from an LCID value
|
// to get a locale name from an LCID value
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var sbName = new System.Text.StringBuilder(Native.LOCALE_NAME_MAX_LENGTH);
|
var sbName = new System.Text.StringBuilder(Native.LOCALE_NAME_MAX_LENGTH);
|
||||||
var len = Native.LCIDToLocaleName(localeID, sbName, sbName.Capacity, 0);
|
var len = Native.LCIDToLocaleName(localeID, sbName, sbName.Capacity, 0);
|
||||||
|
|
||||||
if (len > 0 && sbName.Length > 0)
|
if (len > 0 && sbName.Length > 0)
|
||||||
return sbName.ToString();
|
return sbName.ToString();
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Probably failed to load the DLL or to file the function entry point.
|
||||||
|
// Fail silently
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -5094,7 +5146,7 @@ namespace Microsoft.PowerShell.Commands
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[DllImport(PInvokeDllNames.GetFirmwareTypeDllName, SetLastError = true)]
|
[DllImport(PInvokeDllNames.GetFirmwareTypeDllName, SetLastError = true)]
|
||||||
[return: MarshalAs(UnmanagedType.Bool)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
||||||
public static extern bool GetFirmwareType(ref FirmwareType firmwareType);
|
public static extern bool GetFirmwareType(out FirmwareType firmwareType);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Convert a Local Identifier to a Locale name
|
/// Convert a Local Identifier to a Locale name
|
||||||
|
|
Loading…
Reference in a new issue