Marshal.GetHRForLastWin32Error Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce il valore HRESULT corrispondente all'ultimo errore in cui è incorso il codice Win32 eseguito usando Marshal.
public:
static int GetHRForLastWin32Error();
[System.Security.SecurityCritical]
public static int GetHRForLastWin32Error();
public static int GetHRForLastWin32Error();
[<System.Security.SecurityCritical>]
static member GetHRForLastWin32Error : unit -> int
static member GetHRForLastWin32Error : unit -> int
Public Shared Function GetHRForLastWin32Error () As Integer
Restituisce
HRESULT corrispondente all'ultimo codice errore Win32.
- Attributi
Esempio
Nell'esempio seguente viene illustrato come recuperare un valore HRESULT corrispondente a un codice di errore Win32 usando il GetHRForLastWin32Error metodo .
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 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.
int HRESULT = Marshal.GetHRForLastWin32Error();
Console.WriteLine("The last Win32 Error was: " + HRESULT);
}
static void Main(string[] args)
{
Run();
}
}
// This code example displays the following to the console:
//
// Calling Win32 MessageBox with error...
// The last Win32 Error was: -2147023496
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 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.
Dim HRESULT As Integer
HRESULT = Marshal.GetHRForLastWin32Error()
Console.WriteLine("The last Win32 Error was: " + HRESULT)
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 with error...
' The last Win32 Error was: -2147023496
Commenti
La funzione di destinazione deve avere il setLastError flag di metadati impostato. Ad esempio, il SetLastError campo di System.Runtime.InteropServices.DllImportAttribute deve essere true. Il processo per l'impostazione di questo flag dipende dal linguaggio di origine usato: C# e C++ sono false per impostazione predefinita, ma l'istruzione Declare in Visual Basic è true.