|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
LastDllError Property (Err Object)
Returns a system error code produced by a call to a dynamic-link library (DLL). Read-only.
The LastDllError property applies only to DLL calls made from Visual Basic code. When such a call is made, the called function usually returns a code indicating success or failure, and the LastDllError property is filled. Check the documentation for the DLL's functions to determine the return values that indicate success or failure. Whenever the failure code is returned, the Visual Basic application should immediately check the LastDllError property. No exception is raised when the LastDllError property is set.
The LastDllError property requires unmanaged code permission, which may affect its execution in partial-trust situations. For more information, seeand .
The following example shows how you can use the LastDllError property after calling a function in the Windows API. The PrintWindowCoordinates procedure takes a handle to a window and calls the GetWindowRect function. GetWindowRect fills the RECT data structure with the lengths of the sides of the rectangle that make up the window. If you pass an invalid handle, an error occurs, and the error number is available through the LastDllError property.
Declare Function GetWindowRect Lib "user32" _ (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer ... Public Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure ... Const ERROR_INVALID_WINDOW_HANDLE As Long = 1400 Const ERROR_INVALID_WINDOW_HANDLE_DESCR As String = _ "Invalid window handle."
Private Sub PrintWindowCoordinates(ByVal hwnd As Integer) ' Prints left, right, top, and bottom positions ' of a window in pixels. Dim rectWindow As RECT ' Pass in window handle and empty the data structure. ' If function returns 0, an error occurred. If GetWindowRect(hwnd, rectWindow) = 0 Then ' Check LastDllError and display a dialog box if the error ' occurred because an invalid handle was passed. If Err.LastDllError = ERROR_INVALID_WINDOW_HANDLE Then MsgBox(ERROR_INVALID_WINDOW_HANDLE_DESCR, Title:="Error!") End If Else Debug.Print(rectWindow.Bottom) Debug.Print(rectWindow.Left) Debug.Print(rectWindow.Right) Debug.Print(rectWindow.Top) End If End Sub
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)