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. <Solved>Cannot create System Wide Mutex within Qt application
Forum Updated to NodeBB v4.3 + New Features

<Solved>Cannot create System Wide Mutex within Qt application

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    starbearer
    wrote on last edited by
    #1

    I need to create a System Wide Mutex in Qt.

    However, Qt, by itself, has QMutex, which is a thread level mutex. There's also something called QSystemMutex, but which is an extended solution and not properly documented, so I didn't want to risk it.

    I then created Windows Mutex...but it seems they're not working system wide. It doesn't stop another instance of the application from starting up.

    Where am I going wrong with this?

    @
    HANDLE ghMutex;
    if ( version == "GSA")
    {
    num_version = 1;
    // G1->GSAParseFile();
    QString temp = "GSAMutex";
    //QByteArray text = QByteArray::fromHex(ghMutex);
    LPCTSTR GSAMutex = (LPCTSTR)temp.data();
    ghMutex = CreateMutex(NULL, FALSE, GSAMutex);
    if (ghMutex == NULL)
    {
    Logger::WriteLog("Multiple instances of type GSA - Sent message, shutting down." );

    QApplication::exit();
    

    }
    else
    {
    sharedMem = new QSharedMemory("GSASharedMemory");
    QString temp1 = "creating mutex with:";
    temp1.append(temp);
    Logger::WriteLog(temp);
    }
    @

    This code doesn't work as intended. Multiple mutex of the same name are apparently created....
    Is the check condition,
    @
    if (ghMutex== NULL)
    @

    sufficient?
    This was how it was noted in MSDN examples.

    Kindly advise.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vidar
      wrote on last edited by
      #2

      Just do CreateMutex(NULL, FALSE, NULL); You don't need to pass a name (optional parameter).

      However, you could also try to use QSystemSemaphore, which is documented quite well (not QSystemMutex).

      1 Reply Last reply
      0
      • S Offline
        S Offline
        starbearer
        wrote on last edited by
        #3

        Got the solution.

        If another one tried createMutex(), it would not return NULL, rather it would open the Mutex.

        One needs to get the Last Error Code and use that as a checking condition.

        @
        LPCTSTR GSAMutex = (LPCTSTR)temp.data();
        ghMutex = CreateMutex(NULL, FALSE, GSAMutex);
        m_dwLastError = GetLastError();

        if (ERROR_ALREADY_EXISTS == m_dwLastError)
        {
        Logger::WriteLog("Multiple instances of type GSA - Sent message, shutting down." );

        a->exit();
        

        }
        @

        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