Método Marshal.GetHRForLastWin32Error ()
Dica
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.
Retorna o HRESULT que corresponde ao último erro incorrido pelo código Win32 executado usando Marshal.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (em mscorlib.dll)
Sintaxe
[SecurityCriticalAttribute]
public static int GetHRForLastWin32Error()
public:
[SecurityCriticalAttribute]
static int GetHRForLastWin32Error()
[<SecurityCriticalAttribute>]
static member GetHRForLastWin32Error : unit -> int
<SecurityCriticalAttribute>
Public Shared Function GetHRForLastWin32Error As Integer
Valor Retornado
Type: System.Int32
O HRESULT que corresponde ao último código de erro Win32.
Comentários
A função de destino deve ter tido o setLastError metadados sinalizador definido. Por exemplo, o SetLastError campo o System.Runtime.InteropServices.DllImportAttribute devem ser true. O processo para definir esse sinalizador depende do idioma de origem usado: c# e C++ são false por padrão, mas o Declare instrução no Visual Basic é true.
Exemplos
O exemplo a seguir demonstra como recuperar um HRESULT correspondente a um código de erro Win32 usando o GetHRForLastWin32Error método.
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
Segurança
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Informações de Versão
Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Windows Phone Silverlight
Disponível desde 8.0
Windows Phone
Disponível desde 8.1
Confira Também
DllImportAttribute
Classe Marshal
Namespace System.Runtime.InteropServices
Retornar ao início