DllImportAttribute.PreserveSig Campo
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Indica si los métodos no administrados que tienen HRESULT
valores devueltos se traducen directamente o si HRESULT
los valores devueltos se convierten automáticamente en excepciones.
public: bool PreserveSig;
public bool PreserveSig;
val mutable PreserveSig : bool
Public PreserveSig As Boolean
En el ejemplo de código siguiente se usa DllImportAttribute para importar la función no administrada SHAutoComplete
una vez con el PreserveSig campo establecido true
en y de nuevo con el PreserveSig campo establecido en false
. Este ejemplo de código hace que la SHAutoComplete
función genere errores con una excepción una vez y otra HRESULT
.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
internal class Win32
{
// The SHAutoComplete function allows you
// to add auto-compete functionality to your
// Windows Forms text boxes. In .NET Framework
// 1.1 and earlier, you can use SHAutoComplete.
// Later versions have this ability built in without
// requiring platform invoke.
// See the MSDN documentation of the
// SHAutoComplete function for the
// complete set of flags.
public enum SHAutoCompleteFlags
{
SHACF_DEFAULT = 0x00000000,
SHACF_FILESYSTEM = 0x00000001
}
// Use the DllImportAttribute to import the SHAutoComplete function.
// Set the PreserveSig to false to specify exception errors.
[DllImportAttribute("shlwapi.dll", EntryPoint = "SHAutoComplete", ExactSpelling = true, PreserveSig = false)]
public static extern void SHAutoComplete(IntPtr hwndEdit, SHAutoCompleteFlags dwFlags);
// Use the DllImportAttribute to import the SHAutoComplete function.
// Use the default value of the PreserveSig field to specify HRESULT errors.
[DllImportAttribute("shlwapi.dll", EntryPoint = "SHAutoComplete", ExactSpelling = true)]
public static extern int SHAutoCompleteHRESULT(IntPtr hwndEdit, SHAutoCompleteFlags dwFlags);
}
static class Program
{
static void Main()
{
Run();
}
static void Run()
{
// Create a null (nothing in Visual Basic) IntPtr
// to pass to the SHAutoComplete method. Doing so
// creates a failure and demonstrates the two ways
// that the PreserveSig property allows you to handle
// failures.
// Normally, you would pass a handle to a managed
// Windows Forms text box.
IntPtr iPtr = new IntPtr(0);
// Call the SHAutoComplete function using exceptions.
try
{
Console.WriteLine("Calling the SHAutoComplete method with the PreserveSig field set to false.");
Win32.SHAutoComplete(iPtr, Win32.SHAutoCompleteFlags.SHACF_DEFAULT);
}
catch (Exception e)
{
Console.WriteLine("Exception handled: " + e.Message);
}
Console.WriteLine("Calling the SHAutoComplete method with the PreserveSig field set to true.");
// Call the SHAutoComplete function using HRESULTS.
int HRESULT = Win32.SHAutoCompleteHRESULT(iPtr, Win32.SHAutoCompleteFlags.SHACF_DEFAULT);
Console.WriteLine("HRESULT handled: " + HRESULT.ToString());
}
}
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Module Win32
' The SHAutoComplete function allows you
' to add auto-compete functionality to your
' Windows Forms text boxes. In .NET Framework
' 1.1 and earlier, you can use SHAutoComplete.
' Later versions have this ability built in without
' requiring platform invoke.
' See the MSDN documentation of the
' SHAutoComplete function for the
' complete set of flags.
Public Enum SHAutoCompleteFlags
SHACF_DEFAULT = &H1
SHACF_FILESYSTEM = &H1
End Enum
' Use the DllImportAttribute to import the SHAutoComplete function.
' Set the PreserveSig to false to specify exception errors.
<DllImportAttribute("shlwapi.dll", EntryPoint:="SHAutoComplete", ExactSpelling:=True, PreserveSig:=False)> _
Public Sub SHAutoComplete(ByVal hwndEdit As IntPtr, ByVal dwFlags As SHAutoCompleteFlags)
End Sub
' Use the DllImportAttribute to import the SHAutoComplete function.
' Use the default value of the PreserveSig field to specify HRESULT errors.
<DllImportAttribute("shlwapi.dll", EntryPoint:="SHAutoComplete", ExactSpelling:=True)> _
Public Function SHAutoCompleteHRESULT(ByVal hwndEdit As IntPtr, ByVal dwFlags As SHAutoCompleteFlags) As Integer
End Function
End Module
Module Program
Sub Main()
Run()
End Sub
Sub Run()
' Create a null (nothing in Visual Basic) IntPtr
' to pass to the SHAutoComplete method. Doing so
' creates a failure and demonstrates the two ways
' that the PreserveSig property allows you to handle
' failures.
' Normally, you would pass a handle to a managed
' Windows Forms text box.
Dim iPtr As New IntPtr(0)
' Call the SHAutoComplete function using exceptions.
Try
Console.WriteLine("Calling the SHAutoComplete method with the PreserveSig field set to false.")
Win32.SHAutoComplete(iPtr,Win32.SHAutoCompleteFlags.SHACF_DEFAULT)
Catch e As Exception
Console.WriteLine("Exception handled: " + e.Message)
End Try
Console.WriteLine("Calling the SHAutoComplete method with the PreserveSig field set to true.")
' Call the SHAutoComplete function using HRESULTS.
Dim HRESULT As Integer = Win32.SHAutoCompleteHRESULT(iPtr,Win32.SHAutoCompleteFlags.SHACF_DEFAULT)
Console.WriteLine("HRESULT handled: " + HRESULT.ToString())
End Sub
End Module
Establezca el PreserveSig campo en true
para traducir firmas no administradas con HRESULT
valores directamente; establézcalo en false
para convertir HRESULT
automáticamente los valores devueltos en excepciones. De forma predeterminada, el PreserveSig campo es true
.
Cuando true
es , la firma del método administrado devuelve un valor entero que contiene el HRESULT
valor . En este caso, debe inspeccionar manualmente el valor devuelto y responder en consecuencia en la aplicación.
Cuando se establece el PreserveSig campo false
en , la firma del método administrado tiene un tipo de valor devuelto void o el tipo del último parámetro [out, retval] no administrado. Cuando el método no administrado genera , HRESULT
el tiempo de ejecución omite automáticamente un valor devuelto de S_OK
(o 0) y no produce una excepción. En HRESULT
el caso S_OK
de , el tiempo de ejecución inicia automáticamente una excepción que corresponde a HRESULT
.
Es posible que decida cambiar el comportamiento predeterminado de informes de errores de HRESULT
s a excepciones en los casos en los que las excepciones se ajusten mejor a la estructura de informes de errores de la aplicación.
Este campo es similar a PreserveSigAttribute; sin embargo, a diferencia del PreserveSig campo , el valor predeterminado para el atributo es false
.
En algunos casos, los desarrolladores de Visual Basic usan DllImportAttribute, en lugar de usar la Declare
instrucción , para definir una función DLL en código administrado. Establecer el PreserveSig campo es uno de esos casos.
Produto | Versións |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Comentarios de .NET
.NET é un proxecto de código aberto. Selecciona unha ligazón para ofrecer comentarios: