summaryrefslogtreecommitdiff
path: root/include/net-snmp/library/winservice.h
blob: 76080d69fa585552c176773863bee055c7d2b631 (plain)
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
154
155
156
157
158
159
#ifndef WINSERVICE_H
#define WINSERVICE_H

    /*
     * 
     * Windows Service related functions declaration
     * By Raju Krishanppa(raju_krishnappa@yahoo.com)
     *
     */

#ifdef __cplusplus
extern "C"
{

#endif				/*  */

  /*
   * Define Constants for Register, De-register , Run As service or Console mode
   */
#define REGISTER_SERVICE 0
#define UN_REGISTER_SERVICE 1
#define RUN_AS_SERVICE 2
#define RUN_AS_CONSOLE 3


  /*
   * Error levels returned when registering or unregistering the service
  */
#define SERVICE_ERROR_NONE 0            
#define SERVICE_ERROR_SCM_OPEN 1                /* Can not open SCM */
#define SERVICE_ERROR_CREATE_SERVICE 2          /* Can not create service */
#define SERVICE_ERROR_CREATE_REGISTRY_ENTRIES 3 /* Can not create registry entries */
#define SERVICE_ERROR_OPEN_SERVICE 4            /* Can not open service (service does not exist) */
 
  /*
   * Define Message catalog ID
   * MessageId: DISPLAY_MSG
   * MessageText:  %1.
   */
#define DISPLAY_MSG                      0x00000064L

  /*
   * Hint Value to SCM to wait before sending successive commands to service
   */
#define SCM_WAIT_INTERVAL 7000

  /*
   * Define Generic String Size, to hold Error or Information
   */
#define MAX_STR_SIZE  1024

  /*
   * Delcare Global variables, which are visible to other modules 
   */
  extern BOOL g_fRunningAsService;

  /*
   * Input parameter structure to thread 
   */
  typedef struct _InputParams
  {
    DWORD Argc;
    LPTSTR *Argv;
  } InputParams;

  /*
   * Define Service Related functions
   */

  /*
   * To register application as windows service with SCM 
   */
  int RegisterService (LPCTSTR lpszServiceName,
			LPCTSTR lpszServiceDisplayName,
			LPCTSTR lpszServiceDescription, InputParams * StartUpArg, int quiet);

  /*
   * To unregister service 
   */
  int UnregisterService (LPCTSTR lpszServiceName, int quiet);

  /*
   * To parse command line for startup option 
   */
  INT ParseCmdLineForServiceOption (INT argc, TCHAR * argv[], int *quiet);

  /*
   * To write to windows event log 
   */
  VOID WriteToEventLog (WORD wType, LPCTSTR pszFormat, ...);

  /*
   * To display generic windows error 
   */
  VOID DisplayError (LPCTSTR pszTitle, int quite);

  /*
   * Service Main function,  Which will spawn a thread, and calls the
   * Service run part
   */
  VOID WINAPI ServiceMain (DWORD argc, LPTSTR argv[]);

  /*
   * To start Service 
   */

  BOOL RunAsService (INT (*ServiceFunction) (INT, LPTSTR *));

  /*
   * Call back function to process SCM Requests 
   */
  VOID WINAPI ControlHandler (DWORD dwControl);

  /*
   * To Stop the service 
   */
  VOID ProcessServiceStop (VOID);

  /*
   * To Pause service 
   */
  VOID ProcessServicePause (VOID);

  /*
   * To Continue paused service 
   */
  VOID ProcessServiceContinue (VOID);

  /*
   * To send Current Service status to SCM when INTERROGATE command is sent 
   */
  VOID ProcessServiceInterrogate (VOID);

  /*
   * To allocate and Set security descriptor 
   */
  BOOL SetSimpleSecurityAttributes (SECURITY_ATTRIBUTES * pSecurityAttr);

  /*
   * To free Security Descriptor 
   */
  VOID FreeSecurityAttributes (SECURITY_ATTRIBUTES * pSecurityAttr);

  /*
   * TheadFunction - To spawan as thread - Invokes registered service function 
   */
  unsigned WINAPI ThreadFunction (LPVOID lpParam);

  /*
   * Service STOP function registration with this framewrok
   * * this function must be invoked before calling RunAsService
   */
  VOID RegisterStopFunction (VOID (*StopFunc) (VOID));

#ifdef __cplusplus
}
#endif				/*  */
#endif				/* WINSERVICE_H */