Results 1 to 9 of 9

Thread: Creating a service, Windows 10, Qt 5.4.1,

  1. #1
    Join Date
    Mar 2014
    Location
    USA
    Posts
    70
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Creating a service, Windows 10, Qt 5.4.1,

    Qt used to have a way to create a service, but they deprecated it with Windows Vista. I did some searching around for a couple of days, worded my search right, and found a method to create a service step-by-step with code on the Microsoft site. There's 4 parts to it, the first step is to create a definitions dll. No problem. You do it command line without opening visual studio. You then create a c++ empty project. Then you add the Svc file, add the definitions library to the project, and you're done as far as creating and starting an empty service. There are other programs form configuration, etc. but as far as installing the service, they are not needed though you will need to run svcconfig delete SvcName to take the service back out. If you going to "play" with it, this is the address of the Microsoft site. https://docs.microsoft.com/en-us/win...service-sample. If I open Visual Studio, create an empty project, add the Svc.cpp file and the sample.dll, compile it, it works fine. So that a could add a Qt class to do the work, I made a Qt console application, copied in the Svc.cpp file and the sample.dll and removed the Qt main. I didn't add any other code to it or anything, it's the same project only with Qt's VisualStudio addin. When I run it to try to install the service, (Svc.exe install), I get the error code ERROR_ACCESS_DENIED when I try to open the ServiceManager "OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS )". I tried doing the empty project and then adding all the paths, etc and include (but not use) the class, it says I can't do it because I didn't use the Visual Studio add in. I really need this to run as a service so that SysAdmin can control access and so that it will run in the background even in screen-lock mode. I've tried to give enough information, I hope somebody can help me. I attached a copy of the 3 programs written from an empty project that work, the Svc.cpp code, and the sample.dll . The dll is just a set of error definitions, it doesn't really do anything. I didn't know qtcentre wouldn't allow you to upload dll or exe, but it makes sense. I attached all files to create the programs so that you can see what they look like without having to hunt around and without making this post a mile long. The only one I'm having problems with is the Svc.cpp program. The others don't have to be compiled in a Qt environment.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    307
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,


  3. #3
    Join Date
    Mar 2014
    Location
    USA
    Posts
    70
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    Yes. The part that actually make it as a service in the background doesn't work starting with Windows Vista

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,410
    Thanked 256 Times in 251 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    Are you sure ? I use this for Windows 7/8/10 and WS 2003/2012 without any problems.

  5. #5
    Join Date
    Mar 2014
    Location
    USA
    Posts
    70
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    When I tried it before, I was trying to make a GUI run as a thread. That's the part that was deprecated. Call this a learning lesson. I've already made a command line version, and on reading the documentation again, it says that you should separate the service and the GUI into two separate programs. I've already done this, now all I have to do is get the command line to run as a service. Here we go trying to figure out how to make a service using qt-solutions. I'll let you know how it goes.

  6. #6
    Join Date
    Mar 2014
    Location
    USA
    Posts
    70
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    I had a command line server program written, and I thought it would just be a matter of adding/changing a few lines of code to make it a QtService. Didn't work. I tried making a blank one from scratch in Visual Studio 2013. Started out I made a command line (QCoreApplication) project. Then I tried to create an empty service using add class. Named the class MyService (I had to call it something) made the base class QCoreApplication, left constructor signature blank. Once created, I put all the appropriate values in the constructor. I then added "qtservice.h" and added the code to make it a QtService<QCoreApplication>.
    Then I added all the virtual functions. I left them as empty functions. What it looks like when the compile fails is that QtService<QCoreApplication> is not inheriting QObject. A while back I made a gui service but you can't do that anymore so it got scrapped but I still have the code. On that one I used QtWidgets/QApplication to make an application and then changed the .h and .cpp files to match a QtService, included the .h file, and added the necessary functions (start, stop, etc.) and it compiled fine. I checked the project settings and they are all set to include qtservice. I also tried it with and without Q_OBJECT* at the beginning of the class. What am I doing wrong? added this after post. I'm getting error C2039 staticMetaObject is not a member of QtService<QCoreApplication>. When I looked it up online it said in several places that it's not inheriting QObject.
    Last edited by TonyInSoMD; 20th September 2018 at 15:04.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,410
    Thanked 256 Times in 251 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    We are not fairies. What does "compilation fails" mean?

  8. #8
    Join Date
    Mar 2014
    Location
    USA
    Posts
    70
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    It won't compile. I don't know what else to call it.

    It won't compile. I don't know what else to call it.


    Added after 5 minutes:



    Added after 40 minutes:


    Here's a copy of the code and errors

    Qt Code:
    1. //main
    2.  
    3. #include <QtCore/QCoreApplication>
    4. #include "ckeyboardtest.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. CKeyboardTest a(argc, argv);
    9.  
    10. return a.exec();
    11. }
    12.  
    13.  
    14.  
    15. //.h
    16.  
    17. #ifndef CKEYBOARDTEST_H
    18. #define CKEYBOARDTEST_H
    19.  
    20. #include <QCoreApplication>
    21. #include "qtservice.h"
    22.  
    23. class CKeyboardTest : public QtService<QCoreApplication>
    24. {
    25. /*Q_OBJECT*/
    26.  
    27. public:
    28. CKeyboardTest(int argc, char **argv);
    29. ~CKeyboardTest();
    30.  
    31. protected:
    32.  
    33. void start();
    34. void stop();
    35. void pause();
    36. void resume();
    37. void processCommand(int nCode);
    38.  
    39. private:
    40.  
    41. };
    42.  
    43. #endif // CKEYBOARDTEST_H
    44.  
    45.  
    46. //.cpp
    47. #include "ckeyboardtest.h"
    48.  
    49. CKeyboardTest::CKeyboardTest(int argc, char **argv)
    50. : QtService<QCoreApplication>(argc, argv, "TestService")
    51. {
    52.  
    53. }
    54.  
    55. CKeyboardTest::~CKeyboardTest()
    56. {
    57.  
    58. }
    59.  
    60. void CKeyboardTest::pause()
    61. {
    62.  
    63. }
    64.  
    65. void CKeyboardTest::processCommand(int nCode)
    66. {
    67.  
    68. }
    69.  
    70. void CKeyboardTest::resume()
    71. {
    72.  
    73. }
    74.  
    75. void CKeyboardTest::start()
    76. {
    77.  
    78.  
    79. }
    80.  
    81. void CKeyboardTest::stop()
    82. {
    83.  
    84. }
    85.  
    86. Error 1 error LNK2019: unresolved external symbol "public: __cdecl QtServiceBase::QtServiceBase(int,char * *,class QString const &)"
    87. (??0QtServiceBase@@QEAA@HPEAPEADAEBVQString@@@Z) referenced in function "public: __cdecl QtService<class QCoreApplication>::QtService<class QCoreApplication>(int,char * *,
    88. class QString const &)" (??0?$QtService@VQCoreApplication@@@@QEAA@HPEAPEADAEBVQString@@@Z)
    89. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\ckeyboardtest.obj KeyboardTest
    90.  
    91. Error 2 error LNK2019: unresolved external symbol "public: virtual __cdecl QtServiceBase::~QtServiceBase(void)" (??1QtServiceBase@@UEAA@XZ)
    92. referenced in function "public: virtual __cdecl QtService<class QCoreApplication>::~QtService<class QCoreApplication>(void)" (??1?$QtService@VQCoreApplication@@@@UEAA@XZ)
    93. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\ckeyboardtest.obj KeyboardTest
    94.  
    95. Error 3 error LNK2001: unresolved external symbol "protected: virtual void __cdecl QtServiceBase::stop(void)" (?stop@QtServiceBase@@MEAAXXZ)
    96. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\ckeyboardtest.obj KeyboardTest
    97.  
    98. Error 4 error LNK2001: unresolved external symbol "protected: virtual void __cdecl QtServiceBase::pause(void)" (?pause@QtServiceBase@@MEAAXXZ)
    99. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\ckeyboardtest.obj KeyboardTest
    100.  
    101. Error 5 error LNK2001: unresolved external symbol "protected: virtual void __cdecl QtServiceBase::resume(void)" (?resume@QtServiceBase@@MEAAXXZ)
    102. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\ckeyboardtest.obj KeyboardTest
    103.  
    104. Error 6 error LNK2001: unresolved external symbol "protected: virtual void __cdecl QtServiceBase::processCommand(int)" (?processCommand@QtServiceBase@@MEAAXH@Z)
    105. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\ckeyboardtest.obj KeyboardTest
    106.  
    107. Error 7 error LNK2019: unresolved external symbol "public: int __cdecl QtServiceBase::exec(void)" (?exec@QtServiceBase@@QEAAHXZ) referenced in function main
    108. C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\KeyboardTest\main.obj KeyboardTest
    109.  
    110. Error 8 error LNK1120: 7 unresolved externals C:\ASAM\TFS\Asam\Adams\Applications\KeyboardTest\x64\Debug\\KeyboardTest.exe 1 1 KeyboardTest
    To copy to clipboard, switch view to plain text mode 

    Hope this helps
    Last edited by TonyInSoMD; 20th September 2018 at 15:58.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,765
    Thanks
    216
    Thanked 582 Times in 572 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Creating a service, Windows 10, Qt 5.4.1,

    Your compilation is not failing. It gets to the link stage, and that is what is failing. The linker can't find your implementations of the functions you have declared in your class definition. Possibly you haven't added CKeyboardTest.cpp to your project file.

    If QtService<> inherits from QObject somewhere along the line, you *must* add the Q_OBJECT macro to the class definition otherwise MOC will not run on the .cpp file and you will get QMetaObject-related link errors. If it does not inherit from QObject, then you will get errors if you *do* include the macro.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How do I create a Windows Service in Qt?
    By ayanda83 in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2017, 12:16
  2. How to install a service in windows with automatic startup?
    By josecarlosmissias in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2009, 17:01
  3. Start Qt application as Windows Service
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 10th May 2008, 17:23
  4. Creating QDS service
    By bowser in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th October 2007, 11:12
  5. Windows Service
    By TheGrimace in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2007, 19:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.