Refactoring code
This commit is contained in:
+30
-253
@@ -12,63 +12,23 @@
|
|||||||
|
|
||||||
#include "Models.h"
|
#include "Models.h"
|
||||||
#include "ServiceEntryPoint.hpp"
|
#include "ServiceEntryPoint.hpp"
|
||||||
#include "ServiceControlHandler.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
#define SVCNAME TEXT("ServiceExample")
|
template<typename Handle = SC_HANDLE, typename Char = wchar_t>
|
||||||
// #define SERVICE_NAME _T("My Service Example")
|
|
||||||
|
|
||||||
// service::ServiceControlHandler::g_ServiceStatus = {0};
|
|
||||||
// SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
|
|
||||||
// service::ServiceControlHandler::g_StatusHandle = nullptr;
|
|
||||||
SERVICE_STATUS g_ServiceStatus;
|
|
||||||
// static SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
|
|
||||||
SERVICE_STATUS_HANDLE g_StatusHandle;
|
|
||||||
|
|
||||||
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
|
|
||||||
|
|
||||||
// template<typename D = DWORD, typename LStr = LPTSTR>
|
|
||||||
// VOID WINAPI ServiceMain(D, LStr *);
|
|
||||||
VOID ServiceInstall(void);
|
VOID ServiceInstall(void);
|
||||||
VOID WINAPI ServiceMain(DWORD, LPTSTR*);
|
template<typename TSTR = LPTSTR, typename CTSTR = LPCTSTR,
|
||||||
VOID WINAPI ServiceCtrlHandler(DWORD);
|
typename Handle = HANDLE, typename Char = TCHAR>
|
||||||
// DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
|
|
||||||
|
|
||||||
VOID ReportServiceStatus(DWORD, DWORD, DWORD);
|
|
||||||
VOID ServiceInit(DWORD, LPTSTR*);
|
|
||||||
VOID ServiceReportEvent(LPTSTR);
|
VOID ServiceReportEvent(LPTSTR);
|
||||||
|
|
||||||
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
|
|
||||||
|
|
||||||
// models::ServiceInfo<> service_info(L"Service Example");
|
|
||||||
std::shared_ptr<models::ServiceInfo<>> service_info(new models::ServiceInfo<>(L"Service Example"));
|
std::shared_ptr<models::ServiceInfo<>> service_info(new models::ServiceInfo<>(L"Service Example"));
|
||||||
|
|
||||||
|
|
||||||
#undef main
|
#undef main
|
||||||
|
|
||||||
// int main()
|
|
||||||
int _tmain(int argc, TCHAR *argv[])
|
int _tmain(int argc, TCHAR *argv[])
|
||||||
// int __cdel_tmain(int argc, TCHAR *argv[])
|
|
||||||
{
|
{
|
||||||
std::cout << "ServiceExample\n";
|
std::cout << "ServiceExample\n";
|
||||||
|
|
||||||
auto v = TEXT("sss");
|
|
||||||
|
|
||||||
// service::ServiceControlHandler::g_ServiceStatus = {0};
|
|
||||||
// SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
|
|
||||||
// service::ServiceControlHandler::g_StatusHandle = nullptr;
|
|
||||||
|
|
||||||
/**
|
|
||||||
SERVICE_TABLE_ENTRY SERVICETABLE[] =
|
|
||||||
{
|
|
||||||
// { SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain<DWORD, LPTSTR> },
|
|
||||||
{ SVCNAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
// if (StartServiceCtr)
|
|
||||||
|
|
||||||
if (lstrcmpi(argv[1], TEXT("install")) == 0)
|
if (lstrcmpi(argv[1], TEXT("install")) == 0)
|
||||||
{
|
{
|
||||||
ServiceInstall();
|
ServiceInstall();
|
||||||
@@ -76,18 +36,12 @@ int _tmain(int argc, TCHAR *argv[])
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
service::ServiceEntryPoint entry;
|
|
||||||
// models::ServiceInfo<> service_info(TEXT("ServiceExample"));
|
|
||||||
// models::ServiceInfo<> service_info(L"Service Example");
|
|
||||||
|
|
||||||
SERVICE_TABLE_ENTRY SERVICETABLE[] =
|
SERVICE_TABLE_ENTRY SERVICETABLE[] =
|
||||||
{
|
{
|
||||||
// { SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain<DWORD, LPTSTR> },
|
{ service_info->service_name,
|
||||||
// { SVCNAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
|
(LPSERVICE_MAIN_FUNCTION)
|
||||||
// { service_info.service_name, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
|
service::ServiceEntryPoint::service_main_start },
|
||||||
// { (const_cast<wchar_t*>(service_info.service_name)) , (LPSERVICE_MAIN_FUNCTION)entry.service_main },
|
|
||||||
// { SVCNAME, (LPSERVICE_MAIN_FUNCTION)entry.service_main_start},
|
|
||||||
{ SVCNAME, (LPSERVICE_MAIN_FUNCTION)service::ServiceEntryPoint::service_main_start },
|
|
||||||
{ nullptr, nullptr },
|
{ nullptr, nullptr },
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -103,14 +57,11 @@ int _tmain(int argc, TCHAR *argv[])
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Handle, typename Char>
|
||||||
VOID ServiceInstall()
|
VOID ServiceInstall()
|
||||||
{
|
{
|
||||||
SC_HANDLE schSCManager;
|
Char buffer[MAX_PATH];
|
||||||
SC_HANDLE schService;
|
|
||||||
// TCHAR szPath[MAX_PATH];
|
|
||||||
wchar_t buffer[MAX_PATH];
|
|
||||||
|
|
||||||
// if (!GetModuleFileName("", szPath, MAX_PATH))
|
|
||||||
if (!GetModuleFileName(nullptr, buffer, MAX_PATH))
|
if (!GetModuleFileName(nullptr, buffer, MAX_PATH))
|
||||||
{
|
{
|
||||||
std::cout << "Cannot install service " << GetLastError() << "\n";
|
std::cout << "Cannot install service " << GetLastError() << "\n";
|
||||||
@@ -120,12 +71,12 @@ VOID ServiceInstall()
|
|||||||
|
|
||||||
// Get a handle to the SCM database.
|
// Get a handle to the SCM database.
|
||||||
|
|
||||||
schSCManager = OpenSCManager(
|
Handle schSCManager = OpenSCManager(
|
||||||
NULL, // local computer
|
nullptr, // local computer
|
||||||
NULL, // ServicesActive database
|
nullptr, // ServicesActive database
|
||||||
SC_MANAGER_ALL_ACCESS); // full access rights
|
SC_MANAGER_ALL_ACCESS); // full access rights
|
||||||
|
|
||||||
if (NULL == schSCManager)
|
if (nullptr == schSCManager)
|
||||||
{
|
{
|
||||||
std::cout << "OpenSCManager failed " << GetLastError() << "\n";
|
std::cout << "OpenSCManager failed " << GetLastError() << "\n";
|
||||||
|
|
||||||
@@ -134,25 +85,23 @@ VOID ServiceInstall()
|
|||||||
|
|
||||||
// Create the service
|
// Create the service
|
||||||
|
|
||||||
schService = CreateService(
|
Handle schService = CreateService(
|
||||||
schSCManager, // SCM database
|
schSCManager, // SCM database
|
||||||
SVCNAME, // name of service
|
service_info->service_name, // name of service
|
||||||
SVCNAME, // service name to display
|
service_info->service_name, // service name to display
|
||||||
SERVICE_ALL_ACCESS, // desired access
|
SERVICE_ALL_ACCESS, // desired access
|
||||||
SERVICE_WIN32_OWN_PROCESS, // service type
|
SERVICE_WIN32_OWN_PROCESS, // service type
|
||||||
SERVICE_DEMAND_START, // start type
|
SERVICE_DEMAND_START, // start type
|
||||||
SERVICE_ERROR_NORMAL, // error control type
|
SERVICE_ERROR_NORMAL, // error control type
|
||||||
// szPath, // path to service's binary
|
|
||||||
buffer, // path to service's binary
|
buffer, // path to service's binary
|
||||||
NULL, // no load ordering group
|
nullptr, // no load ordering group
|
||||||
NULL, // no tag identifier
|
nullptr, // no tag identifier
|
||||||
NULL, // no dependencies
|
nullptr, // no dependencies
|
||||||
NULL, // LocalSystem account
|
nullptr, // LocalSystem account
|
||||||
NULL); // no password
|
nullptr); // no password
|
||||||
|
|
||||||
if (schService == NULL)
|
if (schService == nullptr)
|
||||||
{
|
{
|
||||||
// printf("CreateService failed (%d)\n", GetLastError());
|
|
||||||
std::cout << "CreateService failed " << GetLastError() << "\n";
|
std::cout << "CreateService failed " << GetLastError() << "\n";
|
||||||
|
|
||||||
CloseServiceHandle(schSCManager);
|
CloseServiceHandle(schSCManager);
|
||||||
@@ -160,7 +109,6 @@ VOID ServiceInstall()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// printf("Service installed successfully\n");
|
|
||||||
std::cout << "Service installed successfully\n";
|
std::cout << "Service installed successfully\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,204 +116,33 @@ VOID ServiceInstall()
|
|||||||
CloseServiceHandle(schSCManager);
|
CloseServiceHandle(schSCManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename TStr, typename CTStr,
|
||||||
|
typename Handle, typename Char>
|
||||||
VOID ServiceReportEvent(LPTSTR szFunction)
|
VOID ServiceReportEvent(LPTSTR szFunction)
|
||||||
{
|
{
|
||||||
HANDLE hEventSource;
|
CTStr lpszStrings[2];
|
||||||
LPCTSTR lpszStrings[2];
|
Char Buffer[80];
|
||||||
TCHAR Buffer[80];
|
|
||||||
|
|
||||||
hEventSource = RegisterEventSource(NULL, SVCNAME);
|
Handle hEventSource = RegisterEventSource(nullptr, service_info->service_name);
|
||||||
|
|
||||||
if (NULL != hEventSource)
|
if (nullptr != hEventSource)
|
||||||
{
|
{
|
||||||
StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction, GetLastError());
|
StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction, GetLastError());
|
||||||
|
|
||||||
lpszStrings[0] = SVCNAME;
|
lpszStrings[0] = service_info->service_name;
|
||||||
lpszStrings[1] = Buffer;
|
lpszStrings[1] = Buffer;
|
||||||
|
|
||||||
ReportEvent(hEventSource, // event log handle
|
ReportEvent(hEventSource, // event log handle
|
||||||
EVENTLOG_ERROR_TYPE, // event type
|
EVENTLOG_ERROR_TYPE, // event type
|
||||||
0, // event category
|
0, // event category
|
||||||
// SVC_ERROR,
|
|
||||||
SERVICE_ERROR_NORMAL, // event identifier
|
SERVICE_ERROR_NORMAL, // event identifier
|
||||||
NULL, // no security identifier
|
nullptr, // no security identifier
|
||||||
2, // size of lpszStrings array
|
2, // size of lpszStrings array
|
||||||
0, // no binary data
|
0, // no binary data
|
||||||
lpszStrings, // array of strings
|
lpszStrings, // array of strings
|
||||||
NULL); // no binary data
|
nullptr); // no binary data
|
||||||
|
|
||||||
DeregisterEventSource(hEventSource);
|
DeregisterEventSource(hEventSource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
VOID ReportServiceStatus(DWORD dwCurrentState,
|
|
||||||
DWORD dwWin32ExitCode,
|
|
||||||
DWORD dwWaitHint)
|
|
||||||
{
|
|
||||||
static DWORD dwCheckPoint = 1;
|
|
||||||
|
|
||||||
g_ServiceStatus.dwCurrentState = dwCurrentState;
|
|
||||||
g_ServiceStatus.dwWin32ExitCode = dwWin32ExitCode;
|
|
||||||
g_ServiceStatus.dwWaitHint = dwWaitHint;
|
|
||||||
|
|
||||||
if (dwCurrentState == SERVICE_START_PENDING)
|
|
||||||
{
|
|
||||||
g_ServiceStatus.dwControlsAccepted = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((dwCurrentState == SERVICE_RUNNING) ||
|
|
||||||
(dwCurrentState == SERVICE_STOPPED))
|
|
||||||
{
|
|
||||||
g_ServiceStatus.dwCheckPoint = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g_ServiceStatus.dwCheckPoint = dwCheckPoint++;
|
|
||||||
}
|
|
||||||
|
|
||||||
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
VOID WINAPI ServiceCtrlHandler(DWORD dwCtrl)
|
|
||||||
{
|
|
||||||
switch (dwCtrl)
|
|
||||||
{
|
|
||||||
case SERVICE_CONTROL_STOP:
|
|
||||||
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
|
|
||||||
|
|
||||||
SetEvent(g_ServiceStopEvent);
|
|
||||||
ReportServiceStatus(g_ServiceStatus.dwCurrentState, NO_ERROR, 0);
|
|
||||||
|
|
||||||
return;
|
|
||||||
case SERVICE_CONTROL_INTERROGATE:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
|
|
||||||
// template<typename D, typename LStr>
|
|
||||||
// VOID WINAPI ServiceMain(D argc, LStr *argv)
|
|
||||||
{
|
|
||||||
g_StatusHandle = RegisterServiceCtrlHandler(
|
|
||||||
SVCNAME,
|
|
||||||
ServiceCtrlHandler);
|
|
||||||
|
|
||||||
if (!g_StatusHandle)
|
|
||||||
{
|
|
||||||
// Report the service event
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Tell the service controller we are starting
|
|
||||||
ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
|
|
||||||
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
||||||
g_ServiceStatus.dwControlsAccepted = 0;
|
|
||||||
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
|
|
||||||
g_ServiceStatus.dwWin32ExitCode = 0;
|
|
||||||
g_ServiceStatus.dwServiceSpecificExitCode = 0;
|
|
||||||
g_ServiceStatus.dwCheckPoint = 0;
|
|
||||||
|
|
||||||
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
|
|
||||||
{
|
|
||||||
OutputDebugString(_T(
|
|
||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Perform tasks necessary to start the service here
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Create a service stop event to wait on later
|
|
||||||
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
||||||
if (g_ServiceStopEvent == NULL)
|
|
||||||
{
|
|
||||||
// Error creating event
|
|
||||||
// Tell service controller we are stopped and exit
|
|
||||||
g_ServiceStatus.dwControlsAccepted = 0;
|
|
||||||
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
|
|
||||||
g_ServiceStatus.dwWin32ExitCode = GetLastError();
|
|
||||||
g_ServiceStatus.dwCheckPoint = 1;
|
|
||||||
|
|
||||||
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
|
|
||||||
{
|
|
||||||
OutputDebugString(_T(
|
|
||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Tell the service controller we are started
|
|
||||||
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|
||||||
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
|
|
||||||
g_ServiceStatus.dwWin32ExitCode = 0;
|
|
||||||
g_ServiceStatus.dwCheckPoint = 0;
|
|
||||||
|
|
||||||
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
|
|
||||||
{
|
|
||||||
OutputDebugString(_T(
|
|
||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start a thread that will perform the main task of the service
|
|
||||||
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
|
|
||||||
|
|
||||||
// Wait until our worker thread exits signaling that the service needs to stop
|
|
||||||
WaitForSingleObject(hThread, INFINITE);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Perform any cleanup tasks
|
|
||||||
*/
|
|
||||||
|
|
||||||
CloseHandle(g_ServiceStopEvent);
|
|
||||||
|
|
||||||
// Tell the service controller we are stopped
|
|
||||||
g_ServiceStatus.dwControlsAccepted = 0;
|
|
||||||
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
|
|
||||||
g_ServiceStatus.dwWin32ExitCode = 0;
|
|
||||||
g_ServiceStatus.dwCheckPoint = 3;
|
|
||||||
|
|
||||||
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
|
|
||||||
{
|
|
||||||
OutputDebugString(_T(
|
|
||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
|
|
||||||
{
|
|
||||||
// const auto some_file_path = "C:\\Users\\brahmix\\example_file.txt";
|
|
||||||
const auto some_file_path = "C:\\example_file.txt";
|
|
||||||
|
|
||||||
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
|
|
||||||
{
|
|
||||||
std::fstream file(some_file_path, std::ios::out | std::ios::app);
|
|
||||||
|
|
||||||
const auto content = "test";
|
|
||||||
|
|
||||||
file << content << "\n";
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
Sleep(3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return ERROR_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,20 +14,20 @@ namespace service
|
|||||||
class ServiceControlHandler
|
class ServiceControlHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<typename D = DWORD>
|
template<typename D = DWORD,
|
||||||
|
typename Obj = models::ServiceInfo<>,
|
||||||
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
static VOID WINAPI ServiceCtrlHandler(D ctrlCode)
|
static VOID WINAPI ServiceCtrlHandler(D ctrlCode)
|
||||||
{
|
{
|
||||||
extern std::shared_ptr<models::ServiceInfo<>> service_info;
|
extern Ptr service_info;
|
||||||
|
|
||||||
switch (ctrlCode)
|
switch (ctrlCode)
|
||||||
{
|
{
|
||||||
case SERVICE_CONTROL_STOP:
|
case SERVICE_CONTROL_STOP:
|
||||||
// ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
|
ReportServiceStatus<D>(SERVICE_STOP_PENDING, NO_ERROR, 0);
|
||||||
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
|
|
||||||
|
|
||||||
SetEvent(service_info->service_stop_event);
|
SetEvent(service_info->service_stop_event);
|
||||||
// ReportServiceStatus(g_ServiceStatus.dwCurrentState, NO_ERROR, 0);
|
ReportServiceStatus<D>(service_info->service_status.dwCurrentState, NO_ERROR, 0);
|
||||||
ReportServiceStatus(service_info->service_status.dwCurrentState, NO_ERROR, 0);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case SERVICE_CONTROL_INTERROGATE:
|
case SERVICE_CONTROL_INTERROGATE:
|
||||||
@@ -36,21 +36,18 @@ namespace service
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// static SERVICE_STATUS g_ServiceStatus = {0};
|
|
||||||
// static SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
|
|
||||||
// static SERVICE_STATUS g_ServiceStatus;
|
|
||||||
// static SERVICE_STATUS_HANDLE g_StatusHandle = nullptr;
|
|
||||||
// static SERVICE_STATUS_HANDLE g_StatusHandle;
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename D = DWORD,
|
||||||
static VOID ReportServiceStatus(DWORD dwCurrentState,
|
typename Obj = models::ServiceInfo<>,
|
||||||
DWORD dwWin32ExitCode,
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
DWORD dwWaitHint)
|
static VOID ReportServiceStatus(D dwCurrentState,
|
||||||
|
D dwWin32ExitCode,
|
||||||
|
D dwWaitHint)
|
||||||
{
|
{
|
||||||
extern std::shared_ptr<models::ServiceInfo<>> service_info;
|
extern Ptr service_info;
|
||||||
|
|
||||||
static DWORD dwCheckPoint = 1;
|
static D dwCheckPoint = 1;
|
||||||
|
|
||||||
service_info->service_status.dwCurrentState = dwCurrentState;
|
service_info->service_status.dwCurrentState = dwCurrentState;
|
||||||
service_info->service_status.dwWin32ExitCode = dwWin32ExitCode;
|
service_info->service_status.dwWin32ExitCode = dwWin32ExitCode;
|
||||||
@@ -75,7 +72,6 @@ namespace service
|
|||||||
service_info->service_status.dwCheckPoint = dwCheckPoint++;
|
service_info->service_status.dwCheckPoint = dwCheckPoint++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetServiceStatus(g_StatusHandle, &service_info->service_status);
|
|
||||||
SetServiceStatus(service_info->service_handle, &service_info->service_status);
|
SetServiceStatus(service_info->service_handle, &service_info->service_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,60 +16,85 @@ namespace service
|
|||||||
class ServiceEntryPoint
|
class ServiceEntryPoint
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ServiceEntryPoint()
|
ServiceEntryPoint() = default;
|
||||||
{
|
|
||||||
initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceEntryPoint(std::wstring name)
|
template<typename D = DWORD, typename TStr = LPTSTR,
|
||||||
|
typename Handle = HANDLE,
|
||||||
|
typename Obj = models::ServiceInfo<>,
|
||||||
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
|
static VOID WINAPI service_main_start(D argc, TStr *argv)
|
||||||
{
|
{
|
||||||
// service_name = static_cast<std::wstring>(name);
|
extern Ptr service_info;
|
||||||
// service_name = name.c_str();
|
|
||||||
initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
static VOID WINAPI service_main_start(DWORD argc, LPTSTR *argv)
|
|
||||||
{
|
|
||||||
// extern models::ServiceInfo<> service_info;
|
|
||||||
extern std::shared_ptr<models::ServiceInfo<>> service_info;
|
|
||||||
|
|
||||||
|
|
||||||
service_info->service_handle = RegisterServiceCtrlHandler(
|
service_info->service_handle = RegisterServiceCtrlHandler(
|
||||||
// SVCNAME,
|
|
||||||
service_info->service_name,
|
service_info->service_name,
|
||||||
// ServiceCtrlHandler);
|
|
||||||
service::ServiceControlHandler::ServiceCtrlHandler);
|
service::ServiceControlHandler::ServiceCtrlHandler);
|
||||||
|
|
||||||
if (!service_info->service_handle)
|
if (!service_info->service_handle)
|
||||||
{
|
{
|
||||||
// Report the service event
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notify_service_controller_starting(service_info);
|
||||||
|
|
||||||
|
|
||||||
// Tell the service controller we are starting
|
if (!starting_service(service_info))
|
||||||
ZeroMemory(&service_info->service_status, sizeof(service_info->service_status));
|
|
||||||
service_info->service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
||||||
service_info->service_status.dwControlsAccepted = 0;
|
|
||||||
service_info->service_status.dwCurrentState = SERVICE_START_PENDING;
|
|
||||||
service_info->service_status.dwWin32ExitCode = 0;
|
|
||||||
service_info->service_status.dwServiceSpecificExitCode = 0;
|
|
||||||
service_info->service_status.dwCheckPoint = 0;
|
|
||||||
|
|
||||||
if (SetServiceStatus(service_info->service_handle, &service_info->service_status) == FALSE)
|
|
||||||
{
|
{
|
||||||
OutputDebugString(_T(
|
return;
|
||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
notify_service_controller_started(service_info);
|
||||||
|
|
||||||
|
// Start a thread that will perform the main task of the service
|
||||||
|
Handle hThread = CreateThread(nullptr, 0, ServiceWorkerThread, nullptr, 0, nullptr);
|
||||||
|
|
||||||
|
// Wait until our worker thread exits signaling that the service needs to stop
|
||||||
|
WaitForSingleObject(hThread, INFINITE);
|
||||||
|
|
||||||
|
|
||||||
|
close_handle(service_info);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
template<typename D = DWORD, typename Lp = LPVOID,
|
||||||
|
typename Obj = models::ServiceInfo<>,
|
||||||
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
|
static D WINAPI ServiceWorkerThread(Lp lpParam)
|
||||||
|
{
|
||||||
|
extern Ptr service_info;
|
||||||
|
|
||||||
|
const auto INTERVAL = 1500;
|
||||||
|
|
||||||
|
const auto some_file_path = "C:\\example_file.txt";
|
||||||
|
|
||||||
|
while (WaitForSingleObject(service_info->service_stop_event, 0) != WAIT_OBJECT_0)
|
||||||
|
{
|
||||||
|
std::fstream file(some_file_path, std::ios::out | std::ios::app);
|
||||||
|
|
||||||
|
const auto content = "burrow";
|
||||||
|
|
||||||
|
file << content << "\n";
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
Sleep(INTERVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return ERROR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Obj = models::ServiceInfo<>,
|
||||||
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
|
static bool starting_service(Ptr service_info)
|
||||||
|
{
|
||||||
/*
|
/*
|
||||||
* Perform tasks necessary to start the service here
|
* Perform tasks necessary to start the service here
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Create a service stop event to wait on later
|
// Create a service stop event to wait on later
|
||||||
service_info->service_stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
service_info->service_stop_event = CreateEvent(nullptr, TRUE, FALSE, nullptr);
|
||||||
if (service_info->service_stop_event == NULL)
|
if (service_info->service_stop_event == nullptr)
|
||||||
{
|
{
|
||||||
// Error creating event
|
// Error creating event
|
||||||
// Tell service controller we are stopped and exit
|
// Tell service controller we are stopped and exit
|
||||||
@@ -84,29 +109,17 @@ namespace service
|
|||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Tell the service controller we are started
|
template<typename Obj = models::ServiceInfo<>,
|
||||||
service_info->service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
service_info->service_status.dwCurrentState = SERVICE_RUNNING;
|
static void close_handle(Ptr service_info)
|
||||||
service_info->service_status.dwWin32ExitCode = 0;
|
|
||||||
service_info->service_status.dwCheckPoint = 0;
|
|
||||||
|
|
||||||
if (SetServiceStatus(service_info->service_handle, &service_info->service_status) == FALSE)
|
|
||||||
{
|
{
|
||||||
OutputDebugString(_T(
|
|
||||||
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start a thread that will perform the main task of the service
|
|
||||||
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
|
|
||||||
|
|
||||||
// Wait until our worker thread exits signaling that the service needs to stop
|
|
||||||
WaitForSingleObject(hThread, INFINITE);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Perform any cleanup tasks
|
* Perform any cleanup tasks
|
||||||
*/
|
*/
|
||||||
@@ -126,104 +139,42 @@ namespace service
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Obj = models::ServiceInfo<>,
|
||||||
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
static DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
|
static void notify_service_controller_starting(Ptr service_info)
|
||||||
// static DWORD WINAPI ServiceWorkerThread(LPVOID lpParam,
|
|
||||||
// std::shared_ptr<models::ServiceInfo<>> service_info)
|
|
||||||
{
|
{
|
||||||
extern std::shared_ptr<models::ServiceInfo<>> service_info;
|
// Tell the service controller we are starting
|
||||||
|
ZeroMemory(&service_info->service_status, sizeof(service_info->service_status));
|
||||||
|
service_info->service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
||||||
|
service_info->service_status.dwControlsAccepted = 0;
|
||||||
|
service_info->service_status.dwCurrentState = SERVICE_START_PENDING;
|
||||||
|
service_info->service_status.dwWin32ExitCode = 0;
|
||||||
|
service_info->service_status.dwServiceSpecificExitCode = 0;
|
||||||
|
service_info->service_status.dwCheckPoint = 0;
|
||||||
|
|
||||||
// const auto some_file_path = "C:\\Users\\brahmix\\example_file.txt";
|
if (SetServiceStatus(service_info->service_handle, &service_info->service_status) == FALSE)
|
||||||
const auto some_file_path = "C:\\example_file.txt";
|
|
||||||
|
|
||||||
// while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
|
|
||||||
while (WaitForSingleObject(service_info->service_stop_event, 0) != WAIT_OBJECT_0)
|
|
||||||
{
|
{
|
||||||
std::fstream file(some_file_path, std::ios::out | std::ios::app);
|
OutputDebugString(_T(
|
||||||
|
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
||||||
const auto content = "woof";
|
|
||||||
|
|
||||||
file << content << "\n";
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
Sleep(3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return ERROR_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename TStr = LPTSTR, typename H = HANDLE,
|
|
||||||
typename CSTR = LPCTSTR,
|
|
||||||
typename Chr = TCHAR>
|
|
||||||
// VOID ServiceReportEvent(LPTSTR szFunction)
|
|
||||||
VOID ServiceReportEvent(TStr szFunction)
|
|
||||||
{
|
|
||||||
// HANDLE hEventSource;
|
|
||||||
H hEventSource;
|
|
||||||
// LPCTSTR lpszStrings[2];
|
|
||||||
CSTR lpszStrings[2];
|
|
||||||
// TCHAR Buffer[80];
|
|
||||||
CHR Buffer[80];
|
|
||||||
|
|
||||||
hEventSource = RegisterEventSource(NULL, this->service_info.service_name);
|
|
||||||
// hEventSource = RegisterEventSource(NULL, L(""));
|
|
||||||
|
|
||||||
if (NULL != hEventSource)
|
|
||||||
{
|
|
||||||
StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction, GetLastError());
|
|
||||||
|
|
||||||
lpszStrings[0] = SVCNAME;
|
|
||||||
lpszStrings[1] = Buffer;
|
|
||||||
|
|
||||||
ReportEvent(hEventSource, // event log handle
|
|
||||||
EVENTLOG_ERROR_TYPE, // event type
|
|
||||||
0, // event category
|
|
||||||
// SVC_ERROR,
|
|
||||||
SERVICE_ERROR_NORMAL, // event identifier
|
|
||||||
NULL, // no security identifier
|
|
||||||
2, // size of lpszStrings array
|
|
||||||
0, // no binary data
|
|
||||||
lpszStrings, // array of strings
|
|
||||||
NULL); // no binary data
|
|
||||||
|
|
||||||
DeregisterEventSource(hEventSource);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
models::ServiceInfo<> service_info;
|
|
||||||
|
|
||||||
void initialize()
|
template<typename Obj = models::ServiceInfo<>,
|
||||||
|
typename Ptr = std::shared_ptr<Obj>>
|
||||||
|
static void notify_service_controller_started(Ptr service_info)
|
||||||
{
|
{
|
||||||
// service_info.service_name = L" Server Example";
|
// Tell the service controller we are started
|
||||||
|
service_info->service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
||||||
|
service_info->service_status.dwCurrentState = SERVICE_RUNNING;
|
||||||
|
service_info->service_status.dwWin32ExitCode = 0;
|
||||||
|
service_info->service_status.dwCheckPoint = 0;
|
||||||
|
|
||||||
|
if (SetServiceStatus(service_info->service_handle, &service_info->service_status) == FALSE)
|
||||||
|
{
|
||||||
|
OutputDebugString(_T(
|
||||||
|
"My Sample Service: ServiceMain: SetServiceStatus returned error"));
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
class ServiceInfo;
|
|
||||||
|
|
||||||
|
|
||||||
service::ServiceEntryPoint::ServiceInfo service_info = {};
|
|
||||||
|
|
||||||
class ServiceInfo
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ServiceInfo()
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceInfo(std::wstring &&name) :
|
|
||||||
service_name(const_cast<wchar_t*>(name.c_str()))
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
wchar_t *service_name;
|
|
||||||
wchar_t *service_description;
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user