Attribute VB_Name = "modGetLastError1" Option Explicit '============================================================================================================= ' ' modGetLastError1 Module ' ----------------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : February 1, 2000 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : Nothing ' ' Description : This module contains one function that is designed to make it easy to either find if an ' error occured in the last Windows API called, or display an error message for a specified ' error number. ' ' Example Use : ' ' Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long ' Private Sub Form_Load() ' On Error Resume Next ' SetForegroundWindow Me.hwnd ' If GetLastErr_Msg(, "SetForegroundWindow", , , True) = True Then ' Unload Me ' End If ' End Sub ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= ' Constants - General Public Const MAX_PATH = 260 ' Constants - FormatMessage.dwFlags Public Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100 ' Specifies that the lpBuffer parameter is a pointer to a PVOID pointer, and that the nSize parameter specifies the minimum number of TCHARs to allocate for an output message buffer. The function allocates a buffer large enough to hold the formatted message, and places a pointer to the allocated buffer at the address specified by lpBuffer. The caller should use the LocalFree function to free the buffer when it is no longer needed. Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200 ' Specifies that insert sequences in the message definition are to be ignored and passed through to the output buffer unchanged. This flag is useful for fetching a message for later formatting. If this flag is set, the Arguments parameter is ignored. Public Const FORMAT_MESSAGE_FROM_STRING = &H400 ' Specifies that lpSource is a pointer to a null-terminated message definition. The message definition may contain insert sequences, just as the message text in a message table resource may. Cannot be used with FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM. Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800 ' Specifies that lpSource is a module handle containing the message-table resource(s) to search. If this lpSource handle is NULL, the current process's application image file will be searched. Cannot be used with FORMAT_MESSAGE_FROM_STRING. Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 ' Specifies that the function should search the system message-table resource(s) for the requested message. If this flag is specified with FORMAT_MESSAGE_FROM_HMODULE, the function searches the system message table if the message is not found in the module specified by lpSource. Cannot be used with FORMAT_MESSAGE_FROM_STRING. If this flag is specified, an application can pass the result of the GetLastError function to retrieve the message text for a system-defined error. Public Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 ' Specifies that the Arguments parameter is not a va_list structure, but instead is just a pointer to an array of values that represent the arguments. ' Win32 API Declarations Public Declare Sub SetLastError Lib "KERNEL32" (ByVal dwErrCode As Long) Public Declare Function GetLastError Lib "KERNEL32" () As Long Public Declare Function FormatMessage Lib "KERNEL32" Alias "FormatMessageA" (ByVal dwFlags As Long, ByRef lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, ByRef Arguments As Long) As Long '============================================================================================================= ' GetLastErr_Msg ' ' Purpose : ' Function that gets the last error caused by Windows API's. This only works ' with functions that use the GetLastError function to return an error code. ' Not all Windows API's do. ' ' If no error has occured, no message is displayed. ' ' Param Use ' ------------------------------------ ' ErrorNumber Optional. Error number to display. If this is set to ' zero, then the GetLastError API is called to see if any ' errors have occured. If no error have occured, the ' function exits. ' LastAPICalled Optional. If the "ShowErrors" parameter is set to TRUE, ' this is used to display an error dialog and tell the user ' which API caused the problem. ' Return_ErrNum Optional. This returns the number of the error that just ' occured. If the "ErrorNumber" parameter wasn't specified ' but an error was found by this function, the error number ' is returned here. ' Return_ErrDesc Optional. This returns the description of the last error ' that occured. ' ShowErrors Optional. If this parameter is set to TRUE, an error ' dialog is displayed with the error number and description ' for the user to see. ' ' Return ' ------ ' If no error occured, no message is displayed & function returns FALSE. ' If an error occured, an error message is displayed & the function returns TRUE. ' '============================================================================================================= Public Function GetLastErr_Msg(Optional ByVal ErrorNumber As Long, _ Optional ByVal LastAPICalled As String = "last", _ Optional ByRef Return_ErrNum As Long, _ Optional ByRef Return_ErrDesc As String, _ Optional ByVal ShowErrors As Boolean = False) As Boolean On Error Resume Next ' Clear the return values first Return_ErrNum = 0 Return_ErrDesc = "" ' If no error message is specified then check for one If ErrorNumber = 0 Then ErrorNumber = GetLastError If ErrorNumber = 0 Then GetLastErr_Msg = False Exit Function End If End If ' Allocate a buffer for the error description Return_ErrDesc = String(MAX_PATH, 0) ' Get the error description FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrorNumber, 0, Return_ErrDesc, MAX_PATH, 0 Return_ErrNum = ErrorNumber Return_ErrDesc = Left(Return_ErrDesc, InStr(Return_ErrDesc, Chr(0)) - 1) If Right(Return_ErrDesc, Len(vbCrLf)) = vbCrLf Then Return_ErrDesc = Left(Return_ErrDesc, Len(Return_ErrDesc) - Len(vbCrLf)) End If ' Display the error message If ShowErrors = True Then MsgBox "An error occured while calling the " & LastAPICalled & " Windows API function." & Chr(13) & "Below is the error information:" & Chr(13) & Chr(13) & "Error Number = " & CStr(ErrorNumber) & Chr(13) & "Error Description = " & Return_ErrDesc, vbOKOnly + vbExclamation, " Windows API Error" End If GetLastErr_Msg = True ' Set the last error to 0 (no error) so next time through it doesn't report the same error twice SetLastError 0 End Function