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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • As a result of corporate expansions, mergers, and acquisitions, the teams and technologies that support an organization's IT service management (ITSM) practices can over time become somewhat dispersed. Supporting an organization's strategic objectives, and providing consistent and quality IT support is essential, but this can be challenging where disconnected support environments exist. While physically centralizing support is not always possible, nor is it always desirable, delivering a consistent and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds