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(老达摩).
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(老达摩).
mybyte=array[0..15] of byte;
function readdata(pData: mybyte; iLen: Integer): Integer; stdcall; external 'cdata.dll';
不行,一样的结果,谢谢.其他方法?
仔细看了看这个函数,要想根据一个整数参数,
实现对一个数组的赋值,只有通过指针来实现
我猜测它里面的结构是这样的
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语言很烂,也不知道说的对不对但是楼主你可以自己实现这个函数,不去调用这个鬼东西了:-)
mybuf:^Byte;
begin
mybuf := AllocMem(sizeof(Byte)*16);
readdata(mybuf,16);
end;
这样试试
其实你的做法跟我的是一样的,所以结果也一样.呵呵,谢谢.
readdata(mybuf,16);
[Error] : Incompatible types: 'Unit1.Byte' and 'System.Byte'
mybuf:Pointer;
begin
mybuf := AllocMem(sizeof(Byte)*16);
readdata(pbyte(mybuf),16);
end;
这样也不行的,和getmem(tmp,16)的方法一样的结果.
如果使用Pascal协定,VC++的函数定义前要加_stdcall修饰(stdcall是Windows的标准调用方式,可以在所有支持API调用的语言中使用),并且需要在.DEF文件中的EXPORTS端中加入函数名。在Delphi定义时要在函数名后加stdcall修饰(参见帮助)。
如果使用C协定,VC++的函数定义前要加__declspec(dllexport)修饰,不需要再修改.DEF文件了。在Delphi定义时要在函数名后加cdecl修饰换换约定试试。
和你的做法一样?你是指哪个?
readdata(@buf[0],3);? 我知道这样不行,
我是说象上面说的那样自己写一个函数,
不调用这个Dll里的函数了:-)
不知我那个函数能不能满足你的要求?