Evaluar y enviar comentarios
MSDN
MSDN Library
 GetHRForLastWin32Error (Método)
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2005/.NET Framework 2.0

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
Marshal.GetHRForLastWin32Error (Método)

Devuelve el valor de HRESULT correspondiente al último error producido por el código Win32 ejecutado mediante Marshal.

Espacio de nombres: System.Runtime.InteropServices
Ensamblado: mscorlib (en mscorlib.dll)

Visual Basic (Declaración)
Public Shared Function GetHRForLastWin32Error As Integer
Visual Basic (Uso)
Dim returnValue As Integer

returnValue = Marshal.GetHRForLastWin32Error
C#
public static int GetHRForLastWin32Error ()
C++
public:
static int GetHRForLastWin32Error ()
J#
public static int GetHRForLastWin32Error ()
JScript
public static function GetHRForLastWin32Error () : int

Valor devuelto

El valor de HRESULT correspondiente al último código de error Win32.

La función destino debía tener el indicador de metadatos setLastError activado. Por ejemplo, el campo SetLastError de System.Runtime.InteropServices.DllImportAttribute debe ser true. El procedimiento empleado varía según el lenguaje de origen que se utilice: C# y C++ son false de forma predeterminada, pero la instrucción Declare en Visual Basic es true.

NotaNota

Este método utiliza SecurityAction.LinkDemand para evitar que se le llame desde código que no sea de confianza; sólo el llamador inmediato debe disponer del permiso SecurityPermissionAttribute.UnmanagedCode. Si se puede llamar al código desde código de confianza parcial, no pase ninguna entrada de usuario a los métodos de la clase Marshal sin validación. Para conocer las limitaciones importantes que existen a la hora de utilizar el miembro LinkDemand, vea Demand frente a LinkDemand.

En el ejemplo de código siguiente se muestra cómo recuperar el valor de HRESULT correspondiente a un código de error Win32 utilizando el método GetHRForLastWin32Error.

Visual Basic
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)> _
    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
C#
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)]
    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

Windows 98, Windows 2000 SP4, Windows Millennium, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2012 Microsoft. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker