13,002
回編集
100行目: | 100行目: | ||
== C++/CLIのラッパープロジェクトを使用する方法 == | == C++/CLIのラッパープロジェクトを使用する方法 == | ||
==== C#ライブラリの作成 ==== | |||
まず、C#ライブラリを作成する。<br> | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
// CSharpDLL.cs | // CSharpDLL.cs | ||
124行目: | 125行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
==== C++/CLIライブラリの作成 ==== | |||
次に、C++/CLIライブラリを作成する。<br> | |||
<br> | |||
ソリューションエクスプローラからC++/CLIプロジェクトを右クリックして[参照の追加]を選択、上記で作成したC# DLLファイルを追加する。<br> | |||
<br> | <br> | ||
また、Visual C++のプロジェクト設定を開いて、[構成プロパティ] - [全般] - [共通言語ランタイム サポート (/clr)]に変更する。<br> | また、Visual C++のプロジェクト設定を開いて、[構成プロパティ] - [全般] - [共通言語ランタイム サポート (/clr)]に変更する。<br> | ||
168行目: | 171行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
==== C++実行バイナリの作成 ==== | |||
C++実行バイナリのプロジェクトを作成する。<br> | |||
<br> | |||
===== 暗黙的リンクを行う場合 ===== | |||
メニューバーから[Visual C++のプロジェクト設定]を選択して、[構成プロパティ] - [C/C++] - [全般] - [追加のインクルードディレクトリ]項目に、<br> | |||
CppCLIDLL.hが存在するディレクトリを追加する。<br> | CppCLIDLL.hが存在するディレクトリを追加する。<br> | ||
<br> | <br> | ||
176行目: | 182行目: | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// CppEXE.cpp | // CppEXE.cpp | ||
#include "stdafx.h" | #include "stdafx.h" | ||
#include <windows.h> | #include <windows.h> | ||
#include " | #include "CppEXE.h" | ||
#include " | #include "CppCLIDLL.h" | ||
int _tmain() | int _tmain() | ||
202行目: | 209行目: | ||
} | } | ||
system("pause"); | system("pause"); | ||
return 0; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
===== 明示的リンクを行う場合 ===== | |||
# まず、<code>LoadLibrary</code>関数を使用して、C++/CLIライブラリを読み込む。<br> | |||
# 次に、<code>GetProcAddress</code>関数を使用して、C++/CLIライブラリ内の関数オブジェクトのアドレスを取得する。<br> | |||
# C++/CLIライブラリの関数を呼び出す。 | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// CppEXE.cpp | |||
#include "stdafx.h" | |||
#include <windows.h> | |||
#include "CppEXE.h" | |||
using fnShowMessageBox void(*)(int*); | |||
int _tmain() | |||
{ | |||
int result = 0; | |||
// C++/CLIライブラリを呼び出す | |||
auto hModule = ::LoadLibrary(L"CppCLIDLL.dll"); | |||
if (NULL == hModule) { | |||
return -1; | |||
} | |||
// C++/CLIライブラリの関数を読み込む | |||
auto ShowMessageBox = reinterpret_cast<fnShowMessageBox>(::GetProcAddress(hModule, "ShowMessageBox")); | |||
// C++/CLIライブラリの関数を実行する | |||
ShowMessageBox(&result); | |||
return 0; | return 0; |