环境安装
- 安装VS2019,下载地址:VS2019。安装时注意选中VC++桌面开发。
- 下载Windows11 SDK,此为
iso
文件,下载完成打开,双击WinSDKSetup.exe
进行安装,安装成功后按照提示重启。 - 下载Windows 11 WDK安装工具,下载完成后双击打开,默认下载安装,安装成功后会打开
WDK.vsix
文件。如果没有打开,在路径D:\Windows Kits\10\Vsix\VS2019
下可找到,双击安装。
创建驱动
参考微软的官方教程。
1 打开VS,然后搜索选择Kernel Mode Driver, Empty(KMDF),然后点击下一步。
2 新建项目KmdfHelloWorld
,然后点击创建。
3 选择解决方案窗口中的KmdHelloWord,右键->添加->新建项,然后在弹出窗口中新建Driver.c
。
4 在Driver.c
中写入驱动代码(此为微软提供的示例程序)
#include <ntddk.h>
#include <wdf.h>
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD KmdfHelloWorldEvtDeviceAdd;
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
{
// NTSTATUS variable to record success or failure
NTSTATUS status = STATUS_SUCCESS;
// Allocate the driver configuration object
WDF_DRIVER_CONFIG config;
// Print "Hello World" for DriverEntry
KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n" ));
// Initialize the driver configuration object to register the
// entry point for the EvtDeviceAdd callback, KmdfHelloWorldEvtDeviceAdd
WDF_DRIVER_CONFIG_INIT(&config,
KmdfHelloWorldEvtDeviceAdd
);
// Finally, create the driver object
status = WdfDriverCreate(DriverObject,
RegistryPath,
WDF_NO_OBJECT_ATTRIBUTES,
&config,
WDF_NO_HANDLE
);
return status;
}
NTSTATUS
KmdfHelloWorldEvtDeviceAdd(
_In_ WDFDRIVER Driver,
_Inout_ PWDFDEVICE_INIT DeviceInit
)
{
// We're not using the driver object,
// so we need to mark it as unreferenced
UNREFERENCED_PARAMETER(Driver);
NTSTATUS status;
// Allocate the device object
WDFDEVICE hDevice;
// Print "Hello World"
KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: KmdfHelloWorldEvtDeviceAdd\n" ));
// Create the device object
status = WdfDeviceCreate(&DeviceInit,
WDF_NO_OBJECT_ATTRIBUTES,
&hDevice
);
return status;
}
MSB401: SignTask错误解决
我在测试示例代码的过程中,出现了MSB401:SignTask任务意外失败
的错误。
双击错误,进入该错误对应的文件,其路径为D:\Windows Kits\10\build\WindowsDriver.Common.targets
,搜索</SignMode>
,将
<SignMode Condition="'$(SignMode)' == '' and '$(EnableTestSign)' == 'true'">Test Sign</SignMode>
中的Test Sign改为Off,即整行变为
<SignMode Condition="'$(SignMode)' == '' and '$(EnableTestSign)' == 'true'">Off</SignMode>
注1:网上搜到的解决方案均为:进入右键属性->Driver signing->General,然后更改SignMode从Test Sign为Off。但更改之后没有变化,所以在此直接手动更改其对应的文件。
注2:官方教程中要求更改Wpp跟踪,即右键属性->Wpp wrap>General:将 Run Wpp tracing”设置为“否”。但实际操作时发现这项本来就是否,所以就没写这步。
更改之后,再重新生成,就可在项目的debug文件夹\x64\Debug
下发现KmdfHelloWorld.sys
。