Bagikan melalui


Marshal.GetLastWin32Error Metode

Definisi

Mengembalikan kode kesalahan yang dikembalikan oleh fungsi terakhir yang tidak dikelola yang dipanggil menggunakan pemanggilan platform yang memiliki SetLastError set bendera.

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

Mengembalikan

Kode kesalahan terakhir yang diatur oleh panggilan ke fungsi Win32 SetLastError .

Atribut

Contoh

Contoh berikut memanggil GetLastWin32Error metode . Contoh pertama menunjukkan pemanggilan metode tanpa ada kesalahan dan kemudian menunjukkan pemanggilan metode dengan kesalahan yang ada.

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

Keterangan

Pada sistem Windows, GetLastWin32Error mengekspos fungsi Win32 GetLastError dari Kernel32.DLL. Metode ini ada karena tidak dapat diandalkan untuk melakukan panggilan platform langsung untuk GetLastError mendapatkan informasi ini. Jika Anda ingin mengakses kode kesalahan ini, Anda harus memanggil GetLastWin32Error alih-alih menulis definisi pemanggilan platform Anda sendiri untuk GetLastError dan memanggilnya. Runtime bahasa umum dapat melakukan panggilan internal ke API yang menimpa yang GetLastError dikelola oleh sistem operasi.

Anda dapat menggunakan metode ini untuk mendapatkan kode kesalahan hanya jika Anda menerapkan ke System.Runtime.InteropServices.DllImportAttribute tanda tangan metode dan mengatur DllImportAttribute.SetLastError bidang ke true. Proses untuk ini bervariasi tergantung pada bahasa sumber yang digunakan: C# dan C++ secara false default, tetapi Declare pernyataan dalam Visual Basic adalah true.

Ada perbedaan perilaku GetLastWin32Error metode pada .NET Core dan .NET Framework kapan DllImportAttribute.SetLastError adalah true. Pada .NET Framework, GetLastWin32Error metode ini dapat menyimpan informasi kesalahan dari satu panggilan P/Panggil ke panggilan berikutnya. Pada .NET Core, informasi kesalahan dihapus sebelum panggilan P/Invoke, dan GetLastWin32Error hanya mewakili informasi kesalahan dari panggilan metode terakhir.

Pada .NET 6 dan versi yang lebih baru, metode ini secara fungsional setara GetLastPInvokeErrordengan , yang dinamai untuk lebih mencerminkan niat API dan sifat lintas platformnya. GetLastPInvokeError harus lebih disukai daripada GetLastWin32Error.

Berlaku untuk

Lihat juga