1

重複の可能性:
unsigned char ** c#と同等であり、ファイルに戻り値を書き込む必要があります

win32dll関数を呼び出す必要があります

int func1(int arg1, unsigned char *arg2, int *arg3);

そして私はラップされたc#を次のように書きました

public extern int fuc1(int arg1, out IntPtr arg2, out IntPtr arg3);

arg2は2048バイトを割り当て、それをwin32dllに送信する必要があります。arg2とarg3を出力として取得します。

c#テストアプリケーションとc#ラッパーでどのように宣言できますか?私はそれを正しくやっていますか?

4

2 に答える 2

4

C#のバイトはunsigned8ビットintです。byte[]はそれらの配列です。この配列へのポインタを取得するには、次を使用します。

 var myArray = new byte[2048];
 fixed(byte* arg2 = myArray)
 {
      // use arg2
 }

また:

 var myArray = new byte[2048];
 GCHandle pinnedRawData = GCHandle.Alloc(myArray, GCHandleType.Pinned);
 try
 {  
    // Get the address of the data array
    IntPtr pinnedRawDataPtr = pinnedRawData.AddrOfPinnedObject();
 }
 finally
 {
    // must explicitly release
    pinnedRawData.Free(); 
 } 

または、呼び出された関数が配列へのポインターをキャッシュしない場合は、次のようにするだけです。

 public static extern int fuc1(int arg1, [In,Out] byte[] arg2, ref int arg3);

 var arg1 = 0;
 var arg2 = new byte[2048];
 int arg3 = 42; // If this value won't be used, you can skip initializing arg3 and mark arg3 as out instead of ref (of course, this is pedantic and extraneous, and C# shouldn't even have 'out' as a keyword)

 func1(arg1, arg2, ref arg3);

P/Invokeはそれを自動的に固定します。

タイプのMSDNマーシャリング配列

関連するSOの質問

于 2012-12-22T07:39:58.910 に答える
1

次のように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);
于 2012-12-22T09:22:57.520 に答える