Err.LastDLLError Property

Syntax

Err.LastDLLError

Description

A read-only property containing a long data type representing a system error produced within a DLL called from within a VB program.

Rules at a Glance

  • Only direct calls to a Windows system DLL from VB code assign a value to LastDLLError.

  • The value of the LastDLLError property depends upon the particular DLL being called. Your code must be able to handle the various codes that can be returned by the DLL you are calling.

  • Don't forget that a failed DLL call doesn't itself raise an error within your VB program. As a result, the Err object's Number, Description, and Source properties aren't filled.

Programming Tips and Gotchas

  • The LastDLLError property is used only by system DLLs, such as kernel32.dll, and therefore errors that occur within DLLs you may have created in VB won't be assigned.

  • Obtaining accurate documentation about the return values of system DLLs can be a challenging experience. Most useful information can be found by studying the API documentation for Visual C++. However, you can use the Windows API FormatMessage to return the actual Windows error message string from within Kernel32.DLL, which incidentally is also in the correct language. The following is a brief example you can use in your applications to display the actual Windows error description:

    Option Explicit
    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
    Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
    
    Function apiErrDesc(lErrCode As Long) As String
    
       Dim sErrDesc As String
       Dim lReturnLen As Long
       Dim lpNotUsed As Long
        
       sErrDesc = String(256, 0)
     lReturnLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM _
                  Or FORMAT_MESSAGE_IGNORE_INSERTS, _
                  lpNotUsed, lErrCode, 0&, sErrDesc, _
                  Len(sErrDesc), ByVal lpNotUsed)
                          
       If lReturnLen > 0 Then
          apiErrDesc = Left$(sErrDesc, lReturnLen)
       End If
        
    End Function

    Here's a snippet demonstrating how you can use this utility function:

    lReturn = SomeAPICall(someparams)
    If lReturn  <> 0 then
       Err.Raise Err.LastDLLError & vbObjectError, _
                 "MyApp:Kernel32.DLL", _
                  apiErrDesc(Err.LastDLLError)
    End If

    Note that some API calls return to denote a successful function call; others return to denote an unsuccessful call. You should also note that some API functions don't appear to set the LastDLLError property. In most cases, these are functions that return an error code. You could therefore modify the snippet above to handle these cases:

    lReturn = SomeAPICall(someparams)
    If lReturn  <> 0 then
       If Err.LastDLLError <> 0 Then
          Err.Raise Err.LastDLLError & vbObjectError, _
                    "MyApp:Kernel32.DLL", _
                    apiErrDesc(Err.LastDLLError)
       Else
          Err.Raise lReturn  & vbObjectError, _
                    "MyApp:Kernel32.DLL", _
                    apiErrDesc(lReturn)
       End If
    End If

See Also

Err Object, Chapter 6
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset