📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の7版が非表示) | |||
| 247行目: | 247行目: | ||
<br><br> | <br><br> | ||
== C# | == C#のデータ型とC++のデータ型 == | ||
<center> | <center> | ||
{| class="wikitable" | {| class="wikitable" | style="background-color:#fefefe;" | ||
|- | |- | ||
! Windows APIのデータ型<br>(括弧内は対応するC言語の型) ! | ! style="background-color:#66CCFF;" | Windows APIのデータ型<br>(括弧内は対応するC言語の型) | ||
! style="background-color:#66CCFF;" | 対応するC#のデータ型<br>(括弧内は.NET Frameworkでの型名) | |||
! style="background-color:#66CCFF;" | 備考 | |||
|- | |- | ||
| HANDLE (void *) || System.IntPtr<br>System.UIntPtr || x86は4バイト<br>x64は8バイト | | HANDLE (void *)<br>HMODULE<br>HINSTANCE<br>HWND<br>HWM<br>HGDIOBJ<br>HDC || System.IntPtr<br>System.UIntPtr || x86は4バイト<br>x64は8バイト | ||
|- | |- | ||
| BYTE (unsigned char) || byte (System.Byte) || | | char 配列名 [配列サイズ] || MarshalAs(UnmanagedType.ByValTStr, SizeConst = 配列のサイズ)] public string 配列名 || | ||
|- | |||
| char ** || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | |||
|- | |||
| BYTE (unsigned char)<br>UCHAR || byte (System.Byte) || UCHAR型は、C#のstring型およびIntPtr型でも可能。 | |||
|- | |||
| BYTE[] (unsigned char *)<br>PBYTE (byte *) || System.Byte[]<br>[MarshalAs(UnmanagedType.LPArray)] byte[]<br>[MarshalAs(UnmanagedType.LPArray)] Intptr || | |||
|- | |||
| unsigned char & || ref byte || | |||
|- | |- | ||
| SHORT (short) || short (System.Int16) || | | SHORT (short) || short (System.Int16) || | ||
| 261行目: | 271行目: | ||
| WORD (unsigned short) || ushort (System.UInt16) || | | WORD (unsigned short) || ushort (System.UInt16) || | ||
|- | |- | ||
| INT (int)<br>LONG (long) || int (System.Int32) || | | INT (int)<br>LONG (long)<br>DWORDLONG<br>HPARAM<br>LPARAM<br>WPARAM || int (System.Int32) || System.Int16でも取得できる可能性がある。 | ||
|- | |- | ||
| UINT (unsigned int)<br>DWORD | | UINT (unsigned int)<br>DWORD (unsigned long)<br>ULONG (unsigned long) || uint (System.UInt32) || | ||
|- | |||
| DECIMAL || decimal (System.Decimal) || | |||
|- | |||
| bool || bool (System.Boolean) || | |||
|- | |- | ||
| BOOL (long) || bool (System.Boolean) || | | BOOL (long) || bool (System.Boolean) || | ||
| 269行目: | 283行目: | ||
| CHAR (char) || 文字を渡すとき<br>char (System.Char)<br>文字を受け取るとき<br>StringBuilder || | | CHAR (char) || 文字を渡すとき<br>char (System.Char)<br>文字を受け取るとき<br>StringBuilder || | ||
|- | |- | ||
| WCHAR(wchar_t) || 文字を渡すとき<br>char (System.Char)<br>文字を受け取るとき<br>StringBuilder || | | WCHAR (wchar_t) || 文字を渡すとき<br>char (System.Char)<br>文字を受け取るとき<br>StringBuilder || | ||
|- | |- | ||
| LPSTR (char *, char[])<br>LPWSTR (wchar_t *, wchar_t[]) || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | | PCAHR (char *)<br>LPSTR (char *, char[])<br>LPWSTR (wchar_t *, wchar_t[]) || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | ||
|- | |- | ||
| LPCSTR (const char *, const char[])<br>LPCWSTR (const wchar_t *, const wchar_t[]) || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | | LPCSTR (const char *, const char[])<br>LPCWSTR (const wchar_t *, const wchar_t[]) || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | ||
|- | |||
| LPTSTR (char * または wchar_t *) || 文字を渡すとき<br>[MarshalAs(UnmanagedType.LPTStr)] string<br>文字を受け取るとき<br>System.Text.StringBuilder || | |||
|- | |||
| LPCTSTR (const char * または const wchar_t *) || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | |||
|- | |||
| BSTR || 文字を渡すとき<br>string (System.String)<br>文字を受け取るとき<br>System.Text.StringBuilder || | |||
|- | |- | ||
| FLOAT (float) || float (System.Single) || | | FLOAT (float) || float (System.Single) || | ||
|- | |||
| float <配列名>[]<br>float <配列名> [配列サイズ] || float[]<br>ref float[] || | |||
|- | |- | ||
| DOUBLE (double) || double (System.Double) || | | DOUBLE (double) || double (System.Double) || | ||
|- | |||
| double <配列名>[]<br>double <配列名> [配列サイズ] || double[]<br>ref double[] || | |||
|- | |||
| VARIANT || System.Object || | |||
|- | |||
| COLORREF || uint || | |||
|- | |||
| GUID || Guid || | |||
|- | |||
| struct <構造体名> <変数名> || ref <構造体名> <変数名> || | |||
|- | |||
| struct <構造体名> || public struct <構造体名> || <u>out <構造体名></u>であらかじめ構造体をインスタンス化して、変数名を宣言する。 | |||
|} | |} | ||
</center> | </center> | ||
| 776行目: | 810行目: | ||
== 文字列のマーシャリング (2) == | == 文字列のマーシャリング (2) == | ||
C++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> | ==== Linux ==== | ||
Linux向けC++ライブラリを記述する。<br> | |||
<syntaxhighlight lang="c++"> | |||
// SampleLib.cpp | |||
#include <cstdio> | |||
extern "C" double __attribute__((visibility("default"))) SampleFunc01(int a) | |||
{ | |||
printf("--<SampleDll:SampleFunc01>--\r\n"); | |||
printf("a = %d\r\n", a); | |||
printf("---------------------------\r\n"); | |||
return 3.14; | |||
} | |||
extern "C" void __attribute__((visibility("default"))) SampleFunc02(int a, char *pstr) | |||
{ | |||
printf("--<SampleDll:SampleFunc02>--\r\n"); | |||
printf("[%d] %s\r\n", a, pstr); | |||
printf("-----------------------------\r\n"); | |||
} | |||
extern "C" void __attribute__((visibility("default"))) SampleFunc03(int a, char *pstr) | |||
{ | |||
printf("--<SampleDll:SampleFunc03>--\r\n"); | |||
printf("[%d] %s\r\n", a, pstr); | |||
sprintf(pstr, u8"C++ DLL側から文字列を返す場合は、StringBuilderクラスを使用する"); | |||
printf("------------------------\r\n"); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== Windows ==== | |||
Windows向けC++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> | |||
<br> | <br> | ||
下記に、C++ DLLを記述する。<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// SampleDLL.h | // SampleDLL.h | ||
| 831行目: | 899行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
==== Linux / Windows ==== | |||
次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | 次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | ||
<br> | <br> | ||
| 851行目: | 920行目: | ||
/// <param name="a">4 バイト符号付き整数を指定します。</param> | /// <param name="a">4 バイト符号付き整数を指定します。</param> | ||
/// <returns>倍精度浮動小数を返します。</returns> | /// <returns>倍精度浮動小数を返します。</returns> | ||
[DllImport("SampleDLL.dll")] | [DllImport("SampleDLL.dll")] // Windows | ||
[DllImport("SampleLib.so")] // Linux | |||
private static extern double SampleFunc01(int a); | private static extern double SampleFunc01(int a); | ||
[DllImport("SampleDLL.dll", CharSet = CharSet.Unicode)] | [DllImport("SampleDLL.dll", CharSet = CharSet.Unicode)] // Windows | ||
[DllImport("SampleLib.so", CharSet = CharSet.Auto)] // Linux | |||
// C++ DLL側の文字コードがUnicodeの場合は"CharSet = CharSet.Unicode"と明示的に指定する | // C++ DLL側の文字コードがUnicodeの場合は"CharSet = CharSet.Unicode"と明示的に指定する | ||
private static extern void SampleFunc02(int a, string str); | private static extern void SampleFunc02(int a, string str); | ||
[DllImport("SampleDLL.dll", CharSet = CharSet.Unicode)] | [DllImport("SampleDLL.dll", CharSet = CharSet.Unicode)] // Windows | ||
[DllImport("SampleLib.so", CharSet = CharSet.Auto)] // Linux | |||
// C++ DLL側の文字コードがUnicodeの場合は"CharSet = CharSet.Unicode"と明示的に指定する | // C++ DLL側の文字コードがUnicodeの場合は"CharSet = CharSet.Unicode"と明示的に指定する | ||
private static extern void SampleFunc03(int a, StringBuilder str); | private static extern void SampleFunc03(int a, StringBuilder str); | ||
| 1,123行目: | 1,195行目: | ||
== 関数ポインタのマーシャリング == | == 関数ポインタのマーシャリング == | ||
C# EXEからC++ DLLへコールバック関数を渡す。<br> | |||
<br> | |||
ただし、<code>Func</code>型、<code>Action</code>型、構造体内にある<code>char</code>型の配列のようなNon-Bittable型(非Blittable型)が含まれている場合、以下の例外が発生する。<br> | |||
このような型の場合は、属性を用いたマーシャルの変換は不可能である。<br> | |||
Non-blittable generic types cannot be marshaled. | |||
<br> | |||
==== Linux ==== | |||
Linux向けのC++ライブラリを記述する。<br> | |||
<syntaxhighlight lang="c++"> | |||
// SampleLib.cpp | |||
#include <cstdio> | |||
#include <cstring> | |||
using UnManagedCallback = bool (*)(int); | |||
int __stdcall SampleCallback(UnManagedCallback CallbackFunc, int lPalam) | |||
{ | |||
int iRet = 0; | |||
bool bRet = CallbackFunc(lPalam); | |||
if(bRet) | |||
{ | |||
iRet = 1; | |||
} | |||
else | |||
{ | |||
iRet = -1; | |||
} | |||
return iRet; | |||
} | |||
</syntaxhighlight> | |||
<br> | <br> | ||
==== Windows ==== | |||
Windows向けのC++ DLLを記述する。<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// SampleDLL.h | // SampleDLL.h | ||
| 1,172行目: | 1,277行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
==== Linux / Windows ==== | |||
次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | 次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | ||
<br> | <br> | ||
| 1,187行目: | 1,293行目: | ||
class Program | class Program | ||
{ | { | ||
[DllImport("SampleDLL.dll")] | [DllImport("SampleDLL.dll")] // Windows | ||
[DllImport("SampleLib.so")] // Linux | |||
private static extern int SampleCallback(ManagedCallBack CallBack, int lPalam); | private static extern int SampleCallback(ManagedCallBack CallBack, int lPalam); | ||
| 1,209行目: | 1,316行目: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | |||
== トラブル対処 == | == トラブル対処 == | ||