Convert Win32 Error
Contents |
Chen - MSFTNovember 3, 200632 0 0 0 Everybody knows that you can use the HRESULT_FROM_WIN32 macro to convert a Win32 error code to an HRESULT, but how do you do the reverse? hresult_from_win32 Let's look at the definition of HRESULT_FROM_WIN32: #define HRESULT_FROM_WIN32(x) \ ((HRESULT)(x) <= 0 hresult to error code ? ((HRESULT)(x)) \ : ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000))) If the value is less than
Convert Hresult To String
or equal to zero, then the macro returns the value unchanged. Otherwise, it takes the lower sixteen bits and combines them with FACILITY_WIN32 and SEVERITY_ERROR. How do you reverse this process? How
Make_hresult
do you write the function WIN32_FROM_HRESULT? It's impossible to write that function since the mapping provided by the HRESULT_FROM_WIN32 function is not one-to-one. I leave as an execise to draw the set-to-set mapping diagram from DWORD to HRESULT. (Original diagram removed since people hate VML so much, and I can't use SVG since it requies XHTML.) If you do it correctly, you'll have a ntstatus to hresult single line which maps 0to S_OK, and a series of blocks that map blocks of 65536 error codes into the same HRESULT space. Notice that the values in the range 1 through 0x7FFFFFFFF are impossible results from the HRESULT_FROM_WIN32 macro. Furthermore, values in the range 0x80070000 through 0x8007FFFF could have come from quite a few original Win32 codes; you can't pick just one. But let's try to write the reverse function anyway: BOOL WIN32_FROM_HRESULT(HRESULT hr, OUT DWORD *pdwWin32) { if ((hr & 0xFFFF0000) == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32)) { // Could have come from many values, but we choose this one *pdwWin32 = HRESULT_CODE(hr); return TRUE; } if (hr == S_OK) { *pdwWin32 = HRESULT_CODE(hr); return TRUE; } // otherwise, we got an impossible value return FALSE; } Of course, we could have been petulant and just written BOOL WIN32_FROM_HRESULT_alternate(HRESULT hr, OUT DWORD *pdwWin32) { if (hr < 0) { *pdwWin32 = (DWORD)hr; return TRUE; } // otherwise, we got an impossible value return FALSE; } because the HRESULT_FROM_WIN32 macro is idempotent: HRESULT_FROM_WIN32(HRESULT_FROM_WIN32(x)) == HRESULT_FROM_WIN32(x). Therefore you would be technically correct if you declared that the "inverse" function was trivial. But in practice, pe
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired
Rtlntstatustodoserror
content Samples We’re sorry. The content you requested has been removed. You’ll be win32 error codes auto redirected in 1 second. Active Directory Service Interfaces Active Directory Service Interfaces Reference ADSI Error Codes ADSI Error Codes hresult values Win32 Error Codes for ADSI Win32 Error Codes for ADSI Win32 Error Codes for ADSI Generic COM Error Codes Generic ADSI Error Codes Win32 Error Codes for ADSI Win32 Error Codes Win32 Error https://blogs.msdn.microsoft.com/oldnewthing/20061103-07/?p=29133 Codes for ADSI 2.0 LDAP Error Codes for ADSI ADSI Extended Error Messages TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Win32 Error Codes for ADSI Standard Win32 error codes are also used to return ADSI error messages. Specifically, the ADSI LDAP provider maps https://msdn.microsoft.com/en-us/library/aa746529(v=vs.85).aspx all the LDAP error codes to Win32 error codes. The HRESULT values of these error codes are of the 0x8007XXXX format, where the last four hexadecimal digits, XXXX, corresponds to the DWORD values of the appropriate Win32 error code. For example, the ADSI error value 0x80072020 gives the Win32 error value of 0x2020 in hexadecimal or 8224 in decimal. To convert the HRESULT value of an ADSI error code, returned by your application, to the corresponding the Win32 error DWORD value, as defined in the header files above, use the following procedure. Most of the Win32 error codes for ADSI are defined in Winerror.h or Lmerr.h. The error values are listed as decimal values in these files. To convert the HRESULT value of an ADSI error code to the corresponding the Win32 error DWORD value Convert the HRESULT value to a hexadecimal number if starting with a decimal value as you may get from a Visual Basic application. Drop the 0x8007 part produce the remainder. Convert the remainder to a decimal number. Look up the decimal remainder in Winerror.h. If not found in Winerror.h, subtract 2100 from the decimal remainder and look up the resul
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community https://msdn.microsoft.com/en-us/library/cc231199.aspx Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto http://ntcoder.com/bab/2007/06/26/convert-win32-error-codes-to-hresult/ redirected in 1 second. Export (0) Print Expand All MSDN Library Open Specifications Protocols Windows Protocols References [MS-ERREF]: Windows Error Codes 2 Structures 2.1 HRESULT 2.2 win32 error Win32 Error Codes 2.3 NTSTATUS 2.4 LDAP Error to Win32 Error Mapping Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. 2.2 Win32 Error Codes All Win32 error codes MUST be in the range 0x0000 to 0xFFFF, although Win32 error codes can be convert win32 error used both in 16-bit fields (such as within the HRESULT type specified in section 2.1) as well as 32-bit fields. Most values also have a default message defined, which can be used to map the value to a human-readable text message; when this is done, the Win32 error code is also known as a message identifier. The following table specifies the values and corresponding meanings of the Win32 error codes. Vendors SHOULD NOT assign other meanings to these values, to avoid the risk of a collision in the future. This document provides the common usage details of the Win32 error codes; individual protocol specifications provide expanded or modified definitions. Note In the following descriptions, a percentage sign followed by one or more alphanumeric characters (for example, "%1" or "%hs") indicates a variable that will be replaced by text at the time the value is returned. Win32 error codes Description 0x00000000 ERROR_SUCCESS The operation completed successfully. 0x00000000 N
Share What You Found: Use HRESULT_FROM_WIN32. Related Share What You Found: Posted by Nibu Thomas at 8:21 am Tagged with: HRESULT, HRESULT From Win32 error code, WIN32 2 Responses to "Convert win32 error codes to HRESULT" mithun says: October 14, 2009 at 12:12 pm how to convert the hresult code to error description Reply Nibu Thomas says: October 14, 2009 at 2:08 pm Hi Mithun, You can type @err,hr into the debug watch window to see a description of last error. Also there is a API called FormatMessage, which helps in translating an error code to it's description. Reply Leave a Reply Cancel reply How to create SafeArray of BSTR's? Easy way to display Gif's, Ico's, JPEG's and BMP's on your window Follow Me Subscribe to this blogRSS - PostsRSS - Comments Donation shows Appreciation… Custom Search Me Me Visitors Ads Recent Posts How to set ServerRender property of a SharePoint WebPart via Powershell? How to restore deleted default farm and web application level SharePoint timer jobs? SharePoint 2013 August 2015 CU Bug How to manipulate strings using PowerShell? How to get rid of orphaned features in a SharePoint farm using PowerShell? Tags.net ActiveX ATL CComboBox CDialog Clipboard COM Console API Console Application cout CreateProcess CString Debug Debugger Debugging dumpbin Feature pack GDI Interview questions MVP MVP Summit MVP Summit 2008 Nibu Nibu babu thomas Nibu Thomas PowerShell Ray Ozzie Redmond SetBkMode SetWindowPos Sharepoint ShGetFileInfo Steve Ballmer stl Summit VC8 vector Visual studio Visual Studio 2013 VS2005 VS2008 VS2013 windbg Windows Windows Phone Articles About Nibu MFC Feature Pack Tutorial - Part 1 - Getting started MFC Feature Pack Tutorial - Part 2 - CMFCPropertySheet MFC Feature Pack Tutorial - Part 3 - CMFCPropertyGridCtrl What’s new in Visual Studio 2012 Solution Explorer © 2012 bits and bytes Suffusion theme by Sayontan Sinha