DllImportAttribute.PreserveSig 필드

정의

반환 값이 있는 관리되지 않는 메서드가 HRESULT 직접 변환되는지 또는 반환 값이 자동으로 예외로 변환되는지 여부를 HRESULT 나타냅니다.

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

필드 값

예제

다음 코드 예제에서는 를 사용하여 DllImportAttribute 필드가 로 설정된 상태에서 관리 SHAutoComplete 되지 않는 함수를 true 한 번 PreserveSig 가져오고 필드가 로 설정된 false경우 PreserveSig 를 다시 가져옵니다. 이 코드 예제에서는 함수가 SHAutoComplete 예외를 한 번 및 HRESULT 다음 오류와 함께 생성합니다.

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

설명

PreserveSig 필드를 true 로 설정하여 값으로 HRESULT 관리되지 않는 서명을 직접 변환하고 반환 값을 예외로 false 자동으로 변환 HRESULT 하도록 로 설정합니다. 기본적으로 PreserveSig 필드는 입니다 true.

이면 true관리되는 메서드 시그니처는 값이 포함된 정수 값을 반환합니다 HRESULT . 이 경우 수동으로 반환 값을 검사 하며 그에 따라 응답 애플리케이션에서 합니다.

필드를 false로 설정 PreserveSig 하면 관리되는 메서드 서명에 void 반환 형식 또는 마지막 관리되지 않는 [out, retval] 매개 변수의 형식이 있습니다. 관리되지 않는 메서드가 를 HRESULT생성하면 런타임은 (또는 0)의 S_OK 반환 값을 자동으로 무시하고 예외를 throw하지 않습니다. 이 아닌 의 경우 HRESULT런타임은 에 해당하는 예외를 자동으로 throw합니다HRESULT.S_OK

기본 오류 보고 동작을 변경할 수도 있습니다 HRESULTs 오류 보고 애플리케이션의 구조에 예외 보다 적합 한 경우에는 예외입니다.

이 필드는 과 유사 PreserveSigAttribute합니다. 그러나 필드와 달리 PreserveSig 특성의 기본값은 입니다 false.

경우에 따라 Visual Basic 개발자는 문을 사용하는 대신 를 사용하여 DllImportAttributeDeclare 관리 코드에서 DLL 함수를 정의합니다. 필드 설정 PreserveSig 은 이러한 경우 중 하나입니다.

적용 대상

추가 정보