实时获取命令行输出 ,appendLine 需要 processEvents 强制刷新,不是不能实时更新
extern QApplication* pa;
void Window::appendLine(QString str){
logTxt->append(str);
pa->processEvents();
}
std::string ExeVlcCmd(wstring szIperfCmd) { //创建匿名管道 SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; HANDLE hRead, hWrite; if (!CreatePipe(&hRead, &hWrite, &sa, 0)){ return (""); } //设置命令行进程启动信息(以隐藏方式启动命令并定位其输出到hWrite) STARTUPINFO si = { sizeof(STARTUPINFO) }; GetStartupInfo(&si); si.dwFlags = /*STARTF_USESHOWWINDOW | */STARTF_USESTDHANDLES; si.wShowWindow = SW_HIDE;//SW_SHOWNORMAL;//SW_SHOWNORMAL;//SW_HIDE;// si.hStdError = hWrite; si.hStdOutput = hWrite; //启动命令行 PROCESS_INFORMATION pi; if (!CreateProcess(NULL, (TCHAR *)szIperfCmd.c_str(), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)){ //if (!CreateProcess(NULL, (TCHAR *)szIperfCmd.c_str(), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)){ //if (!CreateProcess(NULL, (TCHAR *)szIperfCmd.c_str(), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)){ return (""); } //立即关闭hWrite CloseHandle(hWrite); //读取命令行返回值 std::string strRet; char buff[1024] = { 0 }; DWORD dwRead = 0; while (ReadFile(hRead, buff, 1024, &dwRead, NULL)){ strRet.append(buff, dwRead); QString qstr1 = qstr1.fromStdString(buff); pwindow->appendLine(qstr1); } CloseHandle(hRead); gStrRet = strRet; QString qstr = qstr.fromStdString(gStrRet); pwindow->appendLine(qstr); pwindow->analyseRetStr(gStrRet); return strRet; }
交互命令行的,能输出和输入的
http://www.cppblog.com/aaxron/archive/2016/05/27/213597.html
BOOL CNeowayAndroidCheckToolView::PipeSendCmd(const HANDLE& handle,LPCTSTR cmd) { CString _cmd(cmd); BOOL bRet; DWORD dwWrited; _cmd.Append("\r\n"); return WriteFile(handle,_cmd,_cmd.GetLength(),&dwWrited,NULL); } BOOL CNeowayAndroidCheckToolView::PipeRead(const HANDLE& handle,CString& result,DWORD dwMilliseconds) { BOOL bRet = FALSE; CHAR buffer[4100]; DWORD dwReaded; result.Empty(); while(1) { Sleep(dwMilliseconds); bRet = PeekNamedPipe(handle,buffer,1,&dwReaded,NULL,NULL); if (!bRet) goto _flag_exit; if (dwReaded>0) { bRet = ReadFile(handle, buffer,4096, &dwReaded, NULL); if (!bRet) goto _flag_exit; buffer[dwReaded] = '\0'; result += buffer; }else { break; } } _flag_exit: return bRet; } BOOL CNeowayAndroidCheckToolView::TestGpio() { CString init_cmd = Path::Combine( Path::GetAppDirectory(),"adb.exe shell"); //CString init_cmd = "cmd.exe"; CString cmd; CString result; DWORD exit_code = -1; STARTUPINFO si; PROCESS_INFORMATION pi; HANDLE hStdOutRead=NULL, hStdOutPipe=NULL; HANDLE hStdInWrite=NULL, hStdInPipe=NULL; SECURITY_ATTRIBUTES saRead,saWrite; BOOL bRet; BOOL bSucceed; DWORD dwReaded; std::vector<CString> lines; saRead.nLength = sizeof(SECURITY_ATTRIBUTES); saRead.bInheritHandle = TRUE; saRead.lpSecurityDescriptor = NULL; saWrite = saRead; bRet = CreatePipe(&hStdOutRead, &hStdOutPipe, &saRead, 4096); if (!bRet) goto _flag_exit; bRet = CreatePipe(&hStdInPipe, &hStdInWrite, &saWrite, 4096); if (!bRet) goto _flag_exit; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; si.hStdOutput = hStdOutPipe; si.hStdInput = hStdInPipe; bRet = CreateProcess(NULL, (LPSTR)(LPCTSTR)init_cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); if (!bRet) goto _flag_exit; CloseHandle(hStdOutPipe);hStdOutPipe = NULL; CloseHandle(hStdInPipe);hStdInPipe = NULL; bRet = PipeRead(hStdOutRead,result,50); if (!bRet) goto _flag_exit; PipeSendCmd(hStdInWrite,"cd sys/gpio_test"); if (!bRet) goto _exit_adb; bRet = PipeRead(hStdOutRead,result,50); if (!bRet) goto _exit_adb; PipeSendCmd(hStdInWrite,"echo \"1\" > start"); if (!bRet) goto _exit_adb; bRet = PipeRead(hStdOutRead,result,50); if (!bRet) goto _exit_adb; PipeSendCmd(hStdInWrite,"cat result"); if (!bRet) goto _exit_adb; bRet = PipeRead(hStdOutRead,result,50); if (!bRet) goto _exit_adb; bRet = (result.Find("OK")>=0); _exit_adb: if (!PipeSendCmd(hStdInWrite,"exit")) { bRet = FALSE; goto _flag_exit; } WaitForSingleObject( pi.hProcess, 5000); if (!GetExitCodeProcess(pi.hProcess, &exit_code)) { bRet = FALSE; goto _flag_exit; } bRet = bRet && (exit_code==0); _flag_exit: if (hStdOutRead!=NULL) CloseHandle(hStdOutRead); if (hStdOutPipe!=NULL) CloseHandle(hStdOutPipe); if (hStdInWrite!=NULL) CloseHandle(hStdInWrite); if (hStdInPipe!=NULL) CloseHandle(hStdInPipe); return bRet; }