// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
namespace Wox.Plugin.Common.Win32
{
public static class Win32Helpers
{
///
/// Detects the type of system firmware which is equal to the boot type by calling the method .
///
/// Firmware type like Uefi or Bios.
public static FirmwareType GetSystemFirmwareType()
{
FirmwareType firmwareType = default;
_ = NativeMethods.GetFirmwareType(ref firmwareType);
return firmwareType;
}
///
/// Returns the last Win32 Error code thrown by a native method if enabled for this method.
///
/// The error code as int value.
public static int GetLastError()
{
return Marshal.GetLastPInvokeError();
}
///
/// Validate that the handle is not null and close it.
///
/// Handle to close.
/// Zero if native method fails and nonzero if the native method succeeds.
public static bool CloseHandleIfNotNull(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
// Return true if there is nothing to close.
return true;
}
return NativeMethods.CloseHandle(handle);
}
///
/// Gets the description for an HRESULT error code.
///
/// The HRESULT number
/// A string containing the description.
public static string MessageFromHResult(int hr)
{
return Marshal.GetExceptionForHR(hr).Message;
}
}
}