Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SendMessage in Windows/Linux
Qt 6.11 is out! See what's new in the release blog

SendMessage in Windows/Linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 676 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by
    #1

    In qt-solutions project is implement SendMessage by sockets:

    bool QtLocalPeer::sendMessage(const QString &message, int timeout)
    {
        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(timeout/2);
            if (connOk || i)
                break;
            int ms = 250;
    #if defined(Q_OS_WIN)
            Sleep(DWORD(ms));
    #else
            struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
            nanosleep(&ts, NULL);
    #endif
        }
        if (!connOk)
            return false;
    
        QByteArray uMsg(message.toUtf8());
        QDataStream ds(&socket);
        ds.writeBytes(uMsg.constData(), uMsg.size());
        bool res = socket.waitForBytesWritten(timeout);
        if (res) {
            res &= socket.waitForReadyRead(timeout);   // wait for ack
            if (res)
                res &= (socket.read(qstrlen(ack)) == ack);
        }
        return res;
    }
    

    How implement standard messages like Windows:

    SendMessage(globalHWND, RegisterWindowMessage(IDENT), 0, 0)
    

    Linux has not usual messages and must be used QLocalSocket ?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved