DllImportAttribute.PreserveSig Mező

Definíció

Azt jelzi, hogy a visszatérési értékeket tartalmazó nem felügyelt metódusok HRESULT közvetlenül lefordítva vannak-e, vagy a HRESULT visszatérési értékek automatikusan kivételekké lesznek konvertálva.

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

Mező értéke

Példák

Az alábbi példakód a DllImportAttribute nem felügyelt SHAutoComplete függvény importálására szolgál egyszer, a PreserveSig mező értéke true pedig ismét a PreserveSigfalsekövetkező. Ez a példakód azt eredményezi, hogy a SHAutoComplete függvény minden hibát egy alkalommal HRESULT kivétellel, a következőt pedig kivétellel generál.

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

Megjegyzések

Állítsa be a PreserveSig mezőt úgy, hogy true a nem felügyelt aláírásokat közvetlenül az értékekkel HRESULT lefordítsa; állítsa be úgy, hogy false a visszatérési értékeket automatikusan kivételekké konvertálja HRESULT . Alapértelmezés szerint a mező a PreserveSig következő true: .

Amikor truea felügyelt metódus aláírása egy egész számot ad vissza, amely tartalmazza az HRESULT értéket. Ebben az esetben manuálisan kell megvizsgálnia a visszatérési értéket, és ennek megfelelően kell válaszolnia az alkalmazásban.

A mező PreserveSigbeállításakor a false felügyelt metódus aláírása érvénytelen visszatérési típussal vagy az utolsó nem felügyelt [out, retval] paraméter típusával rendelkezik. Ha a nem felügyelt metódus létrehoz egy HRESULThibát, a futtatókörnyezet automatikusan figyelmen kívül hagyja a (vagy 0) visszatérési S_OK értéket, és nem ad kivételt. Más esetén HRESULTa futtatókörnyezet automatikusan kivételt ad, amely megfelel a S_OK.HRESULT

Dönthet úgy, hogy az alapértelmezett hibajelentési viselkedést s-ről HRESULTkivételre módosítja olyan esetekben, amikor a kivételek jobban illeszkednek az alkalmazás hibajelentési struktúrájához.

Ez a mező hasonló a PreserveSigAttributemezőhöz, azonban a PreserveSig mezővel ellentétben az attribútum alapértelmezett értéke a .false

Bizonyos esetekben Visual Basic fejlesztők a DllImportAttribute a Declare utasítás helyett egy DLL-függvényt definiálnak a felügyelt kódban. PreserveSig A mező beállítása az ilyen esetek egyike.

A következőre érvényes:

Lásd még