0

//デルファイコード

procedure SendData(data:pointer;size:cardinal); stdcall;
   begin
    c.SendData(data,size);
   end;

  function GetEvent(out tag:cardinal):integer; stdcall;
   begin
    result:=0;
    if qEnd<>qStart then begin
     result:=queue[qStart].eventCode;
     tag:=queue[qStart].eventTag;
     inc(qStart);
    end;
   end;

  function GetMessage(handle:cardinal;out data:pointer):cardinal; stdcall;
   begin
    result:=GetMsg(handle,data);
   end;

私はunity3dエンジンを使用しており、そこからc#を使用しています。そして、delphiから作成されたdllファイルを統合して呼び出す必要があります。

だから私はc#で書いた、

using UnityEngine;
using System.Collections;
using System; 
using System.Text; 
using System.Collections.Generic; 
using System.Runtime.InteropServices;

public class test : MonoBehaviour {
    [DllImport ("ServerTool")]
    private static extern void Connect(int a);
    [DllImport ("ServerTool")]
    private static extern int GetEvent();
    [DllImport ("ServerTool")]
    private static extern void SendData(IntPtr p,  int b);

    // Use this for initialization
    void Start () {
        Connect(0);

        SendData("asdf", 1);
        Debug.Log(GetEvent());
    }

    // Update is called once per frame
    void Update () {

    }
}

これはエラーが発生すると言います、

Assets / test.cs(20,17):エラー1502:test.SendData(System.IntPtr、int)に最適なオーバーロードされたメソッドの一致にいくつかの無効な引数がありますAssets / test.cs(20,17):エラー1503:引数/ #/1は文字列式をタイプ`System.IntPtr'に変換できません

デルファイについてはよくわかりませんが、ポインタはc#のオブジェクトに似ていますね。そこで、IntPtrをobjectに変更しようとすると、コンパイルは完了しますが、コードを実行すると、エラーが表示されます。

MarshalDirectiveException:タイプオブジェクトのマーシャリングは実装されていませんtest.Start()(Assets / test.cs:20)

次に、c#から上記のdelphiのSendData関数を呼び出すにはどうすればよいですか?

ありがとう。

4

2 に答える 2

1

C# のマーシャリングを使用する必要があります。

あなたの場合、文字列パラメーターを取り、IntPtr を返す Marshal.StringToHGlobalAnsi を使用して、文字列を IntPtr に変換する必要があります。

MSDN ドキュメントを参照してください。

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.stringtohglobalansi.aspx

David から述べたように、FreeHGlobal も使用する必要があります。

于 2012-06-19T06:01:17.380 に答える
0

ポインターの型は問題ないはずです。あなたの整数は間違っています。Delphi のカーディナルは、32 ビット長の符号なし整数(0 ~ 4294967295)です。DocWiki リファレンス

于 2012-06-19T09:30:09.763 に答える