2 * PROJECT: Filesystem Filter Manager library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/fltlib/fltlib.c
6 * PROGRAMMERS: Ged Murphy (ged.murphy@reactos.org)
11 #define WIN32_NO_STATUS
16 #define NTOS_MODE_USER
17 #include <ndk/iofuncs.h>
18 #include <ndk/obfuncs.h>
19 #include <ndk/rtlfuncs.h>
20 #include <fltmgr_shared.h>
22 /* DATA ****************************************************************************/
26 FilterLoadUnload(_In_z_ LPCWSTR lpFilterName
,
31 SendIoctl(_In_ HANDLE Handle
,
32 _In_ ULONG IoControlCode
,
33 _In_reads_bytes_opt_(BufferSize
) LPVOID lpInBuffer
,
34 _In_ DWORD BufferSize
);
37 /* PUBLIC FUNCTIONS ****************************************************************/
42 FilterLoad(_In_ LPCWSTR lpFilterName
)
44 return FilterLoadUnload(lpFilterName
, TRUE
);
50 FilterUnload(_In_ LPCWSTR lpFilterName
)
52 return FilterLoadUnload(lpFilterName
, FALSE
);
56 /* PRIVATE FUNCTIONS ****************************************************************/
59 NtStatusToHResult(_In_ NTSTATUS Status
)
62 hr
= RtlNtStatusToDosError(Status
);
63 if (hr
!= ERROR_SUCCESS
)
65 hr
= (ULONG
)hr
| 0x80070000;
72 FilterLoadUnload(_In_z_ LPCWSTR lpFilterName
,
75 PFILTER_NAME FilterName
;
81 /* Get a handle to the filter manager */
82 hFltMgr
= CreateFileW(L
"\\\\.\\fltmgr",
87 FILE_ATTRIBUTE_NORMAL
,
89 if (hFltMgr
== INVALID_HANDLE_VALUE
)
91 dwError
= GetLastError();
92 return HRESULT_FROM_WIN32(dwError
);
95 /* Calc and allocate a buffer to hold our filter name */
96 StringLength
= wcslen(lpFilterName
) * sizeof(WCHAR
);
97 BufferLength
= StringLength
+ sizeof(FILTER_NAME
);
98 FilterName
= RtlAllocateHeap(GetProcessHeap(),
101 if (FilterName
== NULL
)
103 CloseHandle(hFltMgr
);
104 return HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY
);
107 /* Build up the filter name */
108 FilterName
->Length
= StringLength
;
109 CopyMemory(FilterName
->FilterName
, lpFilterName
, StringLength
);
111 /* Tell the filter manager to load the filter for us */
112 dwError
= SendIoctl(hFltMgr
,
113 Load
? IOCTL_FILTER_LOAD
: IOCTL_FILTER_UNLOAD
,
117 /* Cleanup and bail*/
118 RtlFreeHeap(GetProcessHeap(), 0, FilterName
);
119 CloseHandle(hFltMgr
);
121 return HRESULT_FROM_WIN32(dwError
);
126 SendIoctl(_In_ HANDLE Handle
,
127 _In_ ULONG IoControlCode
,
128 _In_reads_bytes_opt_(BufferSize
) LPVOID lpInBuffer
,
129 _In_ DWORD BufferSize
)
131 IO_STATUS_BLOCK IoStatusBlock
;
134 Status
= NtDeviceIoControlFile(Handle
,
144 if (Status
== STATUS_PENDING
)
146 Status
= NtWaitForSingleObject(Handle
, FALSE
, NULL
);
147 if (NT_SUCCESS(Status
))
149 Status
= IoStatusBlock
.Status
;
153 return RtlNtStatusToDosError(Status
);
158 DllMain(_In_ HINSTANCE hinstDLL
,
160 _In_ LPVOID lpvReserved
)
164 case DLL_PROCESS_ATTACH
:
165 DisableThreadLibraryCalls(hinstDLL
);