Returns the HRESULT associated with the given error code. : Windows API « Windows « C# / C Sharp






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

1.DLL: GetVersionEx
2.Keyboard timer: GetTickCountKeyboard timer: GetTickCount
3.BitBlt
4.Get the Total Free Space on a Drive by using kernel32.dll
5.imports three functions to display the vertical and horizontal size of the screen.
6.MessageBox from user32.dll
7.Get OS Version from kernel32.dll
8.imports the printf function
9.import CreateDirectory and FormatMessage
10.marshals string for unmanaged memory as ANSI.
11.imports the GetModuleHandleW function specifically
12.Set mouse and keyboard hooks.
13.Sound Utils