Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to create a Windows Service using C++/QT?



  • Hi,

    I have a GUI application which is based on QT/C++. It can be any simple QT/C++ application. I want to run this application as a window service. Actually my requirement is that whenever the machine turns to restart, then my GUI application should run automatically. There should not wait for system login to run it. It should ready and doing its job before the system login. Whenever we login the system it must appear on the screen (or) appear in the system trey. If it appears in the system tray, then double clicked on it, then it must appear on the screen.

    Kindly suggest me what should I do to achieve the same. If possible please share me the sample code for the same.

    Thanks...


  • Lifetime Qt Champion

    @amitgolhani What you describe doesn't sound like a service. It is simply autostart of your app. There is nothing special about it.
    On Windows 10 you can simply create a link to your executable in USE_NAME/AppData/Roaming/Microsoft/Windows/Start/Programs/Autostart. Installer can do this.



  • @jsulm , @amitgolhani

    I have a GUI application which is based on QT/C++.
    ...
    Actually my requirement is that whenever the machine turns to restart, then my GUI application should run automatically. There should not wait for system login to run it.

    Can you run a Windows "GUI" application from machine start, with no login, and therefore no desktop? I would doubt it...


  • Lifetime Qt Champion

    @JonB You're right I was confused by "Whenever we login the system it must appear on the screen". Yes, a service does not have an UI.
    @amitgolhani I think you will need to split your app into two parts:

    1. Background service without any UI
    2. UI front-end


  • @jsulm
    I don't know, I just threw it in there. I don't think your original, simpler suggestion is "wrong", rather I think the OP needs to clarify just what he wants with this in mind. It would be a lot easier if he could skip the "before login" and just do what you suggested.

    Yes, a service does not have an UI.

    If he does need to use a Windows Service, when a Service is configured, Properties > Log On. Assuming this service can be run under default Local System account, there is an Allow service to interact with desktop checkbox. I use this in a (non-Qt) application which is principally a Service (does scheduling jobs), but also allows the user to invoke it (manually) with a command-line option requesting a UI for configuring the job schedules. OP should investigate whether that does allow the kind of (delayed) interaction he has in mind? It may do, I can't recall.

    My "fear" would then be: Suppose the way it works is that a Service can do UI stuff, but it will block on the first UI call till there is a login/desktop. Now if his application goes something like QtWidgets.QApplication(sys.argv) "early", I don't know if that Qt call would try to start from a UI and therefore block at that point? Stuff for him to investigate :)


Log in to reply