C++启动另一个程序

时间:2022-06-17 19:45:49

打开另一个程序,
QT中比较简单:

QProcess *process = new QProcess;
QStringList str;
str << "";
//process->start("C:\\Program Files (x86)\\MyAPP\\app.exe", str);
process->start(m_installPath + "\\" + RUN_EXE, str);

不用QT的库,单纯用C++ Windows API启动另一个程序:

#include <shlobj.h>
#include <windows.h>
#include <shellapi.h>

bool execAPP()
{
    SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
    SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, _pgmptr, NULL);

    char szFilePath[MAX_PATH];
    sprintf(szFilePath, "\"%s\"", _pgmptr);

    HINSTANCE hInstance = ShellExecute(NULL,
        TEXT("open"),
        TEXT("C:\\Program Files (x86)\\MyAPP\\app.exe"),
        (LPCWSTR)szFilePath,
        NULL,
        SW_HIDE);

    if ((int)hInstance > 32)
    {
        Log(TEXT("execute exe success!"));
        return true;
    }
    else
    {
        Log(TEXT("execute exe fail!"));
        return false;
    }
}