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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds