WDK Mini Filter Example
CdoInit.c
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) 1999 - 2003 Microsoft Corporation
4 
5 Module Name:
6 
7  CdoInit.c
8 
9 Abstract:
10 
11  This is the main module of the kernel mode filter driver implementing
12  the control device object sample.
13 
14 
15 Environment:
16 
17  Kernel mode
18 
19 
20 --*/
21 
22 #include "pch.h"
23 
24 
25 //
26 // Local function prototypes
27 //
28 
29 DRIVER_INITIALIZE DriverEntry;
30 NTSTATUS
32  _In_ PDRIVER_OBJECT DriverObject,
33  _In_ PUNICODE_STRING RegistryPath
34  );
35 
36 NTSTATUS
37 CdoUnload (
38  _In_ FLT_FILTER_UNLOAD_FLAGS Flags
39  );
40 
41 NTSTATUS
43  _In_ PCFLT_RELATED_OBJECTS FltObjects,
44  _In_ FLT_INSTANCE_SETUP_FLAGS Flags,
45  _In_ DEVICE_TYPE VolumeDeviceType,
46  _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
47  );
48 
49 #if DBG
50 
51 VOID
52 CdoInitializeDebugLevel (
53  _In_ PUNICODE_STRING RegistryPath
54  );
55 
56 #endif
57 
58 
59 //
60 // Global variables
61 //
62 
64 
65 
66 //
67 // Pragma defintiion table
68 //
69 
70 //
71 // Assign text sections for each routine.
72 //
73 
74 #ifdef ALLOC_PRAGMA
75 #pragma alloc_text(INIT, DriverEntry)
76 
77 #if DBG
78 #pragma alloc_text(INIT, CdoInitializeDebugLevel)
79 #endif
80 
81 #pragma alloc_text(PAGE, CdoUnload)
82 #endif
83 
84 
85 
86 NTSTATUS
88  _In_ PDRIVER_OBJECT DriverObject,
89  _In_ PUNICODE_STRING RegistryPath
90  )
91 {
92  NTSTATUS status;
93 
94 
95  //
96  // This defines what we want to filter with FltMgr
97  //
98 
99  CONST FLT_REGISTRATION filterRegistration = {
100  sizeof( FLT_REGISTRATION ), // Size
101  FLT_REGISTRATION_VERSION, // Version
102  0, // Flags
103  NULL, // Context
104  NULL, // Operation callbacks
105  CdoUnload, // MiniFilterUnload
106  CdoInstanceSetup, // InstanceSetup
107  NULL, // InstanceQueryTeardown
108  NULL, // InstanceTeardownStart
109  NULL, // InstanceTeardownComplete
110  NULL,NULL // NameProvider callbacks
111  };
112 
113 
114  RtlZeroMemory( &Globals, sizeof( Globals ) );
115 
116 #if DBG
117 
118  //
119  // Initialize global debug level
120  //
121 
122  CdoInitializeDebugLevel( RegistryPath );
123 
124 #else
125 
126  UNREFERENCED_PARAMETER( RegistryPath );
127 
128 #endif
129 
130  DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
131  ("[Cdo]: Driver being loaded\n") );
132 
133  //
134  // Initialize the resource
135  //
136 
137  ExInitializeResourceLite( &Globals.Resource );
138 
139  //
140  // Record the driver object
141  //
142 
143  Globals.FilterDriverObject = DriverObject;
144 
145  //
146  // Register with FltMgr to tell it our callback routines
147  //
148 
149  status = FltRegisterFilter( DriverObject,
150  &filterRegistration,
151  &Globals.Filter );
152 
153  if (!NT_SUCCESS( status )) {
154 
155  ExDeleteResourceLite( &Globals.Resource );
156  return status;
157  }
158 
159  //
160  // Now create our control device object
161  //
162 
163  status = CdoCreateControlDeviceObject( DriverObject );
164 
165  if (!NT_SUCCESS( status )) {
166 
167  FltUnregisterFilter( Globals.Filter );
168  ExDeleteResourceLite( &Globals.Resource );
169  return status;
170  }
171 
172  //
173  // Start filtering i/o
174  //
175 
176  status = FltStartFiltering( Globals.Filter );
177 
178  if (!NT_SUCCESS( status )) {
179 
181  FltUnregisterFilter( Globals.Filter );
182  ExDeleteResourceLite( &Globals.Resource );
183  return status;
184  }
185 
186  return status;
187 }
188 
189 #if DBG
190 
191 VOID
192 CdoInitializeDebugLevel (
193  _In_ PUNICODE_STRING RegistryPath
194  )
195 /*++
196 
197 Routine Description:
198 
199  This routine tries to read the filter DebugLevel parameter from
200  the registry. This value will be found in the registry location
201  indicated by the RegistryPath passed in.
202 
203 Arguments:
204 
205  RegistryPath - The path key passed to the driver during DriverEntry.
206 
207 Return Value:
208 
209  None.
210 
211 --*/
212 {
213  OBJECT_ATTRIBUTES attributes;
214  HANDLE driverRegKey;
215  NTSTATUS status;
216  ULONG resultLength;
217  UNICODE_STRING valueName;
218  UCHAR buffer[sizeof( KEY_VALUE_PARTIAL_INFORMATION ) + sizeof( LONG )];
219 
220  Globals.DebugLevel = DEBUG_TRACE_ERROR;
221 
222  //
223  // Open the desired registry key
224  //
225 
226  InitializeObjectAttributes( &attributes,
227  RegistryPath,
228  OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
229  NULL,
230  NULL );
231 
232  status = ZwOpenKey( &driverRegKey,
233  KEY_READ,
234  &attributes );
235 
236  if (NT_SUCCESS( status )) {
237 
238  //
239  // Read the DebugFlags value from the registry.
240  //
241 
242  RtlInitUnicodeString( &valueName, L"DebugLevel" );
243 
244  status = ZwQueryValueKey( driverRegKey,
245  &valueName,
247  buffer,
248  sizeof(buffer),
249  &resultLength );
250 
251  if (NT_SUCCESS( status )) {
252 
253  Globals.DebugLevel = *((PULONG) &(((PKEY_VALUE_PARTIAL_INFORMATION) buffer)->Data));
254  }
255  }
256 
257  //
258  // Close the registry entry
259  //
260 
261  ZwClose( driverRegKey );
262 }
263 
264 #endif
265 
266 
267 NTSTATUS
269  _In_ FLT_FILTER_UNLOAD_FLAGS Flags
270  )
271 /*++
272 
273 Routine Description:
274 
275  This is the unload routine for this filter driver. This is called
276  when the minifilter is about to be unloaded. We can fail this unload
277  request if this is not a mandatory unloaded indicated by the Flags
278  parameter.
279 
280 Arguments:
281 
282  Flags - Indicating if this is a mandatory unload.
283 
284 Return Value:
285 
286  Returns the final status of this operation.
287 
288 --*/
289 {
290 
291  PAGED_CODE();
292 
293  UNREFERENCED_PARAMETER( Flags );
294 
295  DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
296  ("[Cdo]: Unloading driver\n") );
297 
298  //
299  // If the CDO is still referenced and the unload is not mandatry
300  // then fail the unload
301  //
302 
303  CdoAcquireResourceShared( &Globals.Resource );
304 
305  if (FlagOn( Globals.Flags, GLOBAL_DATA_F_CDO_OPEN_REF) &&
306  !FlagOn(Flags,FLTFL_FILTER_UNLOAD_MANDATORY)) {
307 
308  DebugTrace( DEBUG_TRACE_LOAD_UNLOAD | DEBUG_TRACE_ERROR,
309  ("[Cdo]: Fail unloading driver since the unload is optional and the CDO is open\n") );
310  CdoReleaseResource( &Globals.Resource );
311  return STATUS_FLT_DO_NOT_DETACH;
312  }
313 
314 
315  //
316  // Cleanup and unload
317  //
318 
319  FltUnregisterFilter( Globals.Filter );
320  Globals.Filter = NULL;
322 
323 
324  CdoReleaseResource( &Globals.Resource );
325 
326  ExDeleteResourceLite( &Globals.Resource );
327 
328  return STATUS_SUCCESS;
329 }
330 
331 
332 //
333 // Instance setup routine
334 //
335 
336 NTSTATUS
338  _In_ PCFLT_RELATED_OBJECTS FltObjects,
339  _In_ FLT_INSTANCE_SETUP_FLAGS Flags,
340  _In_ DEVICE_TYPE VolumeDeviceType,
341  _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
342  )
343 /*++
344 
345 Routine Description:
346 
347  This routine is called whenever a new instance is created on a volume. This
348  gives us a chance to decide if we need to attach to this volume or not.
349 
350 Arguments:
351 
352  FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
353  opaque handles to this filter, instance and its associated volume.
354 
355  Flags - Flags describing the reason for this attach request.
356 
357 Return Value:
358 
359  STATUS_FLT_DO_NOT_ATTACH - do not attach because we do not want to
360  attach to any volume
361 
362 --*/
363 {
364 
365  UNREFERENCED_PARAMETER( FltObjects );
366  UNREFERENCED_PARAMETER( Flags );
367  UNREFERENCED_PARAMETER( VolumeDeviceType );
368  UNREFERENCED_PARAMETER( VolumeFilesystemType );
369 
370  return STATUS_FLT_DO_NOT_ATTACH;
371 }
372 
373 
374 
NTSTATUS CdoUnload(_In_ FLT_FILTER_UNLOAD_FLAGS Flags)
Definition: CdoInit.c:268
_In_ PCWSTR valueName
Definition: ncinit.c:8
DRIVER_INITIALIZE DriverEntry
Definition: CdoInit.c:29
NTSTATUS CdoInstanceSetup(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_SETUP_FLAGS Flags, _In_ DEVICE_TYPE VolumeDeviceType, _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType)
Definition: CdoInit.c:337
PFLT_FILTER Filter
Definition: CdoStruct.h:51
#define DebugTrace(Level, Data)
Definition: cancelSafe.c:36
NcLoadRegistryStringRetry KeyValuePartialInformation
Definition: ncinit.c:53
CDO_GLOBAL_DATA Globals
Definition: CdoInit.c:63
#define GLOBAL_DATA_F_CDO_OPEN_REF
Definition: CdoStruct.h:33
#define FlagOn(_F, _SF)
Definition: minispy.h:247
UNREFERENCED_PARAMETER(FileObject)
NcLoadRegistryStringRetry NULL
Definition: ncinit.c:53
ERESOURCE Resource
Definition: CdoStruct.h:75
VOID CdoDeleteControlDeviceObject(VOID)
PAGED_CODE()
PDRIVER_OBJECT FilterDriverObject
Definition: CdoStruct.h:57

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