首页 > 代码库 > Keyboard Input WDF Filter Driver (Kbfiltr)源代码学习笔记
Keyboard Input WDF Filter Driver (Kbfiltr)源代码学习笔记
WDF版本键盘过滤驱动——Kbfiltr
The Kbdfltr sample is an example of a keyboard input filter driver.
This sample is WDF version of the original WDM filter driver sample. The WDM version of this sample has been deprecated.
This is an upper device filter driver sample for PS/2 keyboard. This driver layers in between the KbdClass driver and i8042prt driver and hooks the callback routine that moves keyboard inputs from the port driver to class driver. In its current state, it only hooks into the keyboard packet report chain, the keyboard initialization function, and the keyboard ISR, but does not do any processing of the data that it sees. (The hooking of the initialization function and ISR is only available in the i8042prt stack.) With additions to this current filter-only code base, the filter could conceivably add, remove, or modify input as needed.
This sample also creates a raw PDO and registers an interface so that applications can talk to the filter driver directly without going through the PS/2 devicestack. The reason for providing this additional interface is because the keyboard device is an exclusive secure device and it‘s not possible to open the device from usermode and send custom ioctls through it.
This driver filters input for a particular keyboard on the system. If you want to filter keyboard inputs from all the keyboards plugged into the system, you can install this driver as a class filter below the KbdClass filter driver by adding the service name of this filter driver before the KbdClass filter in the registry at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96B-E325-11CE-BFC1-08002BE10318}\UpperFilters
Note
To build this sample, you can use Microsoft Visual Studio 2013 (Express, Professional, or Ultimate) and Windows Driver Kit (WDK) 8.1 Update. You can get Visual Studio 2013 and WDK 8.1 Update here.
You can also build this sample with Visual Studio 2013 (Professional or Ultimate) and Windows Driver Kit (WDK) 8.1.
For Windows Driver Kit (WDK) 8 samples, download the WDK 8 samples pack. The samples in the WDK 8 samples pack will build only with Microsoft Visual Studio Professional 2012 (Professional or Ultimate) and WDK 8.
Operating system requirements
Client | Windows 8 |
---|---|
Server | Windows Server 2012 |
Build the sample
Download and extract the sample
Click the download button on this page. Click Save, and then click Open Folder. Right click Keyboard Input WDF Filter Driver (Kbfiltr).zip, and choose Extract All. Specify or browse to a folder for the extracted files. For example, you could extract to c:\Keyboard Input WDF Filter Driver (Kbfiltr).
Open the driver solution in Visual Studio
Navigate to the folder that has the extracted sample. Double click the solution file, kbfiltr.sln. In Microsoft Visual Studio, locate Solution Explorer. (If this is not already open, choose Solution Explorer from the View menu.) In Solution Explorer, you can see one solution that has two projects. There is a driver project named kbfiltr and a package project named package (lower case).
Set the configuration and platform in Visual Studio
In Visual Studio, in Solution Explorer, right click Solution ‘kbfiltr’ (3 projects), and choose Configuration Manager. Set the configuration and the platform. Make sure that the configuration and platform are the same for both the driver project and the package project. Do not check the Deploy boxes. Here are some examples of configuration and platform settings.
Configuration | Platform | Description |
---|---|---|
Win8.1 Debug | x64 | The driver will run on an x64 hardware platform that is running Windows 8.1. The driver will not run on any earlier versions of Windows. |
Win7 Debug | x64 | The driver will run on an x64 hardware platform that is running Windows 7 or a later version of Windows. |
Set the hardware ID in the inx file
This step is required for automatic deployment (described later) to work properly. In the kbfiltr.inx file (located with the driver source files), find the [DDK_Ex.Mfg.NT$ARCH$] section. Change the hardware ID in the %DDK_Ex% entry from the dummy value to the hardware ID of the PS/2 keyboard on the target computer. The following example shows the hardware ID change.
; For XP and above[DDK_Ex.Mfg.NT$ARCH$];%DDK_Ex% = kbfiltr, *PNP0BAAD%DDK_Ex% = kbfiltr, ACPI\VEN_PNP&DEV_0303
Build the sample using Visual Studio
In Visual Studio, on the Build menu, choose Build Solution.
For more information about using Visual Studio to build a driver package, see Building a Driver.
The test application, kbftest.exe is also built as part of the solution under the ‘exe‘ folder.
Locate the built driver package
In File Explorer, navigate to the folder that contains your built driver package. The location of this folder varies depending on what you set for configuration and platform. For example, if your settings are Win7 Debug and x64, the package is your solution folder under x64\Win7Debug\Package.
The package contains these files:
File | Description |
---|---|
Kmdfsamples.cat | A signed catalog file, which serves as the signature for the entire package. |
kbfiltr.inf | An information (INF) file that contains information needed to install the driver. |
WdfCoinstaller010xx.dll | The coinstaller for version 1.xx of KMDF. |
kbfiltr.sys | The driver file. |
Using MSBuild
As an alternative to building the Kbfiltr Filter Driver sample in Visual Studio, you can build it in a Visual Studio Command Prompt window. In Visual Studio, on the Tools menu, choose Visual Studio Command Prompt. In the Visual Studio Command Prompt window, navigate to the folder that has the solution file, kbfiltr.sln. Use the MSBuild command to build the solution. Here are some examples:
msbuild /p:configuration=”Win7 Debug” /p:platform=”x64” kbfiltr.sln
msbuild /p:configuration=”Win8 Release” /p:platform=”win32” kbfiltr.sln
For more information about using MSBuild to build a driver package, see Building a Driver.
Run the sample
The computer where you install the driver is called the target computer or the test computer. Typically this is a separate computer from where you develop and build the driver package. The computer where you develop and build the driver is called the host computer.
The process of moving the driver package to the target computer and installing the driver is called deploying the driver. You can deploy kbfiltr sample driver automatically or manually.
Automatic deployment
Before you automatically deploy a driver, you must provision the target computer. For instructions, see Configuring a Computer for Driver Deployment, Testing, and Debugging.
- On the host computer, in Visual Studio, in Solution Explorer, right click package (lower case), and choose Properties. Navigate to Configuration Properties > Driver Install > Deployment.
- Check Enable deployment, and check Remove previous driver versions before deployment. For Target Computer Name, select the name of a target computer that you provisioned previously. Select Install and Verify, and choose Default Driver Package Installation Task in the list. Click OK.
- On the Build menu, choose Deploy Package or Build Solution.
Manual deployment
Before you manually deploy a driver, you must turn on test signing and install a certificate on the target computer. You also need to copy the DevCon tool to the target computer. For instructions, see Preparing a Computer for Manual Driver Deployment.
- Copy all of the files in your driver package to a folder on the target computer (for example, c:\KbfiltrDriverPackage).
On the target computer, open a Command Prompt window as Administrator. Navigate to your driver package folder, and enter the Devcon command with the correct hardware ID, such as:
Devcon install kbfiltr.inf ACPI\VEN_PNP&DEV_0303
-or-
Using Device Manager, update the driver for the PS/2 Keyboard by manually selecting kbfiltr.inf from the location where you copied the driver files.
View the installed driver in Device Manager
On the target computer, in a Command Prompt window, enter devmgmt to open Device Manager. In Device Manager, on the View menu, choose Devices by type. In the device tree, locate DDK Example Device that needs filtering under the Keyboards node.
Testing
To use the test application provided with the sample, it must be copied to the target computer manually. Save the kbftest.exe file from the folder where the build result is placed (for example, exe\x64\Win7Debug). This file is copied somewhere on the target, possibly where the driver package files are located. The test application is the executed on the target computer in a Command Prompt using kbftest as the command.
Tip To avoid DLL dependencies for kbftext.exe, and the need to copy additional files, select the statically linked run-time library when building.
Keyboard Input WDF Filter Driver (Kbfiltr)源代码学习笔记