私はC#からC++の関数を呼び出すためにDllImportを使用しています。 C#でポインタ関数をC++からC#にエクスポートするには?
#if defined(__cplusplus)
extern "C" {
#endif
__declspec(dllexport) int __stdcall ABC(int i);
__declspec(dllexport) char* __stdcall C(int i);
#if defined(__cplusplus)
}
#endif
int __stdcall ABC(int i)
{
return i;
}
char* __stdcall C(int i)
{
char* n = new char[i];
memset(n, 9, i);
return n;
}
コードである:
using System.Runtime.InteropServices;
using System;
namespace DepartmentStore
{
class Exercise
{
[DllImport("library.dll")]
public static extern int ABC(int i);
[DllImport("library.dll")]
public static extern char* C(int i);
static int Main()
{
int k = ABC(10);
byte[] b = C(1024);
return 0;
}
}
}
ABC(int i)
がOKである関数が、関数C(int i)
は、次のエラーを生じた場合、建物:
「ポインタと固定サイズのバッファのみかもしれません安全でない状況で使用する」
私は関数のポインタリターンをエクスポートする方法を理解していなかったと思います。
誰かが私に正しい方法を教えてください。C#は関数の戻り値の型をポインタとして呼び出しますか?
戻り値の型を代わりにIntPtr、Marshal.PtrToStringAnsi()を文字列に変換して宣言します。プラグインできないメモリリークを修正することはできません。このAPIはそのまま使用できません。 –