Marshal.GetLastWin32Error Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
SetLastError bayrağı ayarlanmış platform çağrısı 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 hiçbir 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 GetLastWin32Error, Kernel32.DLL win32 GetLastError işlevini 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, kendi platform çağırma tanımınızı GetLastWin32Error yazmak ve çağırmak yerine çağırmalısınızGetLastError. Ortak dil çalışma zamanı, işletim sistemi tarafından bakımı yapılan API'lerin üzerine yazan GetLastError iç çağrılar yapabilir.
Hata kodlarını almak için bu yöntemi yalnızca yöntem imzasına uyguladığınızda System.Runtime.InteropServices.DllImportAttribute ve alanını olarak DllImportAttribute.SetLastErrorayarladığınızda true kullanabilirsiniz. Bunun işlemi, kullanılan kaynak dile bağlı olarak değişir: C# ve C++ varsayılan olarak false'dir, ancak Visual Basic'daki Declare deyimi true'dir.
GetLastWin32Error
DllImportAttribute.SetLastError olduğunda .NET Core ve .NET Framework'te true yönteminin davranışında bir fark vardır. .NET Framework'te GetLastWin32Error yöntemi bir P/Invoke çağrısından sonrakine hata bilgilerini koruyabilir. .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 üzeri sürümlerde, bu yöntem işlevsel olarak API'nin amacını ve platformlar arası doğasını daha iyi yansıtacak şekilde adlandırılan GetLastPInvokeError eşdeğerdir. GetLastPInvokeError yerine GetLastWin32Errortercih edilmelidir.