Click here to Skip to main content
13,864,782 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 7 Jul 2016
Licenced CPOL

GetLastError Error Codes

Rate this:
Please Sign up or sign in to vote.
GetLastError error codes


I've reorganized the win32 error code to Visual Basic enum values, and shared this code with you in this small tip.

Using the Code

Public Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Integer

The returns Integer value from the GetLastError can be converted to the ErrorCodes. These ErrorCodes can be found from MSDN pages here and here:

''' <summary>
''' This article lists the error codes you may encounter in Windows NT. For the remaining error codes, 
''' please see the following article(s) in the Microsoft Knowledge Base:
''' + [155011]( Error Codes in Windows NT Part 1 of 2
''' + [155012]( Error Codes in Windows NT Part 2 of 2
''' (<see cref="GetLastError"/>???????)
''' </summary>
Public Enum ErrorCodes As Integer
    ' ......
End Enum

All of the error codes enum values can be found in the zip attachment at the top of this tip. Here is the wrapper function:

''' <summary>
''' Retrieves the calling thread's last-error code value. The last-error code is maintained on a 
''' per-thread basis. 
''' Multiple threads do not overwrite each other's last-error code.
''' </summary>
''' <returns>
''' The return value is the calling thread's last-error code.
''' The Return Value section of the documentation for each function that sets the last-error code 
''' notes the conditions under which the function sets the last-error code. Most functions that 
''' set the thread's last-error code set it when they fail. However, some functions also set the 
''' last-error code when they succeed. If the function is not documented to set the last-error code, 
''' the value returned by this function is simply the most recent last-error code to have been set; 
''' some functions set the last-error code to 0 on success and others do not.
''' </returns>
''' <remarks>
''' Functions executed by the calling thread set this value by calling the SetLastError function. 
''' You should call the GetLastError function immediately when a function's return value indicates 
''' that such a call will return useful data. 
''' That is because some functions call SetLastError with a zero when they succeed, wiping out the 
''' error code set by the most recently failed function.
''' To obtain an error string for system error codes, use the FormatMessage function. For a complete 
''' list of error codes provided by the operating system, see System Error Codes.
''' The error codes returned by a function are Not part of the Windows API specification And can 
''' vary by operating system Or device driver. For this reason, we cannot provide the complete list 
''' of error codes that can be returned by each function. There are also many functions whose 
''' documentation does Not include even a partial list of error codes that can be returned.
''' Error codes are 32-bit values (bit 31 Is the most significant bit). Bit 29 Is reserved For 
''' application-defined Error codes; no system Error code has this bit Set. If you are defining an 
''' Error code For your application, Set this bit To one. That indicates that the Error code has 
''' been defined by an application, And ensures that your Error code does Not conflict With any 
''' Error codes defined by the system.
''' To convert a system error into an HRESULT value, use the HRESULT_FROM_WIN32 macro.
''' </remarks>
Public Function GetLastErrorCode() As ErrorCodes
    Return CType(GetLastError, ErrorCodes)
End Function


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mr. xieguigang 谢桂纲
Student 中国南方微生物资源利用中心(SMRUCC)
China China
He is good and loves VisualBasic!


You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 7 Jul 2016
Article Copyright 2016 by Mr. xieguigang 谢桂纲
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid