-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
154 lines (129 loc) · 4.11 KB
/
service.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "service.hpp"
#include "handles.hpp"
//#include "win32/event_log.hpp"
typedef w32::auto_handle< SC_HANDLE, &::CloseServiceHandle, NULL > service_handle_t;
//w32::registered_eventlog_holder g_service_eventlog( L"System" );
service_base::service_base( const TCHAR * service_name )
{
SERVICE_STATUS default_status = {
SERVICE_WIN32_OWN_PROCESS,
SERVICE_START,
SERVICE_ACCEPT_PARAMCHANGE | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_STOP,
NO_ERROR, 0, 0, 0
};
status_ = default_status;
_tcsncpy( name_, service_name, sizeof(name_) / sizeof(name_[0]) );
}
bool service_base::install( const TCHAR * command, const TCHAR * name, bool silent )
{
bool result = false;
{
service_handle_t manager = ::OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
service_handle_t srv = ::CreateService(manager.value(), name, name, 0,
SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
command, NULL, NULL, NULL, NULL, NULL);
result = (!!srv);
}
if ( !silent )
{
if (result)
{
::MessageBoxW(NULL, L"Ñëóæáà óñïåøíî óñòàíîâëåíà", L"Âíèìàíèå", MB_OK | MB_ICONINFORMATION);
}
else
{
switch ( ::GetLastError() )
{
case ERROR_SERVICE_EXISTS:
::MessageBoxW(NULL, L"Ñëóæáà óæå óñòàíîâëåíà", L"Îøèáêà!", MB_OK | MB_ICONERROR);
break;
case ERROR_DUPLICATE_SERVICE_NAME:
::MessageBoxW(NULL, L"Ñëóæáà ñ òàêèì èìåíåì óæå óñòàíîâëåíà", L"Îøèáêà!", MB_OK | MB_ICONERROR);
break;
default:
::MessageBoxW(NULL, L"Ó âàñ íåò ïðàâ äëÿ âûïîëíåíèÿ îïåðàöèè", L"Îøèáêà!", MB_OK | MB_ICONERROR);
break;
}
}
}
return result;
}
bool service_base::uninstall( const TCHAR * name, bool silent )
{
bool result = false;
{
service_handle_t manager = ::OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
service_handle_t srv = ::OpenService(manager.value(), name, DELETE);
if ( !!srv )
result = !!::DeleteService( srv.value() );
}
if ( !silent )
{
if (result)
{
::MessageBoxW(NULL, L"Ñëóæáà óñïåøíî óäàëåíà", L"Âíèìàíèå", MB_OK | MB_ICONINFORMATION);
} else
{
switch ( ::GetLastError() )
{
case ERROR_SERVICE_MARKED_FOR_DELETE:
::MessageBoxW(NULL,
L"Ñëóæáà óæå ïîìå÷åíà äëÿ óäàëåíèÿ. Ïåðåçàãðóçèòå êîìïüþòåð",
L"Âíèìàíèå", MB_OK | MB_ICONINFORMATION);
break;
default:
::MessageBoxW(NULL,
L"Ó âàñ íåò ïðàâ äëÿ âûïîëíåíèÿ îïåðàöèè",
L"Îøèáêà!", MB_OK | MB_ICONERROR);
}
}
}
return result;
}
DWORD service_base::start( LPSERVICE_MAIN_FUNCTION service_main_proc )
{
SERVICE_TABLE_ENTRY services[] = {
{ name_, service_main_proc },
{NULL, NULL}
};
return ::StartServiceCtrlDispatcher( services );
}
bool service_base::register_ctrl_handler( LPHANDLER_FUNCTION_EX f )
{
this->status_handle_ = ::RegisterServiceCtrlHandlerEx( this->name_, f, this );
return ( NULL != this->status_handle_ );
}
DWORD service_base::set_status( DWORD status, bool flush )
{
DWORD old = this->status_.dwCurrentState;
this->status_.dwCurrentState = status;
/*
switch( status )
{
case SERVICE_RUNNING: g_service_eventlog.report( L"service status set to SERVICE_RUNNING" ); break;
case SERVICE_START_PENDING: g_service_eventlog.report( L"service status set to SERVICE_START_PENDING" ); break;
case SERVICE_PAUSED: g_service_eventlog.report( L"service status set to SERVICE_PAUSED" ); break;
case SERVICE_STOPPED: g_service_eventlog.report( L"service status set to SERVICE_STOPPED" ); break;
case SERVICE_INTERROGATE: g_service_eventlog.report( L"service status set to SERVICE_INTERROGATE" ); break;
}
//*/
if ( flush )
{
if ( status == SERVICE_START_PENDING )
this->status_.dwControlsAccepted = 0;
else
this->status_.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
if ( (status == SERVICE_RUNNING) || (status == SERVICE_STOPPED) )
this->status_.dwCheckPoint = 0;
else
this->status_.dwCheckPoint++;
this->status_.dwWin32ExitCode = NO_ERROR;
this->status_.dwWaitHint = 0;
::SetServiceStatus(this->status_handle_, &this->status_ );
}
return old;
}
DWORD service_base::status() const
{
return this->status_.dwCurrentState;
}