2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: File system mini-filter support routines
5 * PROGRAMMER: Ged Murphy <gedmurphy@reactos.org>
11 #include <kmt_public.h>
16 // move to a shared location
17 typedef struct _KMTFLT_MESSAGE_HEADER
23 } KMTFLT_MESSAGE_HEADER
, *PKMTFLT_MESSAGE_HEADER
;
25 extern HANDLE KmtestHandle
;
26 static WCHAR TestServiceName
[MAX_PATH
];
30 * @name KmtFltLoadDriver
32 * Load the specified filter driver
33 * This routine will create the service entry if it doesn't already exist
36 * Name of the driver service (Kmtest- prefix will be added automatically)
37 * @param RestartIfRunning
38 * TRUE to stop and restart the service if it is already running
40 * TRUE to create a comms connection to the specified filter
42 * Handle to the filter's comms port
44 * @return Win32 error code
48 _In_z_ PCWSTR ServiceName
,
49 _In_ BOOLEAN RestartIfRunning
,
50 _In_ BOOLEAN ConnectComms
,
53 DWORD Error
= ERROR_SUCCESS
;
54 WCHAR ServicePath
[MAX_PATH
];
55 SC_HANDLE TestServiceHandle
;
57 StringCbCopy(ServicePath
, sizeof ServicePath
, ServiceName
);
58 StringCbCat(ServicePath
, sizeof ServicePath
, L
"_drv.sys");
60 StringCbCopy(TestServiceName
, sizeof TestServiceName
, L
"Kmtest-");
61 StringCbCat(TestServiceName
, sizeof TestServiceName
, ServiceName
);
63 Error
= KmtFltCreateAndStartService(TestServiceName
, ServicePath
, NULL
, &TestServiceHandle
, TRUE
);
65 if (Error
== ERROR_SUCCESS
&& ConnectComms
)
67 Error
= KmtFltConnect(ServiceName
, hPort
);
74 * @name KmtFltUnloadDriver
76 * Unload the specified filter driver
79 * Handle to the filter's comms port
81 * TRUE to disconnect the comms connection before unloading
83 * @return Win32 error code
88 _In_ BOOLEAN DisonnectComms
)
90 DWORD Error
= ERROR_SUCCESS
;
94 Error
= KmtFltDisconnect(hPort
);
102 Error
= KmtFltUnload(TestServiceName
);
115 * @name KmtFltRunKernelTest
117 * Run the specified filter test part
120 * Handle to the filter's comms port
122 * Name of the test to run
124 * @return Win32 error code
129 _In_z_ PCSTR TestName
)
131 return KmtFltSendStringToDriver(hPort
, KMTFLT_RUN_TEST
, TestName
);
135 * @name KmtFltSendToDriver
137 * Send an I/O control message with no arguments to the driver opened with KmtOpenDriver
140 * Handle to the filter's comms port
142 * The message to send to the filter
144 * @return Win32 error code as returned by DeviceIoControl
152 return KmtFltSendBufferToDriver(hPort
, Message
, NULL
, 0, NULL
, 0, NULL
);
156 * @name KmtFltSendStringToDriver
158 * Send an I/O control message with a string argument to the driver opened with KmtOpenDriver
162 * Handle to the filter's comms port
164 * The message associated with the string
166 * An ANSI string to send to the filter
168 * @return Win32 error code as returned by DeviceIoControl
171 KmtFltSendStringToDriver(
178 return KmtFltSendBufferToDriver(hPort
, Message
, (PVOID
)String
, (DWORD
)strlen(String
), NULL
, 0, NULL
);
182 * @name KmtFltSendWStringToDriver
184 * Send an I/O control message with a wide string argument to the driver opened with KmtOpenDriver
187 * Handle to the filter's comms port
189 * The message associated with the string
191 * An wide string to send to the filter
193 * @return Win32 error code as returned by DeviceIoControl
196 KmtFltSendWStringToDriver(
201 return KmtFltSendBufferToDriver(hPort
, Message
, (PVOID
)String
, (DWORD
)wcslen(String
) * sizeof(WCHAR
), NULL
, 0, NULL
);
205 * @name KmtFltSendUlongToDriver
207 * Send an I/O control message with an integer argument to the driver opened with KmtOpenDriver
210 * Handle to the filter's comms port
212 * The message associated with the value
214 * An 32bit valueng to send to the filter
216 * @return Win32 error code as returned by DeviceIoControl
219 KmtFltSendUlongToDriver(
224 return KmtFltSendBufferToDriver(hPort
, Message
, &Value
, sizeof(Value
), NULL
, 0, NULL
);
228 * @name KmtSendBufferToDriver
230 * Send an I/O control message with the specified arguments to the driver opened with KmtOpenDriver
233 * Handle to the filter's comms port
235 * The message associated with the value
237 * Pointer to a buffer to send to the filter
239 * Size of the buffer pointed to by InBuffer
241 * Pointer to a buffer to receive a response from the filter
242 * @param OutBufferSize
243 * Size of the buffer pointed to by OutBuffer
244 * @param BytesReturned
245 * Number of bytes written in the reply buffer
247 * @return Win32 error code as returned by DeviceIoControl
250 KmtFltSendBufferToDriver(
253 _In_reads_bytes_(BufferSize
) LPVOID InBuffer
,
254 _In_ DWORD BufferSize
,
255 _Out_writes_bytes_to_opt_(OutBufferSize
, *BytesReturned
) LPVOID OutBuffer
,
256 _In_ DWORD OutBufferSize
,
257 _Out_opt_ LPDWORD BytesReturned
)
259 PKMTFLT_MESSAGE_HEADER Ptr
;
260 KMTFLT_MESSAGE_HEADER Header
;
261 BOOLEAN FreeMemory
= FALSE
;
271 InBufferSize
= sizeof(KMTFLT_MESSAGE_HEADER
) + BufferSize
;
272 Ptr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, InBufferSize
);
275 return ERROR_NOT_ENOUGH_MEMORY
;
281 InBufferSize
= sizeof(KMTFLT_MESSAGE_HEADER
);
285 Ptr
->Message
= Message
;
288 Ptr
->Buffer
= (Ptr
+ 1);
289 StringCbCopy(Ptr
->Buffer
, BufferSize
, InBuffer
);
290 Ptr
->BufferSize
= BufferSize
;
293 Error
= KmtFltSendMessage(hPort
, Ptr
, InBufferSize
, OutBuffer
, OutBufferSize
, BytesReturned
);
297 HeapFree(GetProcessHeap(), 0, Ptr
);