Pipe管道读取信息不完全问题

开始将buffer大小设为256,结果读取的消息不完整,缺少一些字符,后来将buffer设大点问题就解决了。
 
function TfrmMain.ReadFromPipe(Pipe: THandle): string;
var
  Buffer: PChar;
  BytesRead: DWORD;
  ReadBuffer: Cardinal;
begin
  Result:= '';
  if GetFileSize(Pipe, nil) = 0 then Exit;

  ReadBuffer:= 2560;
  Buffer := AllocMem(ReadBuffer);
// Buffer:= AllocMem(256);
  repeat
    BytesRead:= 0;
    ReadFile(Pipe, Buffer[0], ReadBuffer, BytesRead, nil);
    if BytesRead > 0 then begin
      Buffer[BytesRead]:= #0;
      OemToAnsi(Buffer, Buffer);
      Result:= string(Buffer);
    end;
  until (BytesRead < ReadBuffer);
  FreeMem(Buffer);
end;



通过 Wiz 发布



posted on 2011-04-15 14:34  windboy03  阅读(505)  评论(0编辑  收藏  举报