LastDllError 属性(Err 对象)

更新:2007 年 11 月

返回调用动态链接库 (DLL) 所产生的系统错误代码。只读。

ReadOnly Property LastDllError() As Integer

备注

LastDllError 属性只应用于从 Visual Basic 代码进行的 DLL 调用。当进行此类调用时,所调用的函数通常返回指示成功或失败的代码,并且填充 LastDllError 属性。检查 DLL 函数的文档以确定指示成功或失败的返回值。无论何时返回失败代码,Visual Basic 应用程序都应立即检查 LastDllError 属性。设置 LastDllError 时不引发任何异常。

说明:

LinkDemand 保护 LastDllError 属性,这有助于防止不受信任的代码对它进行访问。但是,LinkDemand 只要求直接调用方具有 UnmanagedCode 权限。如果您的代码可从部分受信任的代码调用,则未经验证就公开 LastDllError 的值将存在安全风险。

有关如何使用 LinkDemand 成员的重要限制,请参见 Demand 和 LinkDemand。有关权限的更多信息,请参见 SecurityPermission代码访问权限

示例

下面的示例演示如何在调用 Windows API 中的函数之后使用 LastDllError 属性。PrintWindowCoordinates 过程采用窗口的句柄,并调用 GetWindowRect 函数。GetWindowRect 使用组成窗口的矩形的边的长度填充 RECT 数据结构。传递无效句柄时,会发生错误,错误号可以通过 LastDllError 属性获取。

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

智能设备开发人员说明

此属性始终返回零。

要求

命名空间:Microsoft.VisualBasic

**模块:**ErrObject

**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

请参见

概念

Demand 和 LinkDemand

参考

Err 对象 (Visual Basic)

Declare 语句

Description 属性(Err 对象)

ErrorToString 函数

HelpContext 属性(Err 对象)

HelpFile 属性(Err 对象)

Number 属性(Err 对象)

Source 属性(Err 对象)