次の方法で共有


DllImportAttribute クラス

属性付きメソッドがアンマネージ DLL (Dynamic-Link Library) によって静的エントリ ポイントとして公開されることを示します。

この型のすべてのメンバの一覧については、DllImportAttribute メンバ を参照してください。

System.Object
   System.Attribute
      System.Runtime.InteropServices.DllImportAttribute

<AttributeUsage(AttributeTargets.Method)>
NotInheritable Public Class DllImportAttribute   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Method)]
public sealed class DllImportAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Method)]
public __gc __sealed class DllImportAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Method)
class DllImportAttribute extends Attribute

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

この属性は、メソッドに適用できます。

DllImportAttribute 属性は、アンマネージ DLL からエクスポートされた関数を呼び出すために必要な情報を提供します。最低限の要件として、エントリ ポイントを含む DLL の名前を指定する必要があります。

この属性は C# および C++ のメソッド定義に対して直接適用します。Visual Basic では、 Declare ステートメントが使用された場合にコンパイラによってこの属性が出力されます。 BestFitMappingCallingConventionExactSpellingPreserveSigSetLastErrorThrowOnUnmappableChar のいずれかのフィールドを含む複雑なメソッド定義の場合は、この属性を Visual Basic のメソッド定義に直接適用します。

メモ   JScript .NET はこの属性をサポートしていません。JScript .NET プログラムからアンマネージ API のメソッドにアクセスするには、C# または Visual Basic のラッパー クラスを使用します。

プラットフォーム呼び出しサービスを使用してアンマネージ DLL の関数にアクセスする方法の詳細については、「 アンマネージ DLL 関数の処理 」を参照してください。

使用例

[Visual Basic, C#, C++] DllImportAttribute をメソッドに適用する方法を次の例に示します。

 
<DllImport("KERNEL32.DLL", EntryPoint := "MoveFileW", _
   SetLastError := True, CharSet := CharSet.Unicode, _
   ExactSpelling := True, _
   CallingConvention := CallingConvention.StdCall)> _
Public Shared Function MoveFile(src As String, dst As String) As Boolean
    ' Leave function empty - DLLImport attribute forwards calls to MoveFile to
    ' MoveFileW in KERNEL32.DLL.
End Function

[C#] 
[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",  SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);

[C++] 
public:
 [DllImport(S"KERNEL32.DLL", EntryPoint=S"MoveFileW",  SetLastError=true,
 CharSet=CharSet::Unicode, ExactSpelling=true,
 CallingConvention=CallingConvention::StdCall)]
 static bool MoveFile(String* src, String* dst);

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Runtime.InteropServices

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

DllImportAttribute メンバ | System.Runtime.InteropServices 名前空間 | SetLastError | ExactSpelling | PreserveSig | CallingConvention