匿名管道的使用,显示CMD命令行内容!

SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0))
{
 ShowMessage("Error On CreatePipe()");
 return;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL,"cmd.exe /c \"dir c:\"" ,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
   ShowMessage("Error on CreateProcess()");
   return;
}
CloseHandle(hWrite);

char buffer[4096] = {0};
DWORD bytesRead;
while (true)
{
   if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
   break;
   mmo1->Lines->Add(buffer);
   Sleep(200);
}

::CloseHandle(hRead);

posted on 2005-11-03 14:09  痛并快乐着  阅读(2418)  评论(0编辑  收藏  举报

导航