13,009
回編集
608行目: | 608行目: | ||
<br> | <br> | ||
==== 文字列のマーシャリング ==== | ==== 文字列のマーシャリング (1) ==== | ||
C++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> | |||
<br> | |||
下記にもC++ DLLを記述する。<br> | |||
<syntaxhighlight lang="c++"> | |||
// SampleDLL.h | |||
int __stdcall Mul(int x, int y) | |||
void __stdcall MulUsePointer(int *x, int *y, int *result) | |||
void __stdcall SetNameStr(const char *t) | |||
const char* __stdcall GetNameStr() | |||
void __stdcall SetNameWStr(const wchar_t *t) | |||
const wchar_t* __stdcall GetNameWStr() | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// SampleDll.cpp | |||
#include <stdio.h> | |||
#include <string.h> | |||
#include "SampleDll.h" | |||
static std::string _str = ""; | |||
static std::wstring _wstr = L""; | |||
int __stdcall Mul(int x, int y) | |||
{ | |||
return x * y; | |||
} | |||
void __stdcall MulUsePointer(int *x, int *y, int *result) | |||
{ | |||
*result = (*x) * (*y); | |||
} | |||
void __stdcall SetNameStr(const char *t) | |||
{ | |||
_str = std::string(t); | |||
} | |||
const char* __stdcall GetNameStr() | |||
{ | |||
return _str.c_str(); | |||
} | |||
void __stdcall SetNameWStr(const wchar_t *t) | |||
{ | |||
_wstr = std::wstring(t); | |||
} | |||
const wchar_t * __stdcall GetNameWStr() | |||
{ | |||
return _wstr.c_str(); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// SampleDll.def // モジュール定義ファイル | |||
LIBRARY SampleDll | |||
EXPORTS | |||
; 公開する関数名をリストアップ | |||
Mul @1 | |||
MulUsePointer @2 | |||
SetNameStr @3 | |||
GetNameStr @4 | |||
SetNameWStr @5 | |||
GetNameWStr @6 | |||
</syntaxhighlight> | |||
<br> | |||
次に、C# EXEからC++ DLLを呼び出す方法を記述する。<br> | |||
<br> | |||
DllImpoort文は、DLLファイル名を指定する。<br> | |||
この時、C#の実行ファイルと同一階層にDLLファイルがある場合、DLLファイル名のみを指定するだけでよい。<br> | |||
異なる階層にDLLファイルがある場合、フルパスで指定する必要がある。<br> | |||
<br> | |||
C++ DLLの関数を実行する時、<u>"エントリーポイントが見つかりません。"</u>とエラーが出力される場合、DllImport文に誤りがある可能性が高い。<br> | |||
<br> | |||
<syntaxhighlight lang="c#"> | |||
using System; | |||
using System.Runtime.InteropServices; | |||
namespace SampleCSharp | |||
{ | |||
class Program | |||
{ | |||
[DllImport("SampleDLL", EntryPoint = "Mul")] | |||
static extern int _Mul(int x, int y); | |||
[DllImport("SampleDLL", EntryPoint = "MulUsePointer")] | |||
static extern void _MulUsePointer(ref int x, ref int y, out int result); | |||
[DllImport("SampleDLL", EntryPoint = "SetNameStr", CharSet = CharSet.Ansi)] | |||
static extern void _SetNameStr(string t); | |||
[DllImport("SampleDLL", EntryPoint = "GetNameStr", CharSet = CharSet.Ansi)] | |||
static extern IntPtr _GetNameStr(); | |||
[DllImport("SampleDLL", EntryPoint = "SetNameWStr", CharSet = CharSet.Unicode)] | |||
static extern void _SetNameWStr(string t); | |||
[DllImport("SampleDLL", EntryPoint = "GetNameWStr", CharSet = CharSet.Unicode)] | |||
static extern IntPtr _GetNameWStr(); | |||
static void Main(string[] args) | |||
{ | |||
int x = 10; | |||
int y = 20; | |||
int resultMul = _Mul(x, y); | |||
Console.WriteLine("Mul = " + resultMul); | |||
_MulUsePointer(ref x, ref y, out int resultMulUsePointer); | |||
Console.WriteLine("MulUsePointer = " + resultMulUsePointer); | |||
string testString = "東京都新宿 1-1"; | |||
_SetNameStr(testString); | |||
Console.WriteLine("GetNameStr = " + Marshal.PtrToStringAnsi(_GetNameStr())); | |||
_SetNameWStr(testString); | |||
Console.WriteLine("GetNameWStr = " + Marshal.PtrToStringUni(_GetNameWStr())); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
==== 文字列のマーシャリング (2) ==== | |||
C++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> | C++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> | ||
<br> | <br> | ||
715行目: | 842行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
==== 構造体のマーシャリング ==== | ==== 構造体のマーシャリング ==== | ||
C++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> | C++ DLLの作成方法は[[ライブラリの基礎 - DLLの作成(C/C++/MFC)|ライブラリの基礎 - DLLの作成(C/C++/MFC)]]を参照する。<br> |