2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: File system filter implementation of the original service.c file
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 * Ged Murphy <gedmurphy@reactos.org>
15 #define SERVICE_ACCESS (SERVICE_START | SERVICE_STOP | DELETE)
18 * We need to call the internal function in the service.c file
22 IN PCWSTR ServiceName
,
23 IN PCWSTR ServicePath
,
24 IN PCWSTR DisplayName OPTIONAL
,
26 OUT SC_HANDLE
*ServiceHandle
);
28 static SC_HANDLE ScmHandle
;
32 * @name KmtFltCreateService
34 * Create the specified driver service and return a handle to it
37 * Name of the service to create
39 * File name of the driver, relative to the current directory
41 * Service display name
42 * @param ServiceHandle
43 * Pointer to a variable to receive the handle to the service
45 * @return Win32 error code
49 _In_z_ PCWSTR ServiceName
,
50 _In_z_ PCWSTR ServicePath
,
51 _In_z_ PCWSTR DisplayName OPTIONAL
,
52 _Out_ SC_HANDLE
*ServiceHandle
)
54 return KmtpCreateService(ServiceName
,
57 SERVICE_FILE_SYSTEM_DRIVER
,
64 * Start the specified filter driver by name
67 * The name of the filter to start
69 * @return Win32 error code
73 _In_z_ PCWSTR ServiceName
)
76 DWORD Error
= ERROR_SUCCESS
;
80 hResult
= FilterLoad(ServiceName
);
81 Error
= SCODE_CODE(hResult
);
87 * @name KmtFltCreateAndStartService
89 * Create and load the specified filter driver and return a handle to it
92 * Name of the service to create
94 * File name of the driver, relative to the current directory
96 * Service display name
97 * @param ServiceHandle
98 * Pointer to a variable to receive the handle to the service
99 * @param RestartIfRunning
100 * TRUE to stop and restart the service if it is already running
102 * @return Win32 error code
105 KmtFltCreateAndStartService(
106 _In_z_ PCWSTR ServiceName
,
107 _In_z_ PCWSTR ServicePath
,
108 _In_z_ PCWSTR DisplayName OPTIONAL
,
109 _Out_ SC_HANDLE
*ServiceHandle
,
110 _In_ BOOLEAN RestartIfRunning
)
112 DWORD Error
= ERROR_SUCCESS
;
114 assert(ServiceHandle
);
116 Error
= KmtFltCreateService(ServiceName
, ServicePath
, DisplayName
, ServiceHandle
);
118 if (Error
== ERROR_SERVICE_EXISTS
)
119 *ServiceHandle
= OpenService(ScmHandle
, ServiceName
, SERVICE_ACCESS
);
121 if (Error
&& Error
!= ERROR_SERVICE_EXISTS
)
124 Error
= KmtFltLoad(ServiceName
);
126 if (Error
!= ERROR_SERVICE_ALREADY_RUNNING
)
129 Error
= ERROR_SUCCESS
;
131 if (!RestartIfRunning
)
134 Error
= KmtFltUnload(ServiceName
);
138 Error
= KmtFltLoad(ServiceName
);
149 * @name KmtFltConnect
151 * Create a comms connection to the specified filter
154 * Name of the filter to connect to
156 * Handle to the filter's comms port
158 * @return Win32 error code
162 _In_z_ PCWSTR ServiceName
,
166 DWORD Error
= ERROR_SUCCESS
;
171 hResult
= FilterConnectCommunicationPort(ServiceName
,
177 Error
= SCODE_CODE(hResult
);
183 * @name KmtFltDisconnect
185 * Disconenct from the comms port
188 * Handle to the filter's comms port
190 * @return Win32 error code
196 DWORD Error
= ERROR_SUCCESS
;
200 if (!CloseHandle(hPort
))
202 Error
= GetLastError();
209 * @name KmtFltSendMessage
211 * Sneds a message to a filter driver
214 * Handle to the filter's comms port
216 * Pointer to a buffer to send to the filter
218 * Size of the buffer pointed to by InBuffer
220 * Pointer to a buffer to receive reply data from the filter
222 * Size of the buffer pointed to by OutBuffer
224 * Number of bytes written in the reply buffer
226 * @return Win32 error code
231 _In_reads_bytes_(dwInBufferSize
) LPVOID InBuffer
,
232 _In_ DWORD InBufferSize
,
233 _Out_writes_bytes_to_opt_(dutBufferSize
, *BytesReturned
) LPVOID OutBuffer
,
234 _In_ DWORD OutBufferSize
,
235 _Out_opt_ LPDWORD BytesReturned
)
243 assert(InBufferSize
);
245 if (BytesReturned
) *BytesReturned
= 0;
247 hResult
= FilterSendMessage(hPort
,
254 Error
= SCODE_CODE(hResult
);
255 if (Error
== ERROR_SUCCESS
)
259 *BytesReturned
= BytesRet
;
267 * @name KmtFltGetMessage
269 * Gets a message from a filter driver
272 * Handle to the filter's comms port
274 * Pointer to a buffer to receive the data from the filter
276 * Size of the buffer pointed to by MessageBuffer
278 * Pointer to an overlapped structure
280 * @return Win32 error code
285 _Out_writes_bytes_(MessageBufferSize
) PFILTER_MESSAGE_HEADER MessageBuffer
,
286 _In_ DWORD MessageBufferSize
,
287 _In_opt_ LPOVERLAPPED Overlapped
)
293 assert(MessageBuffer
);
295 hResult
= FilterGetMessage(hPort
,
299 Error
= SCODE_CODE(hResult
);
304 * @name KmtFltReplyMessage
306 * Replies to a message from a filter driver
309 * Handle to the filter's comms port
311 * Pointer to a buffer to return to the filter
313 * Size of the buffer pointed to by ReplyBuffer
315 * @return Win32 error code
320 _In_reads_bytes_(ReplyBufferSize
) PFILTER_REPLY_HEADER ReplyBuffer
,
321 _In_ DWORD ReplyBufferSize
)
326 hResult
= FilterReplyMessage(hPort
,
329 Error
= SCODE_CODE(hResult
);
334 * @name KmtFltGetMessageResult
336 * Gets the overlapped result from the IO
339 * Handle to the filter's comms port
341 * Pointer to the overlapped structure usdd in the IO
343 * Number of bytes transferred in the IO
345 * @return Win32 error code
348 KmtFltGetMessageResult(
350 _In_ LPOVERLAPPED Overlapped
,
351 _Out_ LPDWORD BytesTransferred
)
354 DWORD Error
= ERROR_SUCCESS
;
356 *BytesTransferred
= 0;
358 Success
= GetOverlappedResult(hPort
, Overlapped
, BytesTransferred
, TRUE
);
361 Error
= GetLastError();
370 * Unload the specified filter driver
373 * The name of the filter to unload
375 * @return Win32 error code
379 _In_z_ PCWSTR ServiceName
)
382 DWORD Error
= ERROR_SUCCESS
;
386 hResult
= FilterUnload(ServiceName
);
387 Error
= SCODE_CODE(hResult
);
393 * @name KmtFltDeleteService
395 * Delete the specified filter driver
398 * If *ServiceHandle is NULL, name of the service to delete
399 * @param ServiceHandle
400 * Pointer to a variable containing the service handle.
401 * Will be set to NULL on success
403 * @return Win32 error code
407 _In_z_ PCWSTR ServiceName OPTIONAL
,
408 _Inout_ SC_HANDLE
*ServiceHandle
)
410 return KmtDeleteService(ServiceName
, ServiceHandle
);
414 * @name KmtFltCloseService
416 * Close the specified driver service handle
418 * @param ServiceHandle
419 * Pointer to a variable containing the service handle.
420 * Will be set to NULL on success
422 * @return Win32 error code
424 DWORD
KmtFltCloseService(
425 _Inout_ SC_HANDLE
*ServiceHandle
)
427 return KmtCloseService(ServiceHandle
);