Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // test.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #include <Windows.h>
- #include <string>
- #pragma comment(lib, "advapi32.lib")
- //defines here
- #define ServiceName L"test"
- //globals here
- SERVICE_STATUS ServiceStatus;
- SERVICE_STATUS_HANDLE ServiceStatusHandle;
- HANDLE ServiceStopEvent = NULL;
- DWORD WINAPI PopupThread(LPVOID lpParameter) {
- return 0;
- }
- VOID ReportServiceStatus(DWORD CurrentState, DWORD Win32ExitCode, DWORD WaitHint) {
- static DWORD CheckPoint = 1;
- ServiceStatus.dwCurrentState = CurrentState;
- ServiceStatus.dwWin32ExitCode = Win32ExitCode;
- ServiceStatus.dwWaitHint = WaitHint;
- if (CurrentState == SERVICE_START_PENDING) {
- ServiceStatus.dwControlsAccepted = 0;
- }
- else {
- ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
- }
- if ((CurrentState == SERVICE_RUNNING) ||
- (CurrentState == SERVICE_STOPPED))
- ServiceStatus.dwCheckPoint = 0;
- else ServiceStatus.dwCheckPoint = CheckPoint++;
- SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
- }
- VOID WINAPI ServiceControlHandler(DWORD Control) {
- switch (Control)
- {
- case SERVICE_CONTROL_STOP:
- ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
- SetEvent(ServiceStopEvent);
- ReportServiceStatus(ServiceStatus.dwCurrentState, NO_ERROR, 0);
- case SERVICE_CONTROL_INTERROGATE:
- break;
- default:
- break;
- }
- }
- VOID ServiceWorker(DWORD Argc, LPTSTR *Argv) {
- //we need to create an event that the svcctrlhhandler will use to signal when it recieves the stop code
- ServiceStopEvent = CreateEvent(
- NULL,
- TRUE,
- FALSE,
- NULL
- );
- if (ServiceStopEvent == NULL) {
- ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
- return;
- }
- ReportServiceStatus(SERVICE_RUNNING, NO_ERROR, 0);
- DWORD ThreadID;
- HANDLE myHandle = CreateThread(0, 0, PopupThread, NULL , 0, &ThreadID);
- while (1) {
- WaitForSingleObject(ServiceStopEvent, INFINITE);
- ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
- return;
- }
- }
- VOID WINAPI ServiceMain(DWORD Argc, LPTSTR *Argv) {
- ServiceStatusHandle = RegisterServiceCtrlHandler(
- ServiceName,
- ServiceControlHandler
- );
- ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
- ServiceStatus.dwServiceSpecificExitCode = 0;
- ReportServiceStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
- ServiceWorker(Argc, Argv);
- }
- int main()
- {
- SERVICE_TABLE_ENTRY DispatchTable[] =
- {
- {(LPWSTR)ServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
- {NULL, NULL}
- };
- StartServiceCtrlDispatcher(DispatchTable);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement