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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • It's hardly surprising that half of small businesses fail within the first 1-5 years. It's not easy to launch a new product, single-handedly manage everything from IT to accounting, fend off the competition, and grow a customer base – all at the same time – even with a great concept. Offering awesome customer service can make the difference between a startup that flies and a startup that dies. Read this white paper to learn nine ways customer support can help you beat the competition and grow your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds