前幾節裏,分享了進程通訊的幾種方式:Windows消息機制、Shared Memory(共享內存),本節講解下關於進程通訊的另一種方式-QProcess。json
進程A-帶參啓動進程B函數
void onSendMessage() { QString strExe(""); if (m_pProcess == NULL) m_pProcess = new QProcess(this); #if defined(QT_DEBUG) strExe = "ReceiveMessaged.exe"; # else strExe = "ReceiveMessage.exe"; # endif // 判斷進程是否存在 QProcess tasklist; tasklist.start("tasklist", QStringList() << "/NH" << "/FO" << "CSV" << "/FI" << QString("IMAGENAME eq %1").arg(strExe)); tasklist.waitForFinished(); QString strOutput = tasklist.readAllStandardOutput(); if (!strOutput.startsWith(QString("\"%1").arg(strExe))) { QJsonObject json; json.insert("UserName", QStringLiteral("╰☆一去、二三裏`")); json.insert("Password", "123456"); QJsonDocument document; document.setObject(json); QByteArray byteArray = document.toJson(QJsonDocument::Compact); QStringList arguments; arguments << byteArray; m_pProcess->startDetached(strExe, arguments); } }
進程B-命令行讀取ui
QStringList cmdLineArgs = QCoreApplication::arguments(); QMessageBox::information(NULL, QStringLiteral("ReceiveMessage"), cmdLineArgs.join(" "));
原文做者:一去丶二三裏
做者博客:去做者博客空間