我用C#调用Delphi写的dll,遇到以下问题:Delphi接口函数function W_information(var info:integer):integer;info是数组首地址function W_flag(var f:boolean):integer;f是数组首地址C#调用声明[DllImport("D.dll")]static int32 W_information(ref int32 info):integer;[DllImport("D.dll")]static int32 W_flag(ref bool f):integer; private write_onclick(){  int32[] info=new int32[3];  info[0]=1;  info[1]=2;  info[2]=3;  W_information(ref info[0]);  bool[] f=new bool[2];  f[0]=false;  f[1]=true;  W_flag(ref f[0]);}W_information操作的结果是正确的,但W_flag操作的结果是错误的。我之前以为是f数组的地址不连续导致,但我查过资料,数组的地址都是连续的。

解决方案 »

  1.   

    没有报错,但是W_flag返回的Int值不正确。例如f[8]={false,true,true,false,false,false,false,false},通过W_flag(f[0])返回8,正确结果应该是96。
      

  2.   

    D.dll是我测试动态库用的,我将D.dll里面W_Flag改为function W_Flag(var b:integer):integer;
    然后调用时,f定义为int32[] f = new int32[8](真假分别用0和1表示),这样调用,就不会出现错误,为什么呢?
      

  3.   

    数据类型我也查过相关资料Delphi的boolean是对应C#的bool类型,但关于这样用的,查不到相关的资料。谁来救救我!!!!!!!!!!!!!!!!!!!!!!!!
      

  4.   

    问题终于自己解决了,调用时,将类型声明为byte,具体改为:
    Delphi接口函数function W_information(var info:integer):integer;info是数组首地址function W_flag(var f:boolean):integer;f是数组首地址C#调用声明[DllImport("D.dll")]static int32 W_information(ref int32 info):integer;[DllImport("D.dll")]static int32 W_flag(ref byte f):integer; private write_onclick(){  int32[] info=new int32[3];  info[0]=1;  info[1]=2;  info[2]=3;  W_information(ref info[0]);  byte[] f=new byte[2];  f[0]=Convert.ToByte(false);  f[1]=Convert.ToByte(true);  W_flag(ref f[0]);}
    问题解决