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;
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;