久しぶりにSEっぽいことを書こうかなと
C++で作られたdllをC#で呼ぶ
ってな仕事をしているので備忘録として残しておきます。
保証とかは何もありません。
使う場合は全てにおいて自己責任で
参考までにということでお願いします。m(__)m
指摘があったら教えてください。何せやる気のないSEですから
C++側の実装
Test.h
-------------------------
#ifdef __cplusplus
extern "C" {
#endif
// C#からコールできるように公開する
__declspec( dllexport ) const char* GetTestMessage();
#ifdef __cplusplus
}
#endif
const char Message[256] = "Test";
Test.cpp
-------------------------
// 【公開API】
const char* GetTestMessage()
{
return Message;
}
C#側の実装
Test.cs
-------------------------
[DllImport("TEST.dll")]
private static extern IntPtr GetMessage();
IntPtr pMsg = EIDGetScriptErrMessage();
String szMsg = Marshal.PtrToStringAnsi(pMsg);
他にもいろんなパターンのAPIが
あったけどとりあえず今日はこれで。。。