关于readfile一个简单的问题!(50分)

黑狼

Unregistered / Unconfirmed
GUEST, unregistred user!
在我对com端口操作时遇到了一个这样的问题。
为什么readfile这个函数接收数据时只能接受静态数组?而动态数组不可以?
procedure TForm1.btnreciveClick(Sender: TObject);
var
cs:tcomstat;
dwerror,nbytesread:longword;
ary:array of byte;//接收数据缓冲区:动态数组
i:integer;
begin
clearcommerror(hcom1,dwerror,@cs);
if cs.cbInQue=0 then
begin
form1.Caption:='没有数据';
exit;
end;
if cs.cbInQue>sizeof(ary) then
begin
form1.Caption:='缓冲区溢出!';
exit;
end;
setlength(ary,cs.cbinque);//将缓冲区的大小设为cs.cbinque
readfile(hcom1,ary,cs.cbInQue,nbytesread,nil);
//注:当执行完以上语句后,我调试时发现ary数组已经空了。
for i:=low(ary) to high(ary) do
listbox1.itmes.add(inttostr(ary));
end;
以上就是我的问题。我想是不是readfile这个函数不接受动态数组,但是如果要不接受动态
数组,那么在程序编译时delphi应该会出现提示。这是为什么?
 
readfile(hcom1,ary^,cs.cbInQue,nbytesread,nil);
~~~~~
动态数组的数组名其实是个指针。
 
动态数组的数组名其实是个指针不能使用
 
动态数组的数组名是指针吗?
 
是的, 这样也可以..
readfile(hcom1,ary[1],cs.cbInQue,nbytesread,nil);
 
多人接受答案了。
 
顶部