InnoSetup / Windows Restart Manager / Native Event Filter problem.
-
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.
-
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
-
Good question, can you show the filter code ?
-
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));
#endifMyEventFilter * 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();
}
@ -
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));
#endifMyEventFilter * 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();
}
@ -
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. -
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.@Sikander-Rafiq hi,
Which version of Windows ?
Which exact version of Qt ?
What are you doing in your code ? -
@SGaist
I have created another thread mentioning QT versions and all code stuff. You can find this thread herehttps://forum.qt.io/topic/162123/unable-to-launch-qt-desktop-app-from-qt-service-using-qprocess
-
@SGaist
I have created another thread mentioning QT versions and all code stuff. You can find this thread herehttps://forum.qt.io/topic/162123/unable-to-launch-qt-desktop-app-from-qt-service-using-qprocess
@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 herehttps://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..