次のようにC#で関数を宣言します。
[DllImport(@"MyDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int func1(
int arg1,
StringBuilder arg2,
out int arg3
);
そしてそれをこのように呼びます:
int arg1 = ...;
StringBuilder sb = new StringBuilder(2048);
int arg3;
int retVal = func1(arg1, sb, out arg3);
string arg2 = sb.ToString();
C#はCIntPtr
と一致しないことに注意してくださいint
。はポインタと同じサイズ(32ビットまたは64ビット)でint
あるため、これに一致するC#が必要です。IntPtr
ただしint
、常に4バイトです。
DLLはcdecl呼び出し規約を使用していると思います。stdcallを使用している場合は、明らかな変更を加えることができます。
また、あなたのデータは実際にはテキストデータであると仮定しました。それが単なる古いバイト配列である場合、コードはより単純です。
[DllImport(@"MyDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int func1(
int arg1,
byte[] arg2,
out int arg3
);
そして、呼び出すには:
int arg1 = ...;
byte[] arg2 = new byte[2048];
int arg3;
int retVal = func1(arg1, arg2, out arg3);