DllImportAttribute.PreserveSig フィールドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DllImportAttribute.PreserveSig フィールドの意味・解説 

DllImportAttribute.PreserveSig フィールド

戻り値HRESULT または retval であるアンマネージ メソッド直接変換するか、戻り値 HRESULT または retval自動的に例外変換するかを示します

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

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
解説解説

PreserveSig フィールドtrue設定すると、HRESULT 値または retval 値でアンマネージ シグネチャ直接変換されます。false設定すると、HRESULT 値または retval 値が自動的に例外変換されます。既定では、PreserveSig フィールドtrue です。

true場合結果として生成されるメソッド シグネチャHRESULT 値を含む整数値を返します。この場合アプリケーション内で戻り値手動検査しその結果に応じて対応する必要があります

PreserveSig フィールドfalse設定すると、結果として生成されるメソッド シグネチャ含まれる戻り値の型は、整数型 (HRESULT) ではなく void 型なります。アンマネージ メソッドHRESULT生成した場合ランタイム戻り値 S_OK (または 0) を自動的に無視し例外スローしません。S_OK 以外の HRESULT場合ランタイムはその HRESULT対応する例外自動的にスローます。DllImportAttribute 属性は、HRESULT返すメソッドに対してこの変換実行するだけです。

アプリケーションエラー報告構造例外適している場合は、既定エラー報告動作HRESULT から例外変更できます。ただし、HRESULT使用する方が望ましい場合は、HRESULT エラー報告使用します

このフィールドは PreserveSigAttribute と似ていますが、PreserveSig フィールドとは異なり、この属性既定値false です。

場合によっては、Visual Basic開発者は、マネージ コードDLL 関数定義する際、Declare ステートメント使用する代わりにDllImportAttribute使用しますPreserveSig フィールドの設定は、このような事例1 つです。

使用例使用例

DllImportAttribute使用して PreserveSig フィールドtrue設定し、アンマネージ SHAutoComplete 関数インポートした後、PreserveSig フィールド再度 false設定するコード例次に示します。このコード例では、SHAutoComplete 関数によって HRESULT エラー1 度生成され次に例外生成されます。

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


    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DllImportAttribute クラス
DllImportAttribute メンバ
System.Runtime.InteropServices 名前空間
PreserveSigAttribute


このページでは「.NET Framework クラス ライブラリ リファレンス」からDllImportAttribute.PreserveSig フィールドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDllImportAttribute.PreserveSig フィールドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDllImportAttribute.PreserveSig フィールドを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

DllImportAttribute.PreserveSig フィールドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DllImportAttribute.PreserveSig フィールドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS