如何让QT应用程序向另一个QT应用程序发送信号?

3
我有三个应用程序:
- ApplicationLauncher.exe - Updater.exe - MyApplication.exe 我想使用 ApplicationLauncher.exe 来启动 Updater.exe,当 Updater.exe 完成更新时,它应该向 ApplicationLauncher.exe 发送一个信号,然后 ApplicationLauncher.exe 再启动 MyApplication.exe。
这是因为 Updater.exe 需要管理员权限来更新,所以我希望在更新程序运行时保持 ApplicationLauncher.exe 运行,并使用 ApplicationLauncher.exe 来启动 MyApplication.exe。
为了使这个过程正常工作,我需要让 ApplicationLauncher.exe 知道 Updater.exe 何时完成。
有什么好的方法可以实现这个功能吗?
2个回答

3

您只需要启动一个进程,并等待其完成。由于没有数据通信,因此实际上没有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

一个开始并且看到它何时结束的示例会是什么样子?我不确定我完全明白。 - undefined
QProcess似乎无法启动Updater.exe,因为它需要管理员权限。 - undefined
将这个添加到 Updater.exe 的 .pro 文件中,但仍然无法打开。 - undefined
这个完全按照我想要的方式运行!感谢你帮我节省了很多时间!! - undefined
嗯...真的很难说出问题在哪里。检查一下是否真的执行了退出操作(调试断点),并且返回到了Qt事件循环中。 - undefined
显示剩余5条评论

1

由于您指定正在运行Windows,我认为最简单的方法是使用Windows消息系统。这些的描述:http://msdn.microsoft.com/en-us/library/windows/desktop/ms632590%28v=vs.85%29.aspx

现在,在Qt应用程序中,您需要使用QWidget的winEvent:http://qt-project.org/doc/qt-4.8/qwidget.html#winEvent

页面http://doc.qt.digia.com/solutions/4/qtwinmigrate/winmigrate-win32-in-qt-example.html有一个很好的示例,说明如何制作一个Windows感知的Qt应用程序。

然而,如果你只想检查一个应用程序是否已经完成,那么只需通过runas命令(http://qt-project.org/forums/viewthread/19060)以管理员身份启动“Updater.exe”,然后等待它完成(QProcess :: waitForFinished(http://qt-project.org/doc/qt-4.8/qprocess.html#waitForFinished))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接