LastDllError 屬性 (Err 物件)

更新:2007 年 11 月

傳回呼叫動態連結程式庫 (DLL) 產生的系統錯誤碼。唯讀。

ReadOnly Property LastDllError() As Integer

備註

LastDllError 屬性只適用於從 Visual Basic 程式碼呼叫 DLL。呼叫時,呼叫的函式通常會傳回表示成功或失敗的代碼,此時也會填入 LastDllError 屬性。請查閱 DLL 函式的文件,以判斷指示成功或失敗的傳回值。傳回失敗代碼時,Visual Basic 應用程式應會立即檢查 LastDllError 屬性。當設定 LastDllError 屬性時,不會引發任何例外狀況。

注意事項:

LastDllError 屬性受到 LinkDemand 保護,有助於避免不受信任的程式碼存取。但是,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

智慧型裝置開發人員注意事項

此屬性永遠會傳回零。

需求

命名空間 (Namespace)︰Microsoft.VisualBasic

**模組︰**ErrObject

組件:Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

概念

Demand 和 LinkDemand 的比較

參考

Err 物件 (Visual Basic)

Declare 陳述式

Description 屬性 (Err 物件)

ErrorToString 函式

HelpContext 屬性 (Err 物件)

HelpFile 屬性 (Err 物件)

Number 屬性 (Err 物件)

Source 屬性 (Err 物件)