赞
踩
一些控制台程序在Windows会有黑窗口弹出,有的时候不想在前台看到,想把它后台运行就行。这时候利用Windows的相关app函数实现。
实现方式如下:
PROCESS_INFORMATION pi; STARTUPINFO si; //隐藏进程窗口 si.cb = sizeof(STARTUPINFO); si.lpReserved = NULL; si.lpDesktop = NULL; si.lpTitle = NULL; si.dwFlags = SW_SHOWDEFAULT ; si.wShowWindow = SW_SHOW; si.cbReserved2 = NULL; si.lpReserved2 = NULL; QString namestr = subelement.attribute("path"); QString argstr = subelement.attribute("arg"); QString name_ = apppath + "/" + namestr; bool ishind = true; if (ishind) ///flag为真为隐藏启动 { si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; } BOOL ret = FALSE; if (!argstr.isEmpty()) { name_ += " " + argstr; } if (!currentdir.isEmpty()) { ret = CreateProcess(NULL,LPWSTR(name_.toStdWString().c_str()),NULL,NULL,NULL,CREATE_NEW_CONSOLE,NULL,LPWSTR(currentdir.toStdWString().c_str()),&si,&pi); } else { ret = CreateProcess(NULL,LPWSTR(name_.toStdWString().c_str()),NULL,NULL,NULL,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi); } if (ret == 0) { int errorcode = GetLastError(); continue ; } if (ret != 0) { qDebug() << "start : " << name_; reStr += QString("<program name='%1' statue='1'/>\n").arg(namestr); }else { reStr += QString("<program name='%1' statue='-1'/>\n").arg(namestr); qDebug() << "start erro: " << name_; }
/将此代码复制到main函数所在cpp任何处就ok了/
#pragma comment( linker, “/subsystem:“windows” /entry:“mainCRTStartup”” )//隐藏DOS黑窗口
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。