Export (0) Print
Expand All

DllImportAttribute.PreserveSig Field

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.

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

public bool PreserveSig
public boolean PreserveSig
public var PreserveSig : boolean

Set the PreserveSig field to true to directly translated unmanaged signatures with HRESULT or retval values; set it to false to automatically convert HRESULT or retval values to exceptions. By default, the PreserveSig field is true.

When true, the resulting method signature returns an integer value that contains the HRESULT value. In this case, you must manually inspect the return value and respond accordingly in your application.

When you set the PreserveSig field to false, the resulting method signature contains a void return type instead of an integer (HRESULT) return type. 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. For HRESULTs other than S_OK, the runtime automatically throws an exception that corresponds to the HRESULT. Note that the DllImportAttribute attribute only performs this conversion to methods that return an HRESULT.

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. However, you might decide to use HRESULT error reporting in other cases where using HRESULTs is more preferment.

This field is similar to the PreserveSigAttribute; however, in contrast to the PreserveSig field, the default value for the attribute is false.

In some cases, Visual Basic developers use the DllImportAttribute, instead of using the Declare statement, to define a DLL function in managed code. Setting the PreserveSig field is one of those cases.

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. This code example causes the SHAutoComplete function to generate an HRESULT error one time and an exception the next.

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 for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft