Freigeben über


DllImportAttribute.PreserveSig-Feld

Gibt an, ob nicht verwaltete Methoden, die über HRESULT-Rückgabewerte oder retval-Rückgabewerte verfügen, direkt übersetzt werden oder ob HRESULT-Rückgabewerte oder retval-Rückgabewerte automatisch in Ausnahmen konvertiert werden.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public PreserveSig As Boolean
'Usage
Dim instance As DllImportAttribute
Dim value As Boolean

value = instance.PreserveSig

instance.PreserveSig = value
public bool PreserveSig
public:
bool PreserveSig
public boolean PreserveSig
public var PreserveSig : boolean

Hinweise

Legen Sie das PreserveSig-Feld auf true fest, um nicht verwaltete Signaturen mit HRESULT-Werten oder retval-Werten direkt zu übersetzen, oder legen Sie es auf false fest, um HRESULT-Werte oder retval-Werte automatisch in Ausnahmen zu konvertieren. Das PreserveSig-Feld ist standardmäßig auf true festgelegt.

Bei true gibt die resultierende Methodensignatur einen Ganzzahlwert zurück, der den HRESULT-Wert enthält. In diesem Fall müssen Sie den Rückgabewert manuell überprüfen und in der Anwendung entsprechend reagieren.

Wenn Sie das PreserveSig-Feld auf false festlegen, enthält die resultierende Methodensignatur anstelle eines Ganzzahlrückgabetyps einen leeren Rückgabetyp (HRESULT). Wenn die nicht verwaltete Methode ein HRESULT generiert, ignoriert die Laufzeit automatisch einen Rückgabewert von S_OK (oder 0) und löst keine Ausnahme aus. Für andere HRESULTs als S_OK löst die Laufzeit automatisch eine Ausnahme aus, die dem HRESULT entspricht. Beachten Sie, dass das DllImportAttribute-Attribut diese Konvertierung nur für Methoden ausführt, die ein HRESULT zurückgeben.

Sie können das Fehlerberichterstattungsstandardverhalten von HRESULT in Ausnahmen ändern, wenn Ausnahmen der Fehlerberichterstattungsstruktur Ihrer Anwendung besser gerecht werden. In anderen Fällen, in denen die Verwendung von HRESULT angemessener erscheint, können Sie die HRESULT-Fehlerberichterstattung verwenden.

Dieses Feld ähnelt PreserveSigAttribute. Im Gegensatz zum PreserveSig-Feld ist der Standardwert für das Attribut jedoch false.

In einigen Fällen verwenden Visual Basic-Entwickler DllImportAttribute anstelle der Declare-Anweisung, um eine DLL-Funktion in verwaltetem Code zu definieren. Ein solcher Fall ist beim Festlegen des PreserveSig-Felds gegeben.

Beispiel

Im folgenden Beispiel wird das DllImportAttribute verwendet, um die nicht verwaltete SHAutoComplete-Funktion zu importieren, nachdem das PreserveSig-Feld auf true festgelegt wurde und nachdem das PreserveSig-Feld auf false festgelegt wurde. Dieses Codebeispiel bewirkt, dass die SHAutoComplete-Funktion zunächst einen HRESULT-Fehler und dann eine Ausnahme generiert.

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());


    }
}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

DllImportAttribute-Klasse
DllImportAttribute-Member
System.Runtime.InteropServices-Namespace
PreserveSigAttribute