GetLastError Function

Declare Function GetLastError Lib "kernel32.dll" () As Long

GetLastError returns the error code returned by the last API function called. Most API functions merely return a number saying if an error occured, but not what kind of error. This function will return a universal error code identifying the type of error that last occured. Note that most functions set the code to 0 (success) if the function completes successfully, erasing the previous error code. Therefore, be sure to check this error code immediately after an error is found.

Example:

' Demonstrate catching an invalid handle error
Dim retval As Long  ' return value of function
Dim errorcode As Long  ' error code

' Make an invalid call to the following function by giving it an invalid handle
retval = CloseHandle(-1)  ' there is no handle -1!
If retval = 0 Then  ' the return value will be 0 if an error occured
  errorcode = GetLastError()  ' find the error code
  If errorcode = 6 Then Debug.Print "ERROR: Invalid Handle Specified"  ' error 6 = invalid handle
End If

Related Call: CommDlgExtendedError
Category: Errors
Back to the index.


Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/g/getlasterror.html