-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.hpp
69 lines (52 loc) · 1.56 KB
/
service.hpp
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
/*
*
*/
#if defined _MSC_VER && (_MSC_VER >= 1000)
#pragma once
#endif
#if !defined HDR_AUTOGRAPH_SERVICE_HPP
#define HDR_AUTOGRAPH_SERVICE_HPP
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x500
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
class service_base
{
SERVICE_STATUS_HANDLE status_handle_;
SERVICE_STATUS status_;
TCHAR name_[256];
protected :
virtual ~service_base() {}
public:
static bool install( const TCHAR * command, const TCHAR * name, bool silent = false );
static bool uninstall( const TCHAR * name, bool silent = false );
service_base( const TCHAR * service_name );
DWORD start( LPSERVICE_MAIN_FUNCTION service_main_proc );
bool register_ctrl_handler( LPHANDLER_FUNCTION_EX f );
DWORD set_status( DWORD status, bool flush = true );
DWORD status() const;
};
template < typename T >
class service : public service_base
{
static DWORD __stdcall service_handler_proc(DWORD control, DWORD eventType, void * eventData, void * ctx)
{
service_base * ptr_this = (service_base *)ctx;
if ( !ptr_this )
return ERROR_INVALID_PARAMETER;
return static_cast< service<T> * >(ptr_this)->control_handler( control, eventType, eventData );
}
DWORD control_handler(DWORD control, DWORD eventType, void * eventData)
{
return static_cast<T*>(this)->handler( control, eventType, eventData );
}
public :
service( const TCHAR * service_name ) : service_base(service_name) {}
bool register_ctrl_handler()
{
return service_base::register_ctrl_handler( &service_handler_proc );
}
};
#endif /* HDR_AUTOGRAPH_SERVICE_HPP */