Results 1 to 2 of 2

Thread: NT Service with networking support

  1. #1
    Join Date
    Oct 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default NT Service with networking support

    Hi everyone!

    I need NT Service(ofcource without GUI) but with some networking support(UDP server).
    Networking depends on QCoreApplication, but i have no idea how to implement service with QCoreApplication.

    My reference service code is here. It works, but how to embed QCoreApplication in it?
    May be there is some other way to implement NT service application?
    Using Qt5.3.2 VS2013
    Qt Code:
    1. // Beeper Service.cpp
    2. // Copyright (c) 2001-2002 Jarmo Muukka. All rights reserved.
    3. #include <windows.h>
    4. #include <tchar.h>
    5.  
    6. TCHAR* serviceName = TEXT("Beeper2 Service");
    7. SERVICE_STATUS serviceStatus;
    8. SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
    9. HANDLE stopServiceEvent = 0;
    10.  
    11. void WINAPI ServiceControlHandler(DWORD controlCode)
    12. {
    13. switch (controlCode)
    14. {
    15. case SERVICE_CONTROL_INTERROGATE:
    16. break;
    17.  
    18. case SERVICE_CONTROL_SHUTDOWN:
    19. case SERVICE_CONTROL_STOP:
    20. serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    21. SetServiceStatus(serviceStatusHandle, &serviceStatus);
    22.  
    23. SetEvent(stopServiceEvent);
    24. return;
    25.  
    26. case SERVICE_CONTROL_PAUSE:
    27. break;
    28.  
    29. case SERVICE_CONTROL_CONTINUE:
    30. break;
    31.  
    32. default:
    33. if (controlCode >= 128 && controlCode <= 255)
    34. // user defined control code
    35. break;
    36. else
    37. // unrecognised control code
    38. break;
    39. }
    40.  
    41. SetServiceStatus(serviceStatusHandle, &serviceStatus);
    42. }
    43.  
    44. void WINAPI ServiceMain(DWORD /*argc*/, TCHAR* /*argv*/[])
    45. {
    46. // initialise service status
    47. serviceStatus.dwServiceType = SERVICE_WIN32;
    48. serviceStatus.dwCurrentState = SERVICE_STOPPED;
    49. serviceStatus.dwControlsAccepted = 0;
    50. serviceStatus.dwWin32ExitCode = NO_ERROR;
    51. serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
    52. serviceStatus.dwCheckPoint = 0;
    53. serviceStatus.dwWaitHint = 0;
    54.  
    55. serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);
    56.  
    57. if (serviceStatusHandle)
    58. {
    59. // service is starting
    60. serviceStatus.dwCurrentState = SERVICE_START_PENDING;
    61. SetServiceStatus(serviceStatusHandle, &serviceStatus);
    62.  
    63. // do initialisation here
    64. stopServiceEvent = CreateEvent(0, FALSE, FALSE, 0);
    65.  
    66. // running
    67. serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
    68. serviceStatus.dwCurrentState = SERVICE_RUNNING;
    69. SetServiceStatus(serviceStatusHandle, &serviceStatus);
    70.  
    71. do
    72. {
    73. Beep(1000, 100);
    74. } while (WaitForSingleObject(stopServiceEvent, 5000) == WAIT_TIMEOUT);
    75.  
    76. // service was stopped
    77. serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    78. SetServiceStatus(serviceStatusHandle, &serviceStatus);
    79.  
    80. // do cleanup here
    81. CloseHandle(stopServiceEvent);
    82. stopServiceEvent = 0;
    83.  
    84. // service is now stopped
    85. serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
    86. serviceStatus.dwCurrentState = SERVICE_STOPPED;
    87. SetServiceStatus(serviceStatusHandle, &serviceStatus);
    88. }
    89. }
    90.  
    91. void RunService()
    92. {
    93. SERVICE_TABLE_ENTRY serviceTable[] =
    94. {
    95. { serviceName, ServiceMain },
    96. { 0, 0 }
    97. };
    98.  
    99. StartServiceCtrlDispatcher(serviceTable);
    100. }
    101.  
    102. void InstallService()
    103. {
    104. SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
    105.  
    106. if (serviceControlManager)
    107. {
    108. TCHAR path[_MAX_PATH + 1];
    109. if (GetModuleFileName(0, path, sizeof(path) / sizeof(path[0])) > 0)
    110. {
    111. SC_HANDLE service = CreateService(serviceControlManager,
    112. serviceName, serviceName,
    113. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
    114. SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
    115. 0, 0, 0, 0, 0);
    116. if (service)
    117. CloseServiceHandle(service);
    118. }
    119.  
    120. CloseServiceHandle(serviceControlManager);
    121. }
    122. }
    123.  
    124. void UninstallService()
    125. {
    126. SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
    127.  
    128. if (serviceControlManager)
    129. {
    130. SC_HANDLE service = OpenService(serviceControlManager,
    131. serviceName, SERVICE_QUERY_STATUS | DELETE);
    132. if (service)
    133. {
    134. SERVICE_STATUS serviceStatus;
    135. if (QueryServiceStatus(service, &serviceStatus))
    136. {
    137. if (serviceStatus.dwCurrentState == SERVICE_STOPPED)
    138. DeleteService(service);
    139. }
    140.  
    141. CloseServiceHandle(service);
    142. }
    143.  
    144. CloseServiceHandle(serviceControlManager);
    145. }
    146. }
    147.  
    148. int _tmain(int argc, TCHAR* argv[])
    149. {
    150.  
    151. if (argc > 1 && lstrcmpi(argv[1], TEXT("install")) == 0)
    152. {
    153. InstallService();
    154. }
    155. else if (argc > 1 && lstrcmpi(argv[1], TEXT("uninstall")) == 0)
    156. {
    157. UninstallService();
    158. }
    159. else
    160. {
    161. RunService();
    162. }
    163.  
    164. return 0;
    165. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: NT Service with networking support


Similar Threads

  1. networking
    By panyang in forum Newbie
    Replies: 1
    Last Post: 6th February 2011, 09:53
  2. QT non-gui networking
    By MAbeeTT in forum Newbie
    Replies: 1
    Last Post: 21st August 2009, 19:47
  3. Qt-3 and networking
    By a550ee in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2006, 11:15

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.