1 // This file has been adapted to the Win32 version of Apcupsd
2 // by Kern E. Sibbald.  Many thanks to ATT and James Weatherall,
3 // the original author, for providing an excellent template.
4 //
5 // Rewrite/Refactoring by Adam Kropelin
6 //
7 // Copyright (2007) Adam D. Kropelin
8 // Copyright (2000) Kern E. Sibbald
9 //
10 
11 
12 // winservice.cpp
13 
14 // SERVICE-MODE CODE
15 
16 // This class provides access to service-oriented routines, under both
17 // Windows NT and Windows 95.  Some routines only operate under one
18 // OS, others operate under any OS.
19 
20 class upsService;
21 
22 #if (!defined(_win_upsSERVICE))
23 #define _win_upsSERVICE
24 
25 // The NT-specific code wrapper class
26 class upsService
27 {
28 public:
29    upsService();
30 
31    // INSTALL & START FUNCTIONS
32 
33    // Routine called by WinMain to cause Apcupsd to be installed
34    // as a service.
35    static int ApcupsdServiceMain();
36 
37    // Routine to install the Apcupsd service on the local machine
38    static int InstallService(bool quiet);
39 
40    // Routine to remove the Apcupsd service from the local machine
41    static int RemoveService(bool quiet);
42 
43    // Stop the service
44    static void ServiceStop();
45 
46 
47    // SERVICE OPERATION FUNCTIONS
48 
49    // SCM callbacks
50    static void WINAPI ServiceMain(DWORD argc, char **argv);
51    static void WINAPI ServiceCtrl(DWORD ctrlcode);
52 
53    // Thread on which service processing will take place
54    static DWORD WINAPI ServiceWorkThread(LPVOID lpwThreadParam);
55 
56 
57    // SUPPORT FUNCTIONS
58 
59    // Report status to the SCM
60    static BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
61 
62    // Set the service's description text
63    static void SetServiceDescription(SC_HANDLE hService, LPSTR lpDesc);
64 
65    // Set registry value to indicate if we're installed to run as a service
66    static void SetServiceFlag(DWORD flag);
67 
68    // Stop an NT service with the given handle
69    static BOOL StopNTService(SC_HANDLE hservice);
70 
71    // Open the Apcupsd NT service
72    static SC_HANDLE OpenNTService();
73 
74 
75    // INTERNAL DATA
76 
77    static SERVICE_STATUS         m_srvstatus;
78    static SERVICE_STATUS_HANDLE  m_hstatus;
79    static DWORD                  m_servicethread;
80 };
81 
82 #endif
83