BOOL CDOSDlg::ExecDosCmd()
{
#define EXECDOSCMD "dir c:"
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0))
{
return FALSE;
}
char command[1024];
strcpy(command,"Cmd.exe /C ");
strcat(command,EXECDOSCMD);
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, command,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
CloseHandle(hWrite);
CloseHandle(hRead);
return FALSE;
}
CloseHandle(hWrite);
char buffer[4096] = {0};
DWORD bytesRead;
while (true)
{
if (!ReadFile(hRead,buffer,4095,&bytesRead,NULL))
break;
AfxMessageBox(buffer); //
}
CloseHandle(hRead);
return TRUE;
}
------------------------
如果#define EXECDOSCMD "dir c:" 换成 "fc e:\\1.txt e:\\2.txt" 就在
if (!ReadFile(hRead,buffer,4095,&bytesRead,NULL))
break;
程序就跳出了?
原因是为什么?为什么dir的输出就可以正常通过管道读取 而fc的输出就不行?
已经在CMD下测试过FC命令,CMD下正常显示结果