Windows驱动开发环境搭建测试HelloWorld
主要参考官方链接:
https://learn.microsoft.com/zh-cn/windows-hardware/drivers/gettingstarted/writing-a-very-small-kmdf--driver
其中注意修改点:
- 双虚拟机安装好windows10系统(MSDN我告诉你下载)。
- vmware 虚拟机设置。
- 目标计算机设置调试模式(设置完成之后重启计算机)。
- 需要自己下载dbgview64.exe。DebugView - Windows Sysinternals
- 需要修改目标计算机调试信息打印注册表。
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Debug Print Filter]
DEFAULT=dword:0000000f
- 微软示例驱动程序打印函数KdPrintEx 修改为 KdPrint。
- 编译生成拷贝到目标计算机,打开debugview(对应平台),使用devcon直接安装驱动可以看到打印调试信息。
调试驱动安装和打印信息:
刚开始学习windows驱动开发,官方文档不友好,自己琢磨,记录得比较乱,有需要交流可以留言。
1 #include <ntddk.h> 2 #include <wdf.h> 3 4 5 DRIVER_INITIALIZE DriverEntry; 6 EVT_WDF_DRIVER_DEVICE_ADD KmdfHelloWorldEvtDeviceAdd; 7 DRIVER_UNLOAD DriverUnload; 8 9 NTSTATUS 10 DriverEntry( 11 _In_ PDRIVER_OBJECT DriverObject, 12 _In_ PUNICODE_STRING RegistryPath 13 ) 14 { 15 // NTSTATUS variable to record success or failure 16 NTSTATUS status = STATUS_SUCCESS; 17 18 // Allocate the driver configuration object 19 WDF_DRIVER_CONFIG config; 20 21 // Print "Hello World" for DriverEntry 22 //KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_WARNING_LEVEL, "KmdfHelloWorld: DriverEntry\n")); 23 KdPrint(("DriverEntry CxlHello....\r\n")); 24 //DbgPrint("Hello World ccll\r\n"); 25 26 // Initialize the driver configuration object to register the 27 // entry point for the EvtDeviceAdd callback, KmdfHelloWorldEvtDeviceAdd 28 WDF_DRIVER_CONFIG_INIT(&config, 29 KmdfHelloWorldEvtDeviceAdd 30 ); 31 32 // Finally, create the driver object 33 status = WdfDriverCreate(DriverObject, 34 RegistryPath, 35 WDF_NO_OBJECT_ATTRIBUTES, 36 &config, 37 WDF_NO_HANDLE 38 ); 39 return status; 40 } 41 42 NTSTATUS 43 KmdfHelloWorldEvtDeviceAdd( 44 _In_ WDFDRIVER Driver, 45 _Inout_ PWDFDEVICE_INIT DeviceInit 46 ) 47 { 48 // We're not using the driver object, 49 // so we need to mark it as unreferenced 50 UNREFERENCED_PARAMETER(Driver); 51 52 NTSTATUS status; 53 54 // Allocate the device object 55 WDFDEVICE hDevice; 56 57 // Print "Hello World" 58 //KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_WARNING_LEVEL, "KmdfHelloWorld: KmdfHelloWorldEvtDeviceAdd\n")); 59 KdPrint(("Driver Config Init CxlHello....\r\n")); 60 //DbgPrint("Hello World ccxx\r\n"); 61 62 // Create the device object 63 status = WdfDeviceCreate(&DeviceInit, 64 WDF_NO_OBJECT_ATTRIBUTES, 65 &hDevice 66 ); 67 return status; 68 }
文档主要是写到飞书,更新有时候不同步,可自行查看: