ErrObject.LastDllError Property


Gets a system error code produced by a call to a dynamic-link library (DLL).

 property int LastDllError { int get(); };
public int LastDllError { get; }
[get: System.Security.SecurityCritical]
public int LastDllError { get; }
member this.LastDllError : int
Public ReadOnly Property LastDllError As Integer

Property Value

A system error code produced by a call to a dynamic-link library (DLL).



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

    "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
  End If
End Sub


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 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.

Note For smart devices this property always returns zero.

Applies to

See also