DllImportAttribute.PreserveSig Campo

Definizione

Indica se i metodi non gestiti con valore restituito HRESULT o retval vengono convertiti direttamente o se il valore restituito HRESULT o retval viene convertito automaticamente in eccezioni.Indicates whether unmanaged methods that have HRESULT or retval return values are directly translated or whether HRESULT or retval return values are automatically converted to exceptions.

public: bool PreserveSig;
public bool PreserveSig;
val mutable PreserveSig : bool
Public PreserveSig As Boolean 

Valore del campo

Esempi

Nell'esempio DllImportAttribute di codice seguente viene utilizzato per importare la SHAutoComplete funzione non gestita una volta con PreserveSig il campo impostato true su e nuovamente con PreserveSig il campo impostato falsesu.The following code example uses the DllImportAttribute to import the unmanaged SHAutoComplete function once with the PreserveSig field set to true and again with the PreserveSig field set to false. Questo esempio di codice fa SHAutoComplete in modo che la funzione generi errori con un'eccezione una volta HRESULT e la successiva.This code example causes the SHAutoComplete function to generate any errors with an exception one time and an HRESULT the next.

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

Commenti

Impostare il PreserveSig campo su true per tradurre direttamente le firme non gestite con HRESULT i retval valori o. impostarlo false su per convertire HRESULT automaticamente retval i valori o in eccezioni.Set the PreserveSig field to true to directly translate unmanaged signatures with HRESULT or retval values; set it to false to automatically convert HRESULT or retval values to exceptions. Per impostazione predefinita, PreserveSig il campo trueè.By default, the PreserveSig field is true.

Se true, la firma del metodo risultante restituisce un valore integer che contiene HRESULT il valore.When true, the resulting method signature returns an integer value that contains the HRESULT value. In questo caso, è necessario esaminare manualmente il valore restituito e rispondere di conseguenza nell'applicazione.In this case, you must manually inspect the return value and respond accordingly in your application.

Quando si imposta il PreserveSig campo su false, la firma del metodo risultante contiene un tipo restituito void anziché un tipo restituitoHRESULTInteger ().When you set the PreserveSig field to false, the resulting method signature contains a void return type instead of an integer (HRESULT) return type. Quando il metodo non gestito produce un oggetto HRESULT, il runtime ignora automaticamente il S_OK valore restituito (o 0) e non genera un'eccezione.When the unmanaged method produces an HRESULT, the runtime automatically ignores a return value of S_OK (or 0) and does not throw an exception. Per HRESULTs HRESULTdiverso da, il runtime genera automaticamente un'eccezione che corrisponde a. S_OKFor HRESULTs other than S_OK, the runtime automatically throws an exception that corresponds to the HRESULT. Si noti che DllImportAttribute l'attributo esegue questa conversione solo a metodi che restituiscono un oggetto. HRESULTNote that the DllImportAttribute attribute only performs this conversion to methods that return an HRESULT.

È possibile decidere di modificare il comportamento predefinito di segnalazione degli HRESULTerrori da s a eccezioni nei casi in cui le eccezioni si adattano meglio alla struttura di segnalazione degli errori dell'applicazione.You might decide to change the default error reporting behavior from HRESULTs to exceptions in cases where exceptions better fit the error reporting structure of your application.

Questo campo è simile a PreserveSigAttribute. Tuttavia, a differenza del PreserveSig campo, il valore predefinito per l'attributo è false.This field is similar to the PreserveSigAttribute; however, in contrast to the PreserveSig field, the default value for the attribute is false.

In alcuni casi, Visual Basic sviluppatori utilizzano DllImportAttribute, anziché utilizzare l' Declare istruzione, per definire una funzione di dll nel codice gestito.In some cases, Visual Basic developers use the DllImportAttribute, instead of using the Declare statement, to define a DLL function in managed code. L'impostazione PreserveSig del campo è uno di questi casi.Setting the PreserveSig field is one of those cases.

Si applica a

Vedi anche