如何启动一个自写的驱动程序

我在Visual Studio 2013中编写了一个驱动程序。构build过程是成功的。 然后,我准备了一个电脑计算机,并将驱动程序文件复制到它。 然后我安装了驱动程序:

C:\Windows\system32>pnputil -a "E:\driverZeug\KmdfHelloWorldPackage\KmdfHelloWorld.inf" Microsoft-PnP-Dienstprogramm Verarbeitungsinf.: KmdfHelloWorld.inf Das Treiberpaket wurde erfolgreich hinzugefügt. Veröffentlichter Name: oem42.inf Versuche gesamt: 1 Anzahl erfolgreicher Importe: 1 

这似乎是成功的。 我在PC上运行了DebugView,但现在我不知道如何启动驱动程序,以便看到一个debugging输出。 我有一个DbgPrintEx() – 在我的源代码中的声明。

有人能告诉我如何启动这个驱动程序,以便我可以看到输出。

这是驱动程序的源代码:

 #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 status; WDF_DRIVER_CONFIG config; DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n"); KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n")); WDF_DRIVER_CONFIG_INIT(&config, KmdfHelloWorldEvtDeviceAdd); status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); return status; } NTSTATUS KmdfHelloWorldEvtDeviceAdd(_In_ WDFDRIVER Driver, _Inout_ PWDFDEVICE_INIT DeviceInit) { NTSTATUS status; WDFDEVICE hDevice; UNREFERENCED_PARAMETER(Driver); KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: KmdfHelloWorldEvtDeviceAdd\n")); status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &hDevice); return status; } 

如果安装已经完成,您需要创建一个EXE (testapp)来启动您的驱动程序。 您可以在应用程序中使用以下代码:

 SC_HANDLE schService; SC_HANDLE schSCManager; schSCManager = OpenSCManager(NULL, // local machine NULL, // local database SC_MANAGER_ALL_ACCESS // access required ); // Open the handle to the existing service. schService = OpenService(SchSCManager, DriverName, //name of the driver SERVICE_ALL_ACCESS ); StartService(schService, // service identifier 0, // number of arguments NULL // pointer to arguments )); 

您需要根据您的需要添加代码。 尝试这个。

有关更多信息,请下载microsoft提供的示例驱动程序和测试应用程序。

您可以使用内置的命令行“sc”(服务控制)工具来启动驱动程序。

语法是:

 sc start <name> 

因此,如果您的驱动程序安装了名称“KmdfHelloWorld”,则该命令应为:

 sc start KmdfHelloWorld 

目前,我正在为Windows 8.1和Windows 10写一个GPIO控制器/驱动程序,并且有类似的问题。 启动驱动程序最简单的方法是设置和配置计算机进行驱动程序测试,并使用Visual Studio在远程计算机上部署,安装和启动驱动程序。

编写驱动程序,然后远程部署和测试(在另一台计算机或VirtualBox等虚拟机上)是一个很好的做法,因为这可以减少您编写代码的计算机的混乱机率。

要配置计算机,我使用了以下MSDN页面: https : //msdn.microsoft.com/en-us/library/windows/hardware/dn745909?f=255&MSPPError=-2147217396

通过运行预先打包的测试,实际上可以让VS和Windows报告驱动程序的状态,获取调试信息,甚至设置断点。 相信我,对于初学者来说,这是最简单的方法。

另外,注册并创建默认工作状态的回调函数并不会有什么坏处,这样一来,您的驱动程序在运行时确实会做某些事情。 为此,请像EVT_WDF_DEVICE_D0_ENTRY一样使用EVT_WDF_DEVICE_D0_ENTRY定义。

快乐编码!