繁体   English   中英

QT将信号发送到另一个QT应用程序?

[英]QT send signal to another QT application?

我有三个应用程序:

  • 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

由于您指定要在Windows下运行,因此我认为最简单的方法是使用Windows消息传递系统。 对此的说明: http : //msdn.microsoft.com/zh-cn/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 ))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM