我有三个应用程序:
- ApplicationLauncher.exe
- Updater.exe
- MyApplication.exe
我想使用 ApplicationLauncher.exe
来启动 Updater.exe
并且当 Updater.exe
完成更新时它应该发送向 ApplicationLauncher.exe
发出信号,然后应启动 MyApplication.exe
这是因为 Updater.exe
需要管理员权限才能更新,所以我想在更新程序工作时保持 ApplicationLauncher.exe
运行,然后使用 ApplicationLauncher.exe
启动 MyApplication.exe
为了让它工作,我需要一种方法让 ApplicationLauncher.exe
知道 Updater.exe
何时完成。
有什么想法可以实现吗?
最佳答案
您只需要生成一个进程,并等待它完成。没有数据通信,所以没有真正的 IPC。
因为更新程序需要提升权限,所以不能使用 QProcess。我们在 Windows 上,您需要回退到 win32 API。
QDir exePath(QCoreApplication::instance()->applicationDirPath());
QString exeFileName = exePath.absoluteFilePath("update.exe");
QByteArray exeFileNameAnsi = exeFileName.toAscii();
// we must use ShellExecuteEx because we need admin privileges on update
// the standard QProcess functions do not provide this.
SHELLEXECUTEINFOA lpExecInfo;
lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.lpFile = exeFileNameAnsi.constData();
lpExecInfo.fMask=SEE_MASK_DOENVSUBST|SEE_MASK_NOCLOSEPROCESS;
lpExecInfo.hwnd = NULL;
lpExecInfo.lpVerb = "open";
lpExecInfo.lpParameters = NULL;
lpExecInfo.lpDirectory = NULL;
lpExecInfo.nShow = SW_SHOW ;
lpExecInfo.hInstApp = (HINSTANCE)SE_ERR_DDEFAIL;
if( ShellExecuteExA(&lpExecInfo) && lpExecInfo.hProcess != NULL) {
DWORD retval;
DWORD waitresult;
do {
waitresult = WaitForSingleObject(lpExecInfo.hProcess, 10);
} while (waitresult == WAIT_TIMEOUT);
GetExitCodeProcess(lpExecInfo.hProcess, &retval);
CloseHandle(lpExecInfo.hProcess);
if(retval == 0) {
// launched and finished without errors
}
} else {
// failed to launch
}
我认为您可能还需要链接到 PRO 文件中的 shell32.lib,不确定。
LIBS += -lshell32
关于c++ - QT 向另一个 QT 应用程序发送信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23360292/