Advertisement
Guest User

WinAPI Services2

a guest
Jun 16th, 2019
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.13 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. SERVICE_STATUS        g_ServiceStatus = { 0 };
  5. SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
  6. HANDLE                g_ServiceStopEvent = INVALID_HANDLE_VALUE;
  7.  
  8. VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
  9. VOID WINAPI ServiceCtrlHandler(DWORD);
  10. DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
  11.  
  12. #define SERVICE_NAME  L"MySampleService"
  13.  
  14. int wmain(int argc, LPWSTR* argv)
  15. {
  16.  
  17.     SERVICE_TABLE_ENTRY ServiceTable[] =
  18.     {
  19.         { (LPWSTR)SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
  20.         {NULL, NULL}
  21.     };
  22.  
  23.     if (argc == 2)
  24.     {
  25.         SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
  26.  
  27.         if (!wcscmp(argv[1], L"-create"))
  28.         {
  29.             std::wcout << L"Creating service..." << std::endl;
  30.  
  31.             SC_HANDLE handErr = CreateService(
  32.                 hSCManager,
  33.                 SERVICE_NAME,
  34.                 SERVICE_NAME,
  35.                 SERVICE_ALL_ACCESS,
  36.                 SERVICE_WIN32_OWN_PROCESS,
  37.                 SERVICE_DEMAND_START,
  38.                 SERVICE_ERROR_NORMAL,
  39.                 argv[0],
  40.                 NULL, NULL, NULL, NULL, NULL);
  41.  
  42.             if (handErr == NULL)
  43.             {
  44.                 std::wcout << L"Error of creating service!" << std::endl;
  45.                 return 1;
  46.             }
  47.             std::wcout << L"Service created!" << std::endl;
  48.         }
  49.         else if (!wcscmp(argv[1], L"-start"))
  50.         {
  51.             std::wcout << L"Starting service..." << std::endl;
  52.             SC_HANDLE service = OpenService(hSCManager, SERVICE_NAME, SERVICE_START);
  53.             if (!StartService(service, 0, nullptr))
  54.             {
  55.                 std::wcout << L"Error of starting service!" << std::endl;
  56.                 return 2;
  57.             }
  58.             std::wcout << L"Service started!" << std::endl;
  59.         }
  60.         else if (!wcscmp(argv[1], L"-delete"))
  61.         {
  62.             DeleteService(OpenService(hSCManager, SERVICE_NAME, SERVICE_STOP | DELETE));
  63.             std::wcout << L"Service deleted!" << std::endl;
  64.         }
  65.         else if (!wcscmp(argv[1], L"-stop"))
  66.         {
  67.             std::wcout << L"Stopping service..." << std::endl;
  68.             DeleteService(OpenService(hSCManager, SERVICE_NAME, SERVICE_STOP | DELETE));
  69.             SC_HANDLE handErr = CreateService(
  70.                 hSCManager,
  71.                 SERVICE_NAME,
  72.                 SERVICE_NAME,
  73.                 SERVICE_ALL_ACCESS,
  74.                 SERVICE_WIN32_OWN_PROCESS,
  75.                 SERVICE_DEMAND_START,
  76.                 SERVICE_ERROR_NORMAL,
  77.                 argv[0],
  78.                 NULL, NULL, NULL, NULL, NULL);
  79.  
  80.             if (handErr == NULL)
  81.             {
  82.                 std::wcout << L"Error of creating service!" << std::endl;
  83.                 return 1;
  84.             }
  85.             std::wcout << L"Service stopped!" << std::endl;
  86.  
  87.         }
  88.     }
  89.  
  90.     if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
  91.     {
  92.         system("pause");
  93.         return GetLastError();
  94.     }
  95.  
  96.     system("pause");
  97.     return 0;
  98. }
  99.  
  100. VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
  101. {
  102.     DWORD Status = E_FAIL;
  103.  
  104.     // Register our service control handler with the SCM
  105.     g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
  106.  
  107.     if (g_StatusHandle == NULL)
  108.         return;
  109.  
  110.     // Tell the service controller we are starting
  111.     ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
  112.     g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  113.     g_ServiceStatus.dwControlsAccepted = 0;
  114.     g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  115.     g_ServiceStatus.dwWin32ExitCode = 0;
  116.     g_ServiceStatus.dwServiceSpecificExitCode = 0;
  117.     g_ServiceStatus.dwCheckPoint = 0;
  118.  
  119.     if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  120.     {
  121.         OutputDebugString(
  122.             L"My Sample Service: ServiceMain: SetServiceStatus returned error");
  123.     }
  124.  
  125.     /*
  126.      * Perform tasks necessary to start the service here
  127.      */
  128.  
  129.      // Create a service stop event to wait on later
  130.     g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  131.     if (g_ServiceStopEvent == NULL)
  132.     {
  133.         // Error creating event
  134.         // Tell service controller we are stopped and exit
  135.         g_ServiceStatus.dwControlsAccepted = 0;
  136.         g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  137.         g_ServiceStatus.dwWin32ExitCode = GetLastError();
  138.         g_ServiceStatus.dwCheckPoint = 1;
  139.  
  140.         if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  141.         {
  142.             OutputDebugString(
  143.                 L"My Sample Service: ServiceMain: SetServiceStatus returned error");
  144.         }
  145.         return;
  146.     }
  147.  
  148.     // Tell the service controller we are started
  149.     g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  150.     g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  151.     g_ServiceStatus.dwWin32ExitCode = 0;
  152.     g_ServiceStatus.dwCheckPoint = 0;
  153.  
  154.     if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  155.     {
  156.         OutputDebugString(
  157.             L"My Sample Service: ServiceMain: SetServiceStatus returned error");
  158.     }
  159.  
  160.     // Start a thread that will perform the main task of the service
  161.     HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
  162.  
  163.     // Wait until our worker thread exits signaling that the service needs to stop
  164.     WaitForSingleObject(hThread, INFINITE);
  165.  
  166.  
  167.     /*
  168.      * Perform any cleanup tasks
  169.      */
  170.  
  171.     CloseHandle(g_ServiceStopEvent);
  172.  
  173.     // Tell the service controller we are stopped
  174.     g_ServiceStatus.dwControlsAccepted = 0;
  175.     g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  176.     g_ServiceStatus.dwWin32ExitCode = 0;
  177.     g_ServiceStatus.dwCheckPoint = 3;
  178.  
  179.     if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  180.     {
  181.         OutputDebugString(
  182.             L"My Sample Service: ServiceMain: SetServiceStatus returned error");
  183.     }
  184. }
  185.  
  186. VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
  187. {
  188.     switch (CtrlCode)
  189.     {
  190.     case SERVICE_CONTROL_STOP:
  191.  
  192.         if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
  193.             break;
  194.  
  195.         /*
  196.          * Perform tasks necessary to stop the service here
  197.          */
  198.  
  199.         g_ServiceStatus.dwControlsAccepted = 0;
  200.         g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  201.         g_ServiceStatus.dwWin32ExitCode = 0;
  202.         g_ServiceStatus.dwCheckPoint = 4;
  203.  
  204.         if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  205.         {
  206.             OutputDebugString(
  207.                 L"My Sample Service: ServiceCtrlHandler: SetServiceStatus returned error");
  208.         }
  209.  
  210.         // This will signal the worker thread to start shutting down
  211.         SetEvent(g_ServiceStopEvent);
  212.  
  213.         break;
  214.  
  215.     default:
  216.         break;
  217.     }
  218. }
  219.  
  220. DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
  221. {
  222.     //  Periodically check if the service has been requested to stop
  223.     while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
  224.     {
  225.         /*
  226.          * Perform main service function here
  227.          */
  228.  
  229.          //  Simulate some work by sleeping
  230.         Sleep(3000);
  231.     }
  232.  
  233.     return ERROR_SUCCESS;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement