Qt 获取cmd运行结果
http://www.cnblogs.com/gisbeginner/archive/2012/12/08/2809063.html
BOOL ExecDosCmd()
{
#define EXECDOSCMD "ping http://www.baidu.com" //可以换成你的命令
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]; //长达1K的命令行,够用了吧
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;
//关键步骤,CreateProcess函数参数意义请查阅MSDN
QString str(command);
if (!CreateProcess(NULL, (LPWSTR)str.utf16(),NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
CloseHandle(hWrite);
CloseHandle(hRead);
return FALSE;
}
CloseHandle(hWrite);
char buffer[4096] = {0}; //用4K的空间来存储输出的内容,只要不是显示文件内容,一般情况下是够用了。
DWORD bytesRead;
while (true)
{
if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
break;
//buffer中就是执行的结果,可以保存到文本,也可以直接输出
QString strTemp(buffer);
QMessageBox testMassage1;
testMassage1.setText(strTemp);
testMassage1.exec();
//这里是弹出对话框显示
}
CloseHandle(hRead);
return TRUE;
}
另一个方法:
QProcess p(0);
p.start("cmd", QStringList()<<"/c"<<"ping 192.168.101.3");
p.waitForStarted();
p.waitForFinished();
QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());
QMessageBox testMassage1;
testMassage1.setText(strTemp);
testMassage1.exec();
return true;