Returns the HRESULT associated with the given error code.
/*
* Public, Non-Commecial use.
*
* Copyright ? 2008 Mordechai Botrushvili (GASS Ltd.)
* moti@gass-ltd.co.il
* Bosmat 2a, Shoham 60850, Israel.
* All rights reserved. Israel.
*
* Use is permitted with restrictions of the attached license.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace GASS.Utilities
{
/// <summary>
/// <code>NativeUtilities</code> wraps several necessary native functions
/// from Win32 API and useful macros that exist when programming in C/C++
/// languages.
/// </summary>
class NativeUtilities
{
/// <summary>
/// Returns the HRESULT associated with the given error code.
/// </summary>
/// <param name="errorCode">Error code received by general WINAPI
/// functions.</param>
/// <returns>The HRESULT associated with the given error code.</returns>
public static int HResultFromWin32(int errorCode)
{
return (errorCode <= 0 ? errorCode : CalculateHResult(errorCode));
}
/// <summary>
/// Used to calculate the HRESULT number associated with the given
/// WIN32 error code.
/// </summary>
/// <param name="code">Win32 error code.</param>
/// <returns>The HRESULT number associated with the given WIN32 error
/// code.</returns>
private static int CalculateHResult(int code)
{
uint number = (uint)code;
unchecked
{
return (int)((number & 0x0000FFFF) | (FACILITY_WIN32 << 16) |
0x80000000);
}
}
/// <summary>
/// Constant taken from winerror.h.
/// </summary>
private const int FACILITY_WIN32 = 7;
/// <summary>
/// Win32 API <code>CloseHandle</code> function.
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
[DllImport("coredll.dll", SetLastError=true)]
public static extern int CloseHandle(IntPtr handle);
/// <summary>
/// Win32 API <code>WaitForSingleObject</code> function.
/// </summary>
/// <param name="handle"></param>
/// <param name="timeout"></param>
/// <returns></returns>
[DllImport("coredll.dll", SetLastError = true)]
public static extern int WaitForSingleObject(IntPtr handle, int timeout);
}
}
Related examples in the same category