/*
* 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
{
///
/// NativeUtilities
wraps several necessary native functions
/// from Win32 API and useful macros that exist when programming in C/C++
/// languages.
///
class NativeUtilities
{
///
/// Returns the HRESULT associated with the given error code.
///
/// Error code received by general WINAPI
/// functions.
/// The HRESULT associated with the given error code.
public static int HResultFromWin32(int errorCode)
{
return (errorCode <= 0 ? errorCode : CalculateHResult(errorCode));
}
///
/// Used to calculate the HRESULT number associated with the given
/// WIN32 error code.
///
/// Win32 error code.
/// The HRESULT number associated with the given WIN32 error
/// code.
private static int CalculateHResult(int code)
{
uint number = (uint)code;
unchecked
{
return (int)((number & 0x0000FFFF) | (FACILITY_WIN32 << 16) |
0x80000000);
}
}
///
/// Constant taken from winerror.h.
///
private const int FACILITY_WIN32 = 7;
///
/// Win32 API CloseHandle
function.
///
///
///
[DllImport("coredll.dll", SetLastError=true)]
public static extern int CloseHandle(IntPtr handle);
///
/// Win32 API WaitForSingleObject
function.
///
///
///
///
[DllImport("coredll.dll", SetLastError = true)]
public static extern int WaitForSingleObject(IntPtr handle, int timeout);
}
}