13,009
回編集
編集の要約なし |
|||
4行目: | 4行目: | ||
== DllImport属性 == | == DllImport属性 == | ||
<code>DllImport</code> | <code>DLLImport</code>属性は、DLLエントリポイントを定義する関数を記述することで、DLLファイルに定義された関数を呼び出すことができる。<br> | ||
<source lang="csharp"> | |||
[ DllImport( "DLL名" ) ] | |||
</source> | |||
<br> | |||
また、DLLファイルに定義された関数を、関数名または序数で指定するには、<code>DllImport</code>属性の<code>EntryPoint</code>フィールドを使用する。<br> | |||
メソッド定義内の関数の名前がDLLエントリポイントと同じである場合は、その関数を<code>EntryPoint</code>フィールドで明示的に識別する必要はない。<br> | |||
同じではない場合は、次のいずれかの記述で名前または序数を指定する。<br> | |||
<br> | |||
以下の例では、<code>EntryPoint</code>フィールドを使用して、<code>MessageBoxA</code>をMsgBoxAに置き換える方法を示している。<br> | |||
<source lang="csharp"> | |||
using System; | |||
using System.Runtime.InteropServices; | |||
internal static class NativeMethods | |||
{ | |||
[DllImport("user32.dll", EntryPoint = "MessageBoxA")] | |||
internal static extern int MsgBoxA(IntPtr hWnd, string lpText, string lpCaption, uint uType); | |||
} | |||
</source> | |||
<br> | |||
また、以下の例では、DLLファイルに定義された関数を序数で指定している。<br> | |||
<source lang="csharp"> | |||
[DllImport("DllName", EntryPoint = "Functionname")] | |||
[DllImport("DllName", EntryPoint = "#123")] // 序数を使用するには、序数値の前にシャープ記号#を付ける必要がある | |||
</source> | |||
<br> | |||
<code>DllImport</code>属性には、DLLファイル名を指定する以外にも、下表のような引数を与えることができる。<br> | |||
<center> | <center> | ||
{| class="wikitable" | {| class="wikitable" | ||
24行目: | 50行目: | ||
|} | |} | ||
</center> | </center> | ||
<br> | |||
詳細は、以下のMSDNのWebサイトを参照すること。<br> | |||
[https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.dllimportattribute.aspx DllImportAttribute クラス (System.Runtime.InteropServices) | MSDN]<br> | |||
<br> | <br> | ||
* 呼び出し規約(Calling Convention) | * 呼び出し規約(Calling Convention) | ||
83行目: | 112行目: | ||
} | } | ||
</source> | </source> | ||
詳細は、以下のMSDNのWebサイトを参照すること。<br> | |||
[https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.callingconvention.aspx CallingConvention 列挙型 (System.Runtime.InteropServices) | MSDN]<br> | |||
<br><br> | <br><br> | ||
129行目: | 160行目: | ||
</source> | </source> | ||
<br> | <br> | ||
詳細は、以下のMSDNのWebサイトを参照すること。<br> | |||
[https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.structlayoutattribute.aspx StructLayoutAttribute クラス (System.Runtime.InteropServices) | MSDN]<br> | [https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.structlayoutattribute.aspx StructLayoutAttribute クラス (System.Runtime.InteropServices) | MSDN]<br> | ||
<br><br> | <br><br> |