DllImportAttribute.SetLastError 欄位

定義

指出自屬性方法傳回之前,被呼叫端是否呼叫 SetLastError Windows API 函式。Indicates whether the callee calls the SetLastError Windows API function before returning from the attributed method.

public: bool SetLastError;
public bool SetLastError;
val mutable SetLastError : bool
Public SetLastError As Boolean 

欄位值

範例

在某些情況下, Visual Basic 開發人員DllImportAttribute使用, 而不是Declare使用語句, 在 managed 程式碼中定義 DLL 函式。In some cases, Visual Basic developers use the DllImportAttribute, instead of using the Declare statement, to define a DLL function in managed code. SetLastError設定欄位是其中一種情況。Setting the SetLastError field is one of those cases.

[DllImport("user32.dll", SetLastError = true)]
int MessageBoxA(IntPtr hWnd, String^ Text,
    String^ Caption, unsigned int Type);
internal static class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern int MessageBoxA(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
Friend Class NativeMethods
    <DllImport("user32.dll", SetLastError:=True)>
    Friend Shared Function MessageBoxA(hWnd As IntPtr, lpText As String,
        lpCaption As String, uType As UInteger) As Integer
    End Function
End Class

備註

true表示被呼叫者將會呼叫SetLastError, false否則為。true to indicate that the callee will call SetLastError; otherwise, false. 預設為 falseThe default is false.

執行時間封送GetLastError處理器會呼叫並快取傳回的值, 以防止其他 API 呼叫覆寫它。The runtime marshaler calls GetLastError and caches the value returned to prevent it from being overwritten by other API calls. 您可以藉由呼叫GetLastWin32Error來捕獲錯誤碼。You can retrieve the error code by calling GetLastWin32Error.

適用於

另請參閱