Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

GetLastError Error Codes

5.00/5 (2 votes)
7 Jul 2016CPOL 15K   155  
GetLastError error codes

Introduction

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

VB.NET
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:

VB.NET
''' <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](https://support.microsoft.com/en-us/kb/155011) Error Codes in Windows NT Part 1 of 2
''' + [155012](https://support.microsoft.com/EN-US/kb/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:

VB.NET
''' <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

License

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