「ライブラリの基礎 - C++DLL」の版間の差分

ナビゲーションに移動 検索に移動
文字列「source lang」を「syntaxhighlight lang」に置換
(文字列「source lang」を「syntaxhighlight lang」に置換)
10行目: 10行目:
== DllImport属性 ==
== DllImport属性 ==
<code>DLLImport</code>属性は、DLLエントリポイントを定義する関数を記述することで、DLLファイルに定義された関数を呼び出すことができる。<br>
<code>DLLImport</code>属性は、DLLエントリポイントを定義する関数を記述することで、DLLファイルに定義された関数を呼び出すことができる。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  [ DllImport( "DLL名" ) ]
  [ DllImport( "DLL名" ) ]
  </source>
  </source>
19行目: 19行目:
<br>
<br>
以下の例では、<code>EntryPoint</code>フィールドを使用して、<code>MessageBoxA</code>をMsgBoxAに置き換える方法を示している。<br>
以下の例では、<code>EntryPoint</code>フィールドを使用して、<code>MessageBoxA</code>をMsgBoxAに置き換える方法を示している。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  using System;
  using System;
  using System.Runtime.InteropServices;
  using System.Runtime.InteropServices;
31行目: 31行目:
<br>
<br>
また、以下の例では、DLLファイルに定義された関数を序数で指定している。<br>
また、以下の例では、DLLファイルに定義された関数を序数で指定している。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  [DllImport("DllName", EntryPoint = "Functionname")]
  [DllImport("DllName", EntryPoint = "Functionname")]
  [DllImport("DllName", EntryPoint = "#123")]  // 序数を使用するには、序数値の前にシャープ記号#を付ける必要がある
  [DllImport("DllName", EntryPoint = "#123")]  // 序数を使用するには、序数値の前にシャープ記号#を付ける必要がある
93行目: 93行目:
以下の例では、呼び出し規約を適用する方法をCdeclとしている。<br>
以下の例では、呼び出し規約を適用する方法をCdeclとしている。<br>
これは、呼び出し元がスタックをクリーンアップするために使用する必要がある。<br>
これは、呼び出し元がスタックをクリーンアップするために使用する必要がある。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  using System;
  using System;
  using System.Runtime.InteropServices;
  using System.Runtime.InteropServices;
126行目: 126行目:
== StructLayout属性 ==
== StructLayout属性 ==
StructLayout属性とは、クラスまたは構造体のデータメンバを、メモリ内でどのように配置するかを表す。<br>
StructLayout属性とは、クラスまたは構造体のデータメンバを、メモリ内でどのように配置するかを表す。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  [ StructLayout( LayoutKind列挙 ) ]
  [ StructLayout( LayoutKind列挙 ) ]
  </source>
  </source>
158行目: 158行目:
</center>
</center>
<br>
<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  [StructLayout( LayoutKind.Sequential )]
  [StructLayout( LayoutKind.Sequential )]
  public struct Position
  public struct Position
179行目: 179行目:
<br>
<br>
以下の例では、UInt32型はCLSに準拠しないため、<code>CLSCompliant(false)</code>と指定する必要がある。<br>
以下の例では、UInt32型はCLSに準拠しないため、<code>CLSCompliant(false)</code>と指定する必要がある。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  [CLSCompliant( false )]
  [CLSCompliant( false )]
  public int SetValue( UInt32 value );
  public int SetValue( UInt32 value );
185行目: 185行目:
<br>
<br>
以下の例では、CLSCompliantAttribute属性をアセンブリ全体に適用する。<br>
以下の例では、CLSCompliantAttribute属性をアセンブリ全体に適用する。<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  using System;
  using System;
  [assembly: CLSCompliant(true)]
  [assembly: CLSCompliant(true)]
206行目: 206行目:
詳細は、以下のMSDNのWebサイトを参照すること。<br>
詳細は、以下のMSDNのWebサイトを参照すること。<br>
[https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.marshalasattribute.aspx MarshalAsAttribute クラス (System.Runtime.InteropServices) | MSDN]<br>
[https://msdn.microsoft.com/ja-jp/library/system.runtime.interopservices.marshalasattribute.aspx MarshalAsAttribute クラス (System.Runtime.InteropServices) | MSDN]<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  // パラメータへの適用
  // パラメータへの適用
  public void M1([MarshalAs(UnmanagedType.LPWStr)]string msg)
  public void M1([MarshalAs(UnmanagedType.LPWStr)]string msg)
242行目: 242行目:
</center>
</center>
<br>
<br>
  <source lang="csharp">
  <syntaxhighlight lang="csharp">
  void Method([in] int[] array);
  void Method([in] int[] array);
  </source>
  </source>
1,094行目: 1,094行目:
<br>
<br>
まず、以下にC++ DLLを記述する。<br>
まず、以下にC++ DLLを記述する。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  // SampleDLL.h
  // SampleDLL.h
   
   
1,102行目: 1,102行目:
  </source>
  </source>
<br>
<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  // SampleDll.cpp
  // SampleDll.cpp
   
   
1,129行目: 1,129行目:
  </source>
  </source>
<br>
<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  // モジュール定義ファイル
  // モジュール定義ファイル
  // SampleDll.def
  // SampleDll.def
1,145行目: 1,145行目:
以下の例では、ManagedCallBackというデリゲートを宣言している。<br>
以下の例では、ManagedCallBackというデリゲートを宣言している。<br>
C++ DLLのSampleCallBack関数にデリゲートを引数として渡すことで、既知のコールバック形式に自動的に変換される。<br>
C++ DLLのSampleCallBack関数にデリゲートを引数として渡すことで、既知のコールバック形式に自動的に変換される。<br>
  <source lang="c#">
  <syntaxhighlight lang="c#">
  using System;
  using System;
  using System.Runtime.InteropServices;
  using System.Runtime.InteropServices;

案内メニュー