Err.LastDLLError Property

   
Err.LastDLLError Property

Class

Microsoft.VisualBasic.ErrObject

Syntax

 Err.LastDLLError 

Description

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

Rules at a Glance

  • Only direct calls to a Windows system DLL from VB code will 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 does not itself raise an error within your VB program. As a result, the Err object's Number, Description, and Source properties are not filled.

Programming Tips and Gotchas

  • The LastDLLError property can be changed by VB at any time, so it is important to save the value in an independent variable as soon as possible.

  • The LastDLLError property is only used by system DLLs, such as kernel32.dll . Therefore, errors that occur within DLLs you may have created will not cause an error code to be assigned to the property.

  • 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 Win32 API FormatMessage function to return the actual Windows error message string from within Kernel32.DLL , which incidentally will also be in the correct language. The following is a brief example that you can use in your applications to display the actual Windows error description:

     Module modMain Declare Function FormatMessage Lib "kernel32" _         Alias "FormatMessageA" ( _         ByVal dwFlags as Integer, ByRef lpSource As Integer, _         ByVal dwMessageId As Integer, _         ByVal dwLanguageId As Integer, _         ByVal lpBuffer As String, ByVal nSize As Integer, _         By Ref Arguments As Integer) As Integer Public Const FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000 Public Const FORMAT_MESSAGE_IGNORE_INSERTS As Integer = &H200 Function apiErrDesc (iErrCode As Integer) As String    Dim sErrDesc As String = Space(256)    Dim iReturnLen, lpNotUsed As Integer    iReturnLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM _                 Or FORMAT_MESSAGE_IGNORE_INSERTS, _                 lpNotUsed, iErrCode, 0&, sErrDesc, _                 Len(sErrDesc), lpNotUsed)    if iReturnLen > 0 Then       apiErrDesc = Left(sErrDesc, iReturnLen)    End If  End Function End Module 

    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 0 to denote a successful function call, and others return 0 to denote an unsuccessful call. You should also note that some API functions do not appear to set the LastDLLError property. In most cases, these are functions that return an error code. You could therefore modify the previous snippet 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

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net