EaseFilter Demo Project
WindowsService.cpp
Go to the documentation of this file.
1 //
3 // (C) Copyright 2011 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 //
12 
13 #include "stdafx.h"
14 #include "Tools.h"
15 #include "FilterAPI.h"
16 #include "UnitTest.h"
17 #include "WindowsService.h"
18 #include "FilterWorker.h"
19 
20 #define SERVICE_NAME _T("EaseFilter Service")
21 
22 SERVICE_STATUS g_ServiceStatus = {0};
23 SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
24 HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
25 
26 //Purchase a license key with the link: http://www.EaseFilter.com/Order.htm
27 //Email us to request a trial key: info@EaseFilter.com //free email is not accepted.
28 #define registerKey "********************************"
29 
31 {
32  OutputDebugString(_T("EaseFilter Service: Main: Entry"));
33 
34  SERVICE_TABLE_ENTRY ServiceTable[] =
35  {
36  {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
37  {NULL, NULL}
38  };
39 
40  if (StartServiceCtrlDispatcher (ServiceTable) == FALSE)
41  {
42  OutputDebugString(_T("EaseFilter Service: Main: StartServiceCtrlDispatcher returned error"));
43  return GetLastError ();
44  }
45 
46  OutputDebugString(_T("EaseFilter Service: Main: Exit"));
47  return 0;
48 }
49 
50 
51 VOID WINAPI ServiceMain (DWORD argc, LPTSTR *argv)
52 {
53  DWORD Status = E_FAIL;
54 
55  OutputDebugString(_T("EaseFilter Service: ServiceMain: Entry"));
56 
57  g_StatusHandle = RegisterServiceCtrlHandler (SERVICE_NAME, ServiceCtrlHandler);
58 
59  if (g_StatusHandle == NULL)
60  {
61  OutputDebugString(_T("EaseFilter Service: ServiceMain: RegisterServiceCtrlHandler returned error"));
62  goto EXIT;
63  }
64 
65  // Tell the service controller we are starting
66  ZeroMemory (&g_ServiceStatus, sizeof (g_ServiceStatus));
67  g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
68  g_ServiceStatus.dwControlsAccepted = 0;
69  g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
70  g_ServiceStatus.dwWin32ExitCode = 0;
71  g_ServiceStatus.dwServiceSpecificExitCode = 0;
72  g_ServiceStatus.dwCheckPoint = 0;
73 
74  if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
75  {
76  OutputDebugString(_T("EaseFilter Service: ServiceMain: SetServiceStatus returned error"));
77  }
78 
79  /*
80  * Perform tasks neccesary to start the service here
81  */
82  OutputDebugString(_T("EaseFilter Service: ServiceMain: Performing Service Start Operations"));
83 
84  // Create stop event to wait on later.
85  g_ServiceStopEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
86  if (g_ServiceStopEvent == NULL)
87  {
88  OutputDebugString(_T("EaseFilter Service: ServiceMain: CreateEvent(g_ServiceStopEvent) returned error"));
89 
90  g_ServiceStatus.dwControlsAccepted = 0;
91  g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
92  g_ServiceStatus.dwWin32ExitCode = GetLastError();
93  g_ServiceStatus.dwCheckPoint = 1;
94 
95  if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
96  {
97  OutputDebugString(_T("EaseFilter Service: ServiceMain: SetServiceStatus returned error"));
98  }
99  goto EXIT;
100  }
101 
102  // Tell the service controller we are started
103  g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
104  g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
105  g_ServiceStatus.dwWin32ExitCode = 0;
106  g_ServiceStatus.dwCheckPoint = 0;
107 
108  if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
109  {
110  OutputDebugString(_T("EaseFilter Service: ServiceMain: SetServiceStatus returned error"));
111  }
112 
113  // Start the thread that will perform the main task of the service
114  HANDLE hThread = CreateThread (NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
115 
116  OutputDebugString(_T("EaseFilter Service: ServiceMain: Waiting for Worker Thread to complete"));
117 
118  // Wait until our worker thread exits effectively signaling that the service needs to stop
119  WaitForSingleObject (hThread, INFINITE);
120 
121  OutputDebugString(_T("EaseFilter Service: ServiceMain: Worker Thread Stop Event signaled"));
122 
123 
124  /*
125  * Perform any cleanup tasks
126  */
127  OutputDebugString(_T("EaseFilter Service: ServiceMain: Performing Cleanup Operations"));
128 
129  CloseHandle (g_ServiceStopEvent);
130 
131  g_ServiceStatus.dwControlsAccepted = 0;
132  g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
133  g_ServiceStatus.dwWin32ExitCode = 0;
134  g_ServiceStatus.dwCheckPoint = 3;
135 
136  if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
137  {
138  OutputDebugString(_T("EaseFilter Service: ServiceMain: SetServiceStatus returned error"));
139  }
140 
141  EXIT:
142  OutputDebugString(_T("EaseFilter Service: ServiceMain: Exit"));
143 
144  return;
145 }
146 
147 
148 VOID WINAPI ServiceCtrlHandler (DWORD CtrlCode)
149 {
150  OutputDebugString(_T("EaseFilter Service: ServiceCtrlHandler: Entry"));
151 
152  switch (CtrlCode)
153  {
154  case SERVICE_CONTROL_STOP :
155 
156  OutputDebugString(_T("EaseFilter Service: ServiceCtrlHandler: SERVICE_CONTROL_STOP Request"));
157 
158  if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
159  break;
160 
161  /*
162  * Perform tasks neccesary to stop the service here
163  */
164 
165  g_ServiceStatus.dwControlsAccepted = 0;
166  g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
167  g_ServiceStatus.dwWin32ExitCode = 0;
168  g_ServiceStatus.dwCheckPoint = 4;
169 
170  if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
171  {
172  OutputDebugString(_T("EaseFilter Service: ServiceCtrlHandler: SetServiceStatus returned error"));
173  }
174 
175  // This will signal the worker thread to start shutting down
176  SetEvent (g_ServiceStopEvent);
177 
178  break;
179 
180  default:
181  break;
182  }
183 
184  OutputDebugString(_T("EaseFilter Service: ServiceCtrlHandler: Exit"));
185 }
186 
187 
188 DWORD WINAPI ServiceWorkerThread (LPVOID lpParam)
189 {
190  OutputDebugString(_T("EaseFilter Service: ServiceWorkerThread: Entry"));
191 
192  // Periodically check if the service has been requested to stop
193  while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
194  {
195  BOOL ret = FALSE;
196  TCHAR* filterFolder = GetFilterMask();
197  ULONG ioRegistration = 0;
198  ULONG accessFlag = ALLOW_MAX_RIGHT_ACCESS;
199  DWORD threadCount = 5;
200  ULONG filterType = FILE_SYSTEM_MONITOR;
201 
202  //Register the I/O request,which will be monitored or will be called back from filter.
203  for (int i = 0; i < MAX_REQUEST_TYPE; i++ )
204  {
205  //register all post request
206  if( (double)i/2 != i/2 )
207  {
208  ioRegistration |= 1<<i;
209  }
210  }
211 
212  ret = SetRegistrationKey(registerKey);
213  if( !ret )
214  {
215  PrintLastErrorMessage( L"SetRegistrationKey failed.");
216  return 1;
217  }
218 
219  ret = RegisterMessageCallback(threadCount,MessageCallback,DisconnectCallback);
220 
221  if( !ret )
222  {
223  PrintLastErrorMessage( L"RegisterMessageCallback failed.");
224  return 1;
225  }
226 
227  //this the demo how to use the control filter.
228  SendConfigInfoToFilter(filterType,filterFolder,ioRegistration,accessFlag);
229 
230  }
231 
232  Disconnect();
233  OutputDebugString(_T("EaseFilter Service: ServiceWorkerThread: Exit"));
234 
235  return ERROR_SUCCESS;
236 }
VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
#define MAX_REQUEST_TYPE
Definition: FilterAPI.h:133
#define registerKey
HANDLE g_ServiceStopEvent
WCHAR * GetFilterMask()
Definition: FeatureDemo.cpp:56
#define SERVICE_NAME
int StartWindowsService()
VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
SERVICE_STATUS g_ServiceStatus
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
Proto_Message_Callback Proto_Disconnect_Callback DisconnectCallback
Definition: FilterAPI.h:508
void PrintLastErrorMessage(WCHAR *message)
Definition: Tools.cpp:49
Proto_Message_Callback MessageCallback
Definition: FilterAPI.h:508
void SendConfigInfoToFilter(ULONG FilterType, WCHAR *FilterFolder, ULONG IoRegistration, ULONG AccessFlag, UCHAR *encryptionKey, ULONG keyLength)
SERVICE_STATUS_HANDLE g_StatusHandle

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com