13,007
回編集
細 (文字列「source lang」を「syntaxhighlight lang」に置換) |
細 (文字列「</source>」を「</syntaxhighlight>」に置換) |
||
12行目: | 12行目: | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
[ DllImport( "DLL名" ) ] | [ DllImport( "DLL名" ) ] | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
また、DLLファイルに定義された関数を、関数名または序数で指定するには、<code>DllImport</code>属性の<code>EntryPoint</code>フィールドを使用する。<br> | また、DLLファイルに定義された関数を、関数名または序数で指定するには、<code>DllImport</code>属性の<code>EntryPoint</code>フィールドを使用する。<br> | ||
28行目: | 28行目: | ||
internal static extern int MsgBoxA(IntPtr hWnd, string lpText, string lpCaption, uint uType); | internal static extern int MsgBoxA(IntPtr hWnd, string lpText, string lpCaption, uint uType); | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
また、以下の例では、DLLファイルに定義された関数を序数で指定している。<br> | また、以下の例では、DLLファイルに定義された関数を序数で指定している。<br> | ||
34行目: | 34行目: | ||
[DllImport("DllName", EntryPoint = "Functionname")] | [DllImport("DllName", EntryPoint = "Functionname")] | ||
[DllImport("DllName", EntryPoint = "#123")] // 序数を使用するには、序数値の前にシャープ記号#を付ける必要がある | [DllImport("DllName", EntryPoint = "#123")] // 序数を使用するには、序数値の前にシャープ記号#を付ける必要がある | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<code>DllImport</code>属性には、DLLファイル名を指定する以外にも、下表のような引数を与えることができる。<br> | <code>DllImport</code>属性には、DLLファイル名を指定する以外にも、下表のような引数を与えることができる。<br> | ||
119行目: | 119行目: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
詳細は、以下のMSDNのWebサイトを参照すること。<br> | 詳細は、以下のMSDNのWebサイトを参照すること。<br> | ||
[https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.callingconvention.aspx CallingConvention 列挙型 (System.Runtime.InteropServices) | MSDN]<br> | [https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.callingconvention.aspx CallingConvention 列挙型 (System.Runtime.InteropServices) | MSDN]<br> | ||
128行目: | 128行目: | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
[ StructLayout( LayoutKind列挙 ) ] | [ StructLayout( LayoutKind列挙 ) ] | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
下表に、LayoutKind列挙子を示す。<br> | 下表に、LayoutKind列挙子を示す。<br> | ||
166行目: | 166行目: | ||
public double z; | public double z; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
詳細は、以下のMSDNのWebサイトを参照すること。<br> | 詳細は、以下のMSDNのWebサイトを参照すること。<br> | ||
182行目: | 182行目: | ||
[CLSCompliant( false )] | [CLSCompliant( false )] | ||
public int SetValue( UInt32 value ); | public int SetValue( UInt32 value ); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
以下の例では、CLSCompliantAttribute属性をアセンブリ全体に適用する。<br> | 以下の例では、CLSCompliantAttribute属性をアセンブリ全体に適用する。<br> | ||
188行目: | 188行目: | ||
using System; | using System; | ||
[assembly: CLSCompliant(true)] | [assembly: CLSCompliant(true)] | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
詳細は、以下のMSDNのWebサイトを参照すること。<br> | 詳細は、以下のMSDNのWebサイトを参照すること。<br> | ||
226行目: | 226行目: | ||
return "Hello World"; | return "Hello World"; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
244行目: | 244行目: | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
void Method([in] int[] array); | void Method([in] int[] array); | ||
</ | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
1,100行目: | 1,100行目: | ||
int __stdcall SampleCallback(int handle, int lPalam); | int __stdcall SampleCallback(int handle, int lPalam); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
1,127行目: | 1,127行目: | ||
return iRet; | return iRet; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
1,138行目: | 1,138行目: | ||
; 公開する関数名をリストアップ | ; 公開する関数名をリストアップ | ||
SampleCallback @1 | SampleCallback @1 | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | 次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | ||
1,176行目: | 1,176行目: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
== トラブル対処 == | == トラブル対処 == |