Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. // test.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <Windows.h>
  7. #include <string>
  8.  
  9. #pragma comment(lib, "advapi32.lib")
  10.  
  11. //defines here
  12. #define ServiceName L"test"
  13.  
  14. //globals here
  15. SERVICE_STATUS ServiceStatus;
  16. SERVICE_STATUS_HANDLE ServiceStatusHandle;
  17. HANDLE ServiceStopEvent = NULL;
  18.  
  19. DWORD WINAPI PopupThread(LPVOID lpParameter) {
  20.  
  21.  
  22.  
  23. return 0;
  24. }
  25.  
  26. VOID ReportServiceStatus(DWORD CurrentState, DWORD Win32ExitCode, DWORD WaitHint) {
  27.  
  28. static DWORD CheckPoint = 1;
  29.  
  30. ServiceStatus.dwCurrentState = CurrentState;
  31. ServiceStatus.dwWin32ExitCode = Win32ExitCode;
  32. ServiceStatus.dwWaitHint = WaitHint;
  33.  
  34. if (CurrentState == SERVICE_START_PENDING) {
  35. ServiceStatus.dwControlsAccepted = 0;
  36. }
  37. else {
  38. ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  39. }
  40. if ((CurrentState == SERVICE_RUNNING) ||
  41. (CurrentState == SERVICE_STOPPED))
  42. ServiceStatus.dwCheckPoint = 0;
  43. else ServiceStatus.dwCheckPoint = CheckPoint++;
  44.  
  45.  
  46. SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
  47.  
  48. }
  49.  
  50. VOID WINAPI ServiceControlHandler(DWORD Control) {
  51.  
  52. switch (Control)
  53. {
  54. case SERVICE_CONTROL_STOP:
  55. ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
  56. SetEvent(ServiceStopEvent);
  57. ReportServiceStatus(ServiceStatus.dwCurrentState, NO_ERROR, 0);
  58. case SERVICE_CONTROL_INTERROGATE:
  59. break;
  60.  
  61. default:
  62. break;
  63. }
  64.  
  65. }
  66.  
  67. VOID ServiceWorker(DWORD Argc, LPTSTR *Argv) {
  68.  
  69. //we need to create an event that the svcctrlhhandler will use to signal when it recieves the stop code
  70. ServiceStopEvent = CreateEvent(
  71. NULL,
  72. TRUE,
  73. FALSE,
  74. NULL
  75. );
  76.  
  77. if (ServiceStopEvent == NULL) {
  78. ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
  79. return;
  80. }
  81.  
  82. ReportServiceStatus(SERVICE_RUNNING, NO_ERROR, 0);
  83.  
  84. DWORD ThreadID;
  85. HANDLE myHandle = CreateThread(0, 0, PopupThread, NULL , 0, &ThreadID);
  86.  
  87. while (1) {
  88.  
  89. WaitForSingleObject(ServiceStopEvent, INFINITE);
  90. ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
  91. return;
  92. }
  93. }
  94.  
  95. VOID WINAPI ServiceMain(DWORD Argc, LPTSTR *Argv) {
  96.  
  97. ServiceStatusHandle = RegisterServiceCtrlHandler(
  98. ServiceName,
  99. ServiceControlHandler
  100. );
  101.  
  102. ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  103. ServiceStatus.dwServiceSpecificExitCode = 0;
  104.  
  105. ReportServiceStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
  106.  
  107. ServiceWorker(Argc, Argv);
  108.  
  109. }
  110.  
  111. int main()
  112. {
  113.  
  114. SERVICE_TABLE_ENTRY DispatchTable[] =
  115. {
  116. {(LPWSTR)ServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
  117. {NULL, NULL}
  118. };
  119.  
  120. StartServiceCtrlDispatcher(DispatchTable);
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement