1、采用Windows指令
//設定操作指令
std::wstring operate = QString("runas").toStdWString();
//設定EXE路徑
std::wstring path = QCoreApplication::applicationDirPath().toStdWString();
//設定EXE
std::wstring fileName = QString(QCoreApplication::applicationDirPath() + "/app.exe").toStdWString();
//使用ShellExecute指令打開程式
int ret = (int)ShellExecute(NULL, operate.c_str(), fileName.c_str(), NULL, path.c_str(), SW_SHOWNORMAL);
2、采用QT啟動—在Windows下很多電腦的環境缺少庫、或者驅動導緻Windows ShellExecute指令失敗
(1)、此種方法QT給我們屏蔽了環境的差異,此方法能直接啟動app.exe程式,
QProcess::startDetached(QCoreApplication::applicationDirPath() + QDir::separator() + "app.exe", QStringList());
但是有些程式需要在Main函數傳遞參數應該怎麼辦呢?
第二個QStringList能完美的解決這個問題
QStringList list;
list.push_back("msg1");//這裡可以傳遞參數
list.push_back("command");
QProcess::startDetached(QCoreApplication::applicationDirPath() + QDir::separator() + "app.exe", list);
int main(int argc, char *argv[])
{
for (i = 0; i < argc; i++)
{
QString qsCommad = QString(QLatin1String(argv[i]));
if(qsCommad == "msg1")
{
//do something
}
else if(qsCommand == "command")
{
//do someting
}
}
}