windows驱动开发8.HelloWorld驱动程序是如何一步步完整的
1.使用vs2022创建KMDF空项目
- 选择Kernel Mode Driver, Empty(KMDF)
- 源文件夹添加entry.c
- entry.c添加驱动入口函数
1 | int DriverEntry() |
- 编译成功,但是没什么用。
- 如果出现Inf2Cat错误,关闭Inf2Cat或者在Inf2Cat选项中使用本地时间。
- 如果出现spectre缓解错误,下载缓解库版本SKD或者在项目中关闭spectre缓解。
- 如果之后的驱动程序加载失败试着在项目属性->Driver Settings中设置Target Platform为Desktop。
2.使用标准入口函数
- 修改代码
1 |
|
解决报错
问题:
1
2
3Error C2220 the following warning is treated as an error
Warning C4100 'RegistryPath': unreferenced formal parameter
Warning C4100 'DriverObject': unreferenced formal parameter解决方法一:
项目属性->c / c++->常规->将警告视为错误, 选择否.
解决方法二:
修改代码在函数中使用参数
1
2
3
4
5
6
7
8
9
10
11
12
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
{
DriverObject;
RegistryPath;
return 0;
}解决方法三:
修改代码忽略警告
1
2
3
4
5
6
7
8
9
10
11
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
{
return 0;
}
3.添加卸载驱动代码
1 |
|
4.添加打印消息功能
修改entry.c代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NTSTATUS UnloadDriver(PDRIVER_OBJECT pDriverObject)
{
UNREFERENCED_PARAMETER(pDriverObject);
IoDeleteDevice(pDriverObject->DeviceObject);
return STATUS_SUCCESS;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath)
{
UNREFERENCED_PARAMETER(pRegistryPath);
pDriverObject->DriverUnload = UnloadDriver;
DbgPrintEx(0, 0, "Message!");
return STATUS_SUCCESS;
}编译生成驱动后, 将sys驱动找到并复制到虚拟机中。
使用
DriverMonitor.exe加载驱动程序。DebugView中选中Cpature Kernel, Enable Verbose Kernel Output, Capture Events。
DriverMonitor.exe中运行驱动程序。
DebugView中查看。
5.在打印信息中添加字符并在DebugView中筛选显示
- 修改entry.c代码
1 |
|
- 编译生成驱动后, 将sys驱动找到并复制到虚拟机中。
- 使用
DriverMonitor.exe加载驱动程序。 - DebugView中选中Cpature Kernel, Enable Verbose Kernel Output, Capture Events。
- DebugView中设置Filter / Hightlight->include内容为qi; abc。
- DriverMonitor.exe中运行驱动程序。
- DebugView中查看打印消息。
- 在Monitor中点击stop卸载。
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
