Marshal.GetLastWin32Error Yöntem

Tanım

Bayrağı ayarlanmış platform çağrısı SetLastError kullanılarak çağrılan son yönetilmeyen işlev tarafından döndürülen hata kodunu döndürür.

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

Döndürülenler

Win32 SetLastError işlevi çağrısı tarafından ayarlanan son hata kodu.

Öznitelikler

Örnekler

Aşağıdaki örnek yöntemini çağırır GetLastWin32Error . Örnekte ilk olarak hata olmadan yönteminin çağrılması ve ardından bir hata ile yönteminin çağrılması gösterilmektedir.

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

Açıklamalar

Windows sistemlerinde Win32 GetLastWin32ErrorGetLastError işlevini Kernel32.DLL kullanıma sunar. Bu yöntem, bu bilgileri almak için doğrudan platform çağırma çağrısı GetLastError yapmak güvenilir olmadığından mevcuttur. Bu hata koduna erişmek istiyorsanız, için GetLastError kendi platform çağırma tanımınızı yazmak ve çağırmak yerine öğesini çağırmanız GetLastWin32Error gerekir. Ortak dil çalışma zamanı, işletim sistemi tarafından bakımı yapılan api'lerin üzerine yazan API'lere GetLastError iç çağrılar yapabilir.

Hata kodlarını almak için bu yöntemi yalnızca yöntem imzasına uygular System.Runtime.InteropServices.DllImportAttribute ve alanını olarak trueayarlarsanız DllImportAttribute.SetLastError kullanabilirsiniz. Bunun işlemi, kullanılan kaynak dile bağlı olarak değişir: C# ve C++ varsayılan olarakdır false , ancak Declare Visual Basic'teki deyimi şeklindedir true.

.NET Core'da yönteminin GetLastWin32Error davranışında bir fark vardır ve olduğunda DllImportAttribute.SetLastError .NET Framework.true .NET Framework,yöntem GetLastWin32Error hata bilgilerini bir P/Invoke çağrısından sonrakine saklayabilir. .NET Core'da hata bilgileri P/Invoke çağrısından önce temizlenir ve GetLastWin32Error yalnızca son yöntem çağrısındaki hata bilgilerini temsil eder.

.NET 6 ve sonraki sürümlerde bu yöntem, API'nin amacını GetLastPInvokeErrorve platformlar arası niteliğini daha iyi yansıtmak için adlandırılan ile işlevsel olarak eşdeğerdir. GetLastPInvokeError yerine GetLastWin32Errortercih edilmelidir.

Şunlara uygulanır

Ayrıca bkz.