【问题标题】:World's most simple Windows driver世界上最简单的 Windows 驱动程序
【发布时间】:2012-06-27 22:34:54
【问题描述】:

我遇到了一个稍微奇怪的问题,涉及将 MoGo 鼠标放入我的 Windows XP 笔记本电脑的墨盒插槽时无法充电。说来话长,但修复它的一个建议是编写一个定制的驱动程序,它只说“我运行正常:不要关闭电源”。

我认为这应该是微不足道的,但我唯一的驱动程序经验是通过提供的 MSI 下载和安装它们。我意识到我不知道:

  • 它们是用什么语言编写的。
  • 他们必须遵循哪些约定。
  • 它们如何与各自的硬件相关联。
  • 他们的位置。
  • 或者实际上,任何东西......

我也没有在网络上发现任何非常有用的东西 - 可能是因为它们的目标远高于我的水平。

欢迎任何见解。

【问题讨论】:

  • 由于还没有答案(但我相信会有好的答案...),请查看Windows Driver Kit (WDK)
  • 谢谢,吉恩。看来我可能不得不双脚跳入并尝试理解 Microsoft 文档!
  • 我也一直对此很好奇。幸运的是,there is a guide from Microsoft。不幸的是,这类问题不在此处讨论。

标签: windows windows-xp device-driver


【解决方案1】:

Microsoft 在其文档中提供了“Hello World”驱动程序示例。这是“世界上最简单的Windows驱动程序”的一个例子。不幸的是,它有 13 页长,因此不适合 StackOverflow 的答案。

它们的编写语言是 C++。

要开始使用,请确保您拥有 Microsoft Visual Studio、Windows SDK 和已安装的 Windows 驱动程序工具包 (WDK)。

他们的示例包含一个名为 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;
}

在这里找到完整的详细信息: https://docs.microsoft.com/en-us/windows-hardware/drivers/gettingstarted/writing-a-very-small-kmdf--driver

【讨论】:

  • 感谢@HackSlash。我没有那个鼠标了,但我相信这在以后会有用。
猜你喜欢
  • 1970-01-01
  • 2013-07-08
  • 2018-01-03
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多