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. InnoSetup / Windows Restart Manager / Native Event Filter problem.
Forum Update on Monday, May 27th 2025

InnoSetup / Windows Restart Manager / Native Event Filter problem.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 4.8k 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.
  • C Offline
    C Offline
    cs96and
    wrote on last edited by
    #1

    I am using InnoSetup as the installer for my QT application. InnoSetup tries to close my application if it is running when to try to install a new version. According to the InnoSetup docs, this is done using the windows RestartManager service.

    My application is a small application that sits in the system tray. When InnoSetup tries to terminate the application, the system tray icon disappears (at least it does when I move my mouse over it), but the process is still running in Task Manager and never terminates.

    My app starts with just the system tray icon showing. If you double click it it opens a log window, or right clicking it shows a menu. If you do either of these things before running the installer, the application exits fine. If the app has just been run up and not interacted with, then it does not exit.

    I tried installing a native event filter at the application level to capture WM_QUERYENDSESSION and WM_ENDSESSION (with lParam ENDSESSION_CLOSEAPP). Again, if the app has been interacted with, my event filter code executes. If there has been no interaction with the app, the event filter code never gets run.

    If you want to take a look at my application code it is available at https://bitbucket.org/cs96and/rockscrobbler

    EDIT: sorry I forgot to mention, I am using QT 5.1.1 compiled by me for VS2008 (32-bit).

    Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      WM_QUERYENDSESSION and WM_ENDSESSION are now again handled starting Qt 5.2, so you should get the proper functionality with this version

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cs96and
        wrote on last edited by
        #3

        That's great news thanks.

        However, I am still interested to know why my native event filter does not get fired unless I have interacted with the application?

        Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Good question, can you show the filter code ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cs96and
            wrote on last edited by
            #5

            Here's my modified main.cpp that installs an event filter.

            @/*
            Copyright 2011-2013 Alan Davies.

            This file is part of Rock Scrobbler.

            Rock Scrobbler is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
            
            Rock Scrobbler is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
            
            You should have received a copy of the GNU General Public License
            along with Rock Scrobbler.  If not, see <http://www.gnu.org/licenses/>.
            

            In addition, as a special exception, the copyright holders give
            permission to link the code of portions of this program with the
            OpenSSL library under certain conditions as described in each
            individual source file, and distribute linked combinations
            including the two.

            You must obey the GNU General Public License in all respects
            for all of the code used other than OpenSSL. If you modify
            file(s) with this exception, you may extend this exception to your
            version of the file(s), but you are not obligated to do so. If you
            do not wish to do so, delete this exception statement from your
            version. If you delete this exception statement from all source
            files in the program, then also delete it here.
            */

            #include <QAbstractNativeEventFilter>
            #include <QFileInfo>
            #include <QSharedMemory>
            #include <QtWidgets/QApplication>

            #include <QtPlugin>

            #include "LogWindow.h"

            #include <windows.h>

            #if (defined QT_STATICPLUGIN)
            Q_IMPORT_PLUGIN(qico)
            #endif

            #define SHARED_MEMORY_UUID "928fe311-dbb1-43e3-b659-8fa44ac50fcf"

            class MyEventFilter : public QAbstractNativeEventFilter
            {
            private:
            virtual bool nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pResult);
            };

            bool MyEventFilter::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pResult)
            {
            const MSG &msg = *static_cast<MSG *>(pMessage);

            switch (msg.message)
            {
            case WM_QUERYENDSESSION:
            case WM_ENDSESSION:
            if (msg.lParam == ENDSESSION_CLOSEAPP)
            {
            qApp->quit();
            *pResult = 1;
            return true;
            }
            break;

            default:
            break;
            }

            return false;
            }

            int main(int argc, char *argv[])
            {
            // Only allow one instance by creating a named shared memory segment.
            // If the create() call fails because the segment exists, then exit.
            QSharedMemory sharedMem(SHARED_MEMORY_UUID);
            if (!sharedMem.create(1) && (sharedMem.error() == QSharedMemory::AlreadyExists))
            exit(2);

            #if (!defined QT_DEBUG)
            // Get path of current executable
            const QString executablePath = QFileInfo(LogWindow::getCurrentExecutablePath()).path();

            // Set plugin path to only be the path of the running executable
            // This means that an installed version won't pick up plugins from the QT SDK
            QApplication::setLibraryPaths(QStringList(executablePath));
            #endif

            MyEventFilter * const pMyEventFilter = new MyEventFilter;

            QApplication a(argc, argv);
            a.setQuitOnLastWindowClosed(false);
            a.installNativeEventFilter(pMyEventFilter);

            LogWindow w;

            // Don't call w.show() - start hidden.

            return a.exec();
            }
            @

            R 1 Reply Last reply
            0
            • C cs96and

              Here's my modified main.cpp that installs an event filter.

              @/*
              Copyright 2011-2013 Alan Davies.

              This file is part of Rock Scrobbler.

              Rock Scrobbler is free software: you can redistribute it and/or modify
              it under the terms of the GNU General Public License as published by
              the Free Software Foundation, either version 3 of the License, or
              (at your option) any later version.
              
              Rock Scrobbler is distributed in the hope that it will be useful,
              but WITHOUT ANY WARRANTY; without even the implied warranty of
              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
              GNU General Public License for more details.
              
              You should have received a copy of the GNU General Public License
              along with Rock Scrobbler.  If not, see <http://www.gnu.org/licenses/>.
              

              In addition, as a special exception, the copyright holders give
              permission to link the code of portions of this program with the
              OpenSSL library under certain conditions as described in each
              individual source file, and distribute linked combinations
              including the two.

              You must obey the GNU General Public License in all respects
              for all of the code used other than OpenSSL. If you modify
              file(s) with this exception, you may extend this exception to your
              version of the file(s), but you are not obligated to do so. If you
              do not wish to do so, delete this exception statement from your
              version. If you delete this exception statement from all source
              files in the program, then also delete it here.
              */

              #include <QAbstractNativeEventFilter>
              #include <QFileInfo>
              #include <QSharedMemory>
              #include <QtWidgets/QApplication>

              #include <QtPlugin>

              #include "LogWindow.h"

              #include <windows.h>

              #if (defined QT_STATICPLUGIN)
              Q_IMPORT_PLUGIN(qico)
              #endif

              #define SHARED_MEMORY_UUID "928fe311-dbb1-43e3-b659-8fa44ac50fcf"

              class MyEventFilter : public QAbstractNativeEventFilter
              {
              private:
              virtual bool nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pResult);
              };

              bool MyEventFilter::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pResult)
              {
              const MSG &msg = *static_cast<MSG *>(pMessage);

              switch (msg.message)
              {
              case WM_QUERYENDSESSION:
              case WM_ENDSESSION:
              if (msg.lParam == ENDSESSION_CLOSEAPP)
              {
              qApp->quit();
              *pResult = 1;
              return true;
              }
              break;

              default:
              break;
              }

              return false;
              }

              int main(int argc, char *argv[])
              {
              // Only allow one instance by creating a named shared memory segment.
              // If the create() call fails because the segment exists, then exit.
              QSharedMemory sharedMem(SHARED_MEMORY_UUID);
              if (!sharedMem.create(1) && (sharedMem.error() == QSharedMemory::AlreadyExists))
              exit(2);

              #if (!defined QT_DEBUG)
              // Get path of current executable
              const QString executablePath = QFileInfo(LogWindow::getCurrentExecutablePath()).path();

              // Set plugin path to only be the path of the running executable
              // This means that an installed version won't pick up plugins from the QT SDK
              QApplication::setLibraryPaths(QStringList(executablePath));
              #endif

              MyEventFilter * const pMyEventFilter = new MyEventFilter;

              QApplication a(argc, argv);
              a.setQuitOnLastWindowClosed(false);
              a.installNativeEventFilter(pMyEventFilter);

              LogWindow w;

              // Don't call w.show() - start hidden.

              return a.exec();
              }
              @

              R Offline
              R Offline
              Rohinee
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sikander Rafiq
                wrote last edited by
                #7

                HI All,
                Hi @SGaist
                I also want to capture shutdown event (i.e. WM_QUERYENDSESSION) and I am using QT 6.8, but unable to get it. Any hint/reason why it is not capturing this event. Thanks in advance.

                SGaistS 1 Reply Last reply
                0
                • S Sikander Rafiq

                  HI All,
                  Hi @SGaist
                  I also want to capture shutdown event (i.e. WM_QUERYENDSESSION) and I am using QT 6.8, but unable to get it. Any hint/reason why it is not capturing this event. Thanks in advance.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote last edited by
                  #8

                  @Sikander-Rafiq hi,

                  Which version of Windows ?
                  Which exact version of Qt ?
                  What are you doing in your code ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sikander Rafiq
                    wrote last edited by
                    #9

                    @SGaist
                    I have created another thread mentioning QT versions and all code stuff. You can find this thread here

                    https://forum.qt.io/topic/162123/unable-to-launch-qt-desktop-app-from-qt-service-using-qprocess

                    A 1 Reply Last reply
                    0
                    • S Sikander Rafiq

                      @SGaist
                      I have created another thread mentioning QT versions and all code stuff. You can find this thread here

                      https://forum.qt.io/topic/162123/unable-to-launch-qt-desktop-app-from-qt-service-using-qprocess

                      A Offline
                      A Offline
                      AlbertoJ2
                      wrote last edited by
                      #10

                      @Sikander-Rafiq said in InnoSetup / Windows Restart Manager / Native Event Filter problem.:

                      @SGaist
                      I have created another thread mentioning QT versions and all code stuff. You can find this thread here

                      https://forum.qt.io/topic/162123/unable-to-launch-qt-desktop-app-from-qt-service-tubidy-using-qprocess

                      Thanks for sharing the link. I happen to have the same issues, I’ll check it out, launching a Qt desktop app from a service using QProcess sounds tricky, especially with display/session permissions. Curious to see what you’ve tried so far..

                      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