diff --git a/src/qtsingleapp/qtlocalpeer.cpp b/src/qtsingleapp/qtlocalpeer.cpp index 72f8cd6db..fcf2d4a83 100644 --- a/src/qtsingleapp/qtlocalpeer.cpp +++ b/src/qtsingleapp/qtlocalpeer.cpp @@ -201,8 +201,62 @@ void QtLocalPeer::receiveConnection() return; } QString message(QString::fromUtf8(uMsg)); +#ifdef Q_OS_WIN + if (message == "qbt://pid") + socket->write(QByteArray::number(qApp->applicationPid())); + else + socket->write(ack, qstrlen(ack)); +#else socket->write(ack, qstrlen(ack)); +#endif socket->waitForBytesWritten(1000); delete socket; +#ifdef Q_OS_WIN + if (message == "qbt://pid") + return; +#endif emit messageReceived(message); //### (might take a long time to return) } + +#ifdef Q_OS_WIN +ulong QtLocalPeer::getRunningPid() { + if (!isClient()) + return false; + + QLocalSocket socket; + bool connOk = false; + for (int i = 0; i < 2; i++) { + // Try twice, in case the other instance is just starting up + socket.connectToServer(socketName); + connOk = socket.waitForConnected(5000/2); + if (connOk || i) + break; + Sleep(DWORD(250)); + } + if (!connOk) + return false; + QByteArray uMsg("qbt://pid"); + QDataStream ds(&socket); + ds.writeBytes(uMsg.constData(), uMsg.size()); + bool res = socket.waitForBytesWritten(5000); + res &= socket.waitForReadyRead(5000); + if (!res) + return -1; + while (socket.bytesAvailable() < (int)sizeof(quint32)) + socket.waitForReadyRead(); + uMsg.clear(); + quint32 remaining = socket.bytesAvailable(); + uMsg.resize(remaining); + int got = 0; + char* uMsgBuf = uMsg.data(); + do { + got = ds.readRawData(uMsgBuf, remaining); + remaining -= got; + uMsgBuf += got; + } while (remaining && got >= 0 && socket.waitForReadyRead(2000)); + if (got < 0) + return -1; + + return uMsg.toULong(); +} +#endif diff --git a/src/qtsingleapp/qtlocalpeer.h b/src/qtsingleapp/qtlocalpeer.h index b81dbd11a..3a6d46959 100644 --- a/src/qtsingleapp/qtlocalpeer.h +++ b/src/qtsingleapp/qtlocalpeer.h @@ -64,6 +64,9 @@ public: bool sendMessage(const QString &message, int timeout); QString applicationId() const { return id; } +#ifdef Q_OS_WIN + ulong getRunningPid(); +#endif Q_SIGNALS: void messageReceived(const QString &message); diff --git a/src/qtsingleapp/qtsingleapplication.cpp b/src/qtsingleapp/qtsingleapplication.cpp index a3a1fd7fd..fa0b0b6a6 100644 --- a/src/qtsingleapp/qtsingleapplication.cpp +++ b/src/qtsingleapp/qtsingleapplication.cpp @@ -349,3 +349,9 @@ void QtSingleApplication::activateWindow() \obsolete */ + +#ifdef Q_OS_WIN +ulong QtSingleApplication::getRunningPid() { + return peer->getRunningPid(); +} +#endif diff --git a/src/qtsingleapp/qtsingleapplication.h b/src/qtsingleapp/qtsingleapplication.h index 5df916561..e45e373c9 100644 --- a/src/qtsingleapp/qtsingleapplication.h +++ b/src/qtsingleapp/qtsingleapplication.h @@ -88,6 +88,9 @@ public: // Obsolete: void initialize(bool dummy = true) { isRunning(); Q_UNUSED(dummy) } +#ifdef Q_OS_WIN + ulong getRunningPid(); +#endif public Q_SLOTS: bool sendMessage(const QString &message, int timeout = 5000);