Get Error Messages Directly from the System

Here is a routine used to get error messages directly from the system instead of hard-coding them :

Sample :


       Call APIError

       ' *** Or
       Debug.Print ReturnAPIError(53)
       ' *** Return : The network path was not found.

' #VBIDEUtils#**************************************************
' * Programmer Name  : Waty Thierry
' * Web Site	 : www.geocities.com/ResearchTriangle/6311/
' * E-Mail		 : waty.thierry@usa.net
' * Date		 : 12/10/1998
' * Time		 : 20:20
' * Module Name	 : APIError_Module
' * Module Filename  : APIError.bas
' **************************************************************
' * Comments	 :
' * Used to get error messages directly from the
' *   system instead of hard-coding them
' *
' *
' **************************************************************

Option Explicit

Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200

Private Declare Function FormatMessage Lib "kernel32" _ Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _ ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize _ As Long, Arguments As Long) As Long

' *** Status Codes Public Const INVALID_HANDLE_VALUE = -1& Public Const ERROR_SUCCESS = 0&

Public Function ReturnAPIError(ErrorCode As Long) As String ' #VBIDEUtils#*********************************************** ' * Programmer Name : Waty Thierry ' * Web Site : www.geocities.com/ResearchTriangle/6311/ ' * E-Mail : waty.thierry@usa.net ' * Date : 12/10/1998 ' * Time : 20:21 ' * Module Name : APIError_Module ' * Module Filename : APIError.bas ' * Procedure Name : ReturnAPIError ' * Parameters : ' * ErrorCode As Long ' *********************************************************** ' * Comments : ' * Takes an API error number, and returns ' * a descriptive text string of the error ' * ' ***********************************************************

Dim sBuffer As String

' *** Allocate the string, then get the system to ' *** tell us the error message associated with ' *** this error number sBuffer = String(256, 0) FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or _ FORMAT_MESSAGE_IGNORE_INSERTS, 0&, ErrorCode, 0&, sBuffer, Len(sBuffer), 0&

' *** Strip the last null, then the last CrLf pair if it exists sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1) If Right$(sBuffer, 2) = Chr$(13) & Chr$(10) Then sBuffer = Mid$(sBuffer, 1, Len(sBuffer) - 2) End If

ReturnAPIError = sBuffer

End Function

Public Sub APIError() ' #VBIDEUtils#*********************************************** ' * Programmer Name : Waty Thierry ' * Web Site : www.geocities.com/ResearchTriangle/6311/ ' * E-Mail : waty.thierry@usa.net ' * Date : 12/10/1998 ' * Time : 20:35 ' * Module Name : APIError_Module ' * Module Filename : APIError.bas ' * Procedure Name : APIError ' * Parameters : ' *********************************************************** ' * Comments : ' * Takes an API error number, and returns ' * a descriptive text string of the error ' * ' ***********************************************************

Dim sError As String On Error GoTo ERROR_APIError sError = InputBox("Enter the error number", _ "Returns API error") If IsNumeric(sError) = False Then Exit Sub MsgBox ReturnAPIError(CLng(sError)), vbInformation + vbOKOnly, _ "Error n " & sError Exit Sub ERROR_APIError: MsgBox "Error n " & sError & vbCrLf & _ " Invalid error number" & vbCrLf & _ "You have to give another one", _ vbCritical + vbOKOnly, "Error n " & sError End Sub



Comments

  • How to use unlimmited buffer

    Posted by Legacy on 08/27/2002 12:00am

    Originally posted by: yoram

    change lpBuffer to Byref lpBuffer as long
    so
    Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByRef lpBuffer As Long, ByVal nSize As Long, Arguments As Long) As Long

    use th flag
    Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100

    now
    Dim buffer as long
    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_ALLOCATE_BUFFER, _
    0, msgId, 0, buffer, 0, 0

    now you post the string by
    ' Note that this is not vb msgbox but an API function
    MessageBox(0, buffer, Caption, mbType)
    end then
    ' Enother API function
    LocalFree buffer

    Reply
  • More on API errors and Visual basic

    Posted by Legacy on 02/14/2000 12:00am

    Originally posted by: Duncan Jones

    When testing for API errors in VB, do not use the GetLastError() api call as Visual basic itself may perform any number of API functions which will reset this value behind the scenes.
    Fortunately the App object has a "LastDLLError" member in VB5 which you can use instead.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds