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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Windows » Windows APIScreenshots 
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 <= ? 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.