Marshal.GetLastWin32Error 方法

定義

傳回使用平台叫用 (已設定 SetLastError 旗標) 來呼叫的最後 Unmanaged 函式所傳回的錯誤碼。

public:
 static int GetLastWin32Error();
[System.Security.SecurityCritical]
public static int GetLastWin32Error ();
public static int GetLastWin32Error ();
[<System.Security.SecurityCritical>]
static member GetLastWin32Error : unit -> int
static member GetLastWin32Error : unit -> int
Public Shared Function GetLastWin32Error () As Integer

傳回

藉由呼叫 Win32 SetLastError 函式設定的最後錯誤碼。

屬性

範例

下列範例會呼叫 GetLastWin32Error 方法。 此範例會先示範呼叫沒有錯誤的方法,然後示範呼叫方法並出現錯誤。

using System;
using System.Runtime.InteropServices;

internal class Win32
{
    // Use DllImportAttribute to inport the Win32 MessageBox
    // function.  Set the SetLastError flag to true to allow
    // the function to set the Win32 error.
    [DllImportAttribute("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);
}

class Program
{

    static void Run()
    {
        // Call the MessageBox with normal parameters.

        Console.WriteLine("Calling Win32 MessageBox without error...");

        Win32.MessageBox(new IntPtr(0), "Press OK...", "Press OK Dialog", 0);

        // Get the last error and display it.
        int error = Marshal.GetLastWin32Error();

        Console.WriteLine("The last Win32 Error was: " + error);

        // Call the MessageBox with an invalid window handle to
        // produce a Win32 error.

        Console.WriteLine("Calling Win32 MessageBox with error...");

        Win32.MessageBox(new IntPtr(123132), "Press OK...", "Press OK Dialog", 0);

        // Get the last error and display it.

        error = Marshal.GetLastWin32Error();

        Console.WriteLine("The last Win32 Error was: " + error);
    }

    static void Main(string[] args)
    {
        Run();
    }
}
// This code example displays the following to the console:
//
// Calling Win32 MessageBox without error...
// The last Win32 Error was: 0
// Calling Win32 MessageBox with error...
// The last Win32 Error was: 1400
Imports System.Runtime.InteropServices

Module Win32
    ' Use DllImportAttribute to inport the Win32 MessageBox
    ' function.  Set the SetLastError flag to true to allow
    ' the function to set the Win32 error.
    <DllImportAttribute("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Function MessageBox(ByVal hwnd As IntPtr, ByVal text As String, ByVal caption As String, ByVal type As UInt32) As Integer
    End Function

End Module

Module Program


    Sub Run()


        ' Call the MessageBox with normal parameters.

        Console.WriteLine("Calling Win32 MessageBox without error...")

        Win32.MessageBox(New IntPtr(0), "Press OK...", "Press OK Dialog", 0)

        ' Get the last error and display it.
        Dim errorVal As Integer

        errorVal = Marshal.GetLastWin32Error()

        Console.WriteLine("The last Win32 Error was: " + errorVal)

        ' Call the MessageBox with an invalid window handle to
        ' produce a Win32 error.

        Console.WriteLine("Calling Win32 MessageBox with error...")

        Win32.MessageBox(New IntPtr(123132), "Press OK...", "Press OK Dialog", 0)

        ' Get the last error and display it.

        errorVal = Marshal.GetLastWin32Error()

        Console.WriteLine("The last Win32 Error was: " + errorVal)

    End Sub

    Sub Main(ByVal args() As String)

        Run()

    End Sub

End Module

' This code example displays the following to the console: 
'
' Calling Win32 MessageBox without error...
' The last Win32 Error was: 0
' Calling Win32 MessageBox with error...
' The last Win32 Error was: 1400

備註

在 Windows 系統上, GetLastWin32Error 從 Kernel32.DLL 公開 Win32 GetLastError 函式。 這個方法存在,因為進行直接平臺叫用呼叫 GetLastError 以取得這項資訊並不可靠。 如果您想要存取這個錯誤碼,您必須呼叫 GetLastWin32Error ,而不是撰寫自己的平臺叫用定義 GetLastError ,並呼叫它。 Common Language Runtime 可以對覆寫 GetLastError 作業系統所維護 的 API 進行內部呼叫。

只有在將 套用 System.Runtime.InteropServices.DllImportAttribute 至方法簽章並將欄位 true 設定 DllImportAttribute.SetLastError 為 時,才能使用此方法來取得錯誤碼。 此程式會根據所使用的來來源語言而有所不同:C# 和 C++ 預設為 false ,但 Declare Visual Basic 中的 語句為 true

在 .NET Core 上,方法的行為 GetLastWin32Error 有差異,當 為 trueDllImportAttribute.SetLastError ,.NET Framework。 在.NET Framework上, GetLastWin32Error 方法可以從下一個 P/Invoke 呼叫保留錯誤資訊。 在 .NET Core 上,錯誤資訊會在 P/Invoke 呼叫之前清除,而且 GetLastWin32Error 只代表上一個方法呼叫的錯誤資訊。

在 .NET 6 和更新版本上,這個方法的功能相當於 GetLastPInvokeError ,其命名為更能反映 API 的意圖及其跨平臺本質。 GetLastPInvokeError 應該優先于 GetLastWin32Error

適用於

另請參閱