DLLImport を使用して C++ DLL 内の関数を呼び出す C# コードをいくつか実行しています。
[DllImport("my.dll", EntryPoint = "#16", CallingConvention = CallingConvention.StdCall)]
private static extern void sendstring(string s);
C# では次のように呼び出します。
sendstring("Test1\\0test2\\0");
私の C++ DLL は static const char XY[] = "Test1\0test2\0"; を作成する必要があります。これは、次のように C++ DLL 内から別の DLL 関数を呼び出すために必要だからです。
functiontootherdll(sizeof(s),(void*)s);
したがって、C++ での私のコード:
extern "C" {
void MyClass::sendstring( const char *s) {
functiontootherdll(sizeof(s),(void*)s);
}
問題: 次のように C++ DLL 内で手動で定義すると、機能します。
static const char Teststring[] = "Test1\0test2\0";
functiontootherdll(sizeof(Teststring),(void*)Teststring);
しかし、私のC#ファイルからこれを呼び出すときに const char *s を取っていません(呼び出された他のdllとは異なるエラーを報告します)。const char * を static const char s[] などにキャストする方法を知る必要があります。
お気づきのように、私はこれらすべてについてほとんど手がかりがないので、どんな助けも大歓迎です!