vc dll:
 DWORD  __stdcall  readdata(BYTE* pData, size_t iLen); 返回值:成功返回0,不成功返回1
 成功时将iLen指定的byte个数写入pData.vc exe调用dll:
 unsigned char buf[16];
 ReadData(buf,16);
在delphi中调用vc写的dll:  function readdata(pData: PByte; iLen: Integer): Integer; stdcall; external 'cdata.dll';procedure TForm1.Button1Click(Sender: TObject);
var
  buf: array[0..15] of Byte;
begin
  readdata(@buf[0],16);
end;问题:
    为什么只有buf[0]得到返回值,其他buf[1..15]都没有得到值?
    在vc中调用dll是一点问题都没有的,可以正确得到16个byte的值buf[16].
    如何返回16个byte的值?
    应该怎么写?请给出代码.
    ps:谢谢xzgyb(老达摩).

解决方案 »

  1.   

    type
      mybyte=array[0..15] of byte;
    function readdata(pData: mybyte; iLen: Integer): Integer; stdcall; external 'cdata.dll';
      

  2.   

    outer2000(天外流星):
        不行,一样的结果,谢谢.其他方法?
      

  3.   

    先显式的AllocMem一下内存给buf
      

  4.   

    靠,刚打完,一提交出错,害得还要重打
    仔细看了看这个函数,要想根据一个整数参数,
    实现对一个数组的赋值,只有通过指针来实现
    我猜测它里面的结构是这样的
    procedure  readdata(pData: PByte; iLen: Integer);
    var i:integer;
    begin
     for i:=0 to iLen do
     begin
     (pbyte(integer(pdata)+i))^:=i;
     end;
    end;这样你对在赋值
    procedure TForm1.Button1Click(Sender: TObject);
    var
      buf: array[0..3] of Byte;
    begin
      readdata(buf[0],3);
      showmessage(inttostr(buf[0]));
      showmessage(inttostr(buf[1]));
      showmessage(inttostr(buf[2]));
      showmessage(inttostr(buf[3]));
    end;显示0、1、2、3在C里行delphi里不行,我猜可能是内部机制的问题吧?
    语法绝对正确,取值也正确,说明调用正确但是没有正确赋值,可能是指针的偏移量或者数组的结构不一样吧?我的C语言很烂,也不知道说的对不对但是楼主你可以自己实现这个函数,不去调用这个鬼东西了:-)
      

  5.   

    var
      mybuf:^Byte;
    begin
      mybuf := AllocMem(sizeof(Byte)*16);
      readdata(mybuf,16);
    end;
    这样试试
      

  6.   

    myling(阿德):
        其实你的做法跟我的是一样的,所以结果也一样.呵呵,谢谢.
      

  7.   

    peiweiwei(一指残):
        readdata(mybuf,16);
        [Error] : Incompatible types: 'Unit1.Byte' and 'System.Byte'
      

  8.   

    var
      mybuf:Pointer;
    begin
      mybuf := AllocMem(sizeof(Byte)*16);
      readdata(pbyte(mybuf),16);
    end;
      

  9.   

    to peiweiwei(一指残) :
      这样也不行的,和getmem(tmp,16)的方法一样的结果.
      

  10.   

    VC++和Delphi都支持Pascal和C语言两种调用协定。 
        如果使用Pascal协定,VC++的函数定义前要加_stdcall修饰(stdcall是Windows的标准调用方式,可以在所有支持API调用的语言中使用),并且需要在.DEF文件中的EXPORTS端中加入函数名。在Delphi定义时要在函数名后加stdcall修饰(参见帮助)。 
        如果使用C协定,VC++的函数定义前要加__declspec(dllexport)修饰,不需要再修改.DEF文件了。在Delphi定义时要在函数名后加cdecl修饰换换约定试试。
      

  11.   

    zdcnow(磁效应) :
       和你的做法一样?你是指哪个?
       readdata(@buf[0],3);? 我知道这样不行,
       我是说象上面说的那样自己写一个函数,
       不调用这个Dll里的函数了:-)
       不知我那个函数能不能满足你的要求?
      

  12.   

    各位兄弟,搞定了.我用这个dll是访问芯片P89C52BP,问题在芯片的数据返回函数和PC的dll返回不一致.谢谢各位.结贴.