Indique si les méthodes non managées qui ont les valeurs de retour
HRESULT ou
retval sont traduites directement ou si les valeurs de retour
HRESULT ou
retval sont automatiquement converties en exceptions.
Espace de noms : System.Runtime.InteropServices
Assembly : mscorlib (dans mscorlib.dll)
Visual Basic (Déclaration)
Public PreserveSig As Boolean
Visual Basic (Utilisation)
Dim instance As DllImportAttribute
Dim value As Boolean
value = instance.PreserveSig
instance.PreserveSig = value
public boolean PreserveSig
public var PreserveSig : boolean
Affectez au champ PreserveSig la valeur true pour traduire directement les signatures non managées avec des valeurs HRESULT ou retval ; affectez-lui la valeur false pour convertir automatiquement les valeurs HRESULT ou retval en exceptions. Par défaut, le champ PreserveSig a la valeur true.
Lorsque la valeur est true, la signature de méthode résultante retourne une valeur entière qui contient la valeur HRESULT. Dans ce cas, vous devez inspecter manuellement la valeur de retour et répondre en conséquence dans votre application.
Lorsque vous affectez au champ PreserveSig la valeur false, la signature de méthode résultante contient un type de retour void au lieu d'un type de retour entier (HRESULT). Lorsque la méthode non managée produit HRESULT, le runtime ignore automatiquement une valeur de retour S_OK (ou 0) et ne lève pas d'exception. Pour les valeurs HRESULT autres que S_OK, le runtime lève automatiquement une exception qui correspond à HRESULT. Notez que l'attribut DllImportAttribute effectue uniquement cette conversion pour les méthodes qui retournent HRESULT.
Vous pouvez modifier le comportement de rapport d'erreurs par défaut des valeurs HRESULT en exceptions lorsque des exceptions conviennent mieux à la structure de rapport d'erreurs de votre application. Toutefois, vous pouvez décider d'utiliser le rapport d'erreurs HRESULT dans d'autres cas où l'utilisation des valeurs HRESULT est préférable.
Ce champ est similaire à PreserveSigAttribute ; toutefois, à la différence du champ PreserveSig, l'attribut a, par défaut, la valeur false.
Dans certains cas, les développeurs Visual Basic utilisent DllImportAttribute au lieu de l'instruction Declare pour définir une fonction DLL dans du code managé. La définition du champ PreserveSig figure parmi ces cas.
L'exemple de code suivant utilise DllImportAttribute pour importer la fonction SHAutoComplete non managée, une première fois avec la valeur true affectée au champ PreserveSig, puis avec la valeur false affectée au champ PreserveSig. Dans cet exemple de code, la fonction SHAutoComplete génère une erreur HRESULT la première fois, puis une exception la fois suivante.
Imports System
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 = &H0
SHACF_FILESYSTEM = &H1
End Enum 'SHAutoCompleteFlags
' 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
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());
}
}
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
.NET Framework
Prise en charge dans : 2.0, 1.1, 1.0
.NET Compact Framework
Prise en charge dans : 2.0