为什么这个循环跳不出来呢?
do
{
BytesRead = 0;
ReadFile(ReadPipe,Buffer,ReadBuffer,&BytesRead,NULL);
Buffer[BytesRead]= '\0';
OemToAnsi(Buffer,Buffer);
Memo1->Text =Memo1->Text + String(Buffer);
}while (BytesRead < ReadBuffer);
用于创建管道并发送命令和接受执行的结果,为什么跳不出来??编译无错
完整代码:
bool TNmap_Win::RunDosInMemo(String strCmdLine, TMemo * mmoOut)
{
const int ReadBuffer=2048;
const char CommandLine[]="ping 127.0.0.1";
//bool bRet = false;
TSecurityAttributes Security;
HANDLE ReadPipe,WritePipe;
STARTUPINFO Start={sizeof(Start)};
PROCESS_INFORMATION ProcessInfo;
OVERLAPPED olp={sizeof(olp)};
char * Buffer;
DWord BytesRead,Apprunning,Result;
Security.nLength= sizeof(TSecurityAttributes);
Security.bInheritHandle =true;
Security.lpSecurityDescriptor =NULL;
if (!CreatePipe (&ReadPipe, &WritePipe, &Security, 0)==0)
{
//Edit1->Text = IntToStr(GetLastError());
Buffer= new char(ReadBuffer + 1);
memset(&Start,sizeof(Start),0);
Start.cb= sizeof(Start);
Start.hStdOutput= WritePipe;
Start.hStdError= WritePipe;
Start.dwFlags= STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
Start.wShowWindow= SW_HIDE;
if (CreateProcess(NULL,PChar(CommandLine),&Security,&Security,true,NORMAL_PRIORITY_CLASS,NULL,NULL,&Start,&ProcessInfo))
{
CloseHandle(WritePipe);
do
{
do
{
BytesRead= 0;
ReadFile(ReadPipe,Buffer,ReadBuffer,&BytesRead,NULL);
Buffer[BytesRead]= '\0';
OemToAnsi(Buffer,Buffer);
Memo1->Text= Memo1->Text + String(Buffer);
}
while (BytesRead < ReadBuffer);
//Apprunning->WaitForSingleObject(ProcessInfo.hProcess, 100);
Application->ProcessMessages();
}
while (Apprunning != WAIT_TIMEOUT);
GetExitCodeProcess(ProcessInfo.hProcess, &Result);
do
{
BytesRead = 0;
ReadFile(ReadPipe,Buffer,ReadBuffer,&BytesRead,NULL);
Buffer[BytesRead]= '\0';
OemToAnsi(Buffer,Buffer);
Memo1->Text =Memo1->Text + String(Buffer);
}while (BytesRead < ReadBuffer);
};
Memo1->Perform(WM_VSCROLL,SB_BOTTOM ,0);
Memo1->SelStart=Memo1->Text.Length() ;
Memo1->SetFocus();
delete []Buffer;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
};
}