LastDllError Property (Err Object)
Returns a system error code produced by a call to a dynamic-link library (DLL). Read-only.
ReadOnly Property LastDllError() As Integer
Remarks
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.
Note
The LastDllError property is protected by LinkDemand, which helps prevent it from being accessed from untrusted code. However, LinkDemand requires only the immediate caller to have UnmanagedCode permission. If your code can be called from partially trusted code, exposing the value of LastDllError without validation is a security risk.
For important limitations about how to use the LinkDemand member, see Demand vs. LinkDemand. For more information about permissions, see SecurityPermission and Code Access Permissions.
Example
The following example shows how you can use the LastDllError property after you call 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
Smart Device Developer Notes
This property always returns zero.
Requirements
Namespace: Microsoft.VisualBasic
**Module:**ErrObject
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
See Also
Concepts
Reference
Description Property (Err Object)
HelpContext Property (Err Object)