简体   繁体   English

从C ++调用WDF驱动程序

[英]Calling WDF driver from c++

I've been trying to call a sample driver. 我一直在尝试给示例驱动程序打电话。 I have written DriverEntry method, where I initialize both the driver name and symbolic ling pointing to the driver. 我已经编写了DriverEntry方法,在其中初始化了驱动程序名称和指向该驱动程序的符号ling。

// UNICODE_STRING DriverName, SymbolName; // Driver registry paths
...
    // Driver Entrypoint
    NTSTATUS
    DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) {
  Q_UNUSED(pRegistryPath);

  DbgPrintEx(0, 0, "Driver Loaded\n");

  // The PsSetLoadImageNotifyRoutine routine registers a driver-supplied
  // callback that is subsequently notified whenever
  // an image is loaded (or mapped into memory).
  PsSetLoadImageNotifyRoutine(ImageLoadCallback);

  // initialize driver name
  RtlInitUnicodeString(&DriverName, L"\\Device\\Explorer");
  // initialize symbolic link
  RtlInitUnicodeString(&SymbolName, L"\\DosDevices\\Explorer");

  IoCreateDevice(pDriverObject, 0, &SymbolName, FILE_DEVICE_UNKNOWN,
                 FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
  IoCreateSymbolicLink(&DriverName, &SymbolName);

  pDriverObject->MajorFunction[IRP_MJ_CREATE] = CreateCall;
  pDriverObject->MajorFunction[IRP_MJ_CLOSE] = CloseCall;
  pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IoControl;
  pDriverObject->DriverUnload = UnloadDriver;

  pDeviceObject->Flags |= DO_DIRECT_IO;
  pDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;

  return STATUS_SUCCESS;
}

When I load the driver up (using OSR Driver Loader, could be done using cmd also, by registering the driver as a new service), I get expected output in DebugView (sysinternals tool allowing to see kernel debug logs) 当我加载驱动程序时(使用OSR Driver Loader,也可以使用cmd,通过将驱动程序注册为新服务​​来完成),我在DebugView中得到了预期的输出(sysinternals工具允许查看内核调试日志)

在此处输入图片说明

Now I needed to make sure that both the device and symlink are present in Windows Object Directories. 现在,我需要确保Windows对象目录中同时存在设备和符号链接。 To do that, I use WinObj (another tool from sysinternals), here is the output 为此,我使用WinObj(sysinternals的另一个工具),这是输出

What confuses me here, is that the symbolic link is in Device folder, instead of GLOBAL?? 让我感到困惑的是,符号链接位于Device文件夹中,而不是GLOBAL? . Symbolic link in Device 设备中的符号链接 在此处输入图片说明

Device in GLOBAL?? GLOBAL中的设备?

在此处输入图片说明

Now, finally, calling the driver itself. 现在,最后,调用驱动程序本身。 I use c++ for that purpose and this is my code, 我为此使用c ++,这是我的代码,

class Test
{
public:
HANDLE hDriver; // Handle to driver

                // Initializer
Test::Test(LPCSTR RegistryPath)
{
    LPCSTR path = "\\\\.\\Explorer";
    hDriver = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);


    if (hDriver == INVALID_HANDLE_VALUE)
    {
        //  Handle the error.
        char result = GetLastError();
        bool zadek = false;
    }
}

The problem is that I can't get a valid handle for the driver. 问题是我无法获得驱动程序的有效句柄。 The value of hDriver is always either 0x00000000000000a0 or 0xffffffff, no matter the path I use. 无论我使用的路径如何,hDriver的值始终为0x00000000000000a0或0xffffffff。 I'm using createFileA because I want to access system memory. 我使用createFileA是因为我想访问系统内存。

Is there some blatant mistake I made? 我犯了一些公然的错误吗?

I should say it is over 8-9 year since last time I written a device driver, but what comes off the top of my head are: 我应该说,自上次编写设备驱动程序以来已经有8-9年的时间了,但是我想到的是:

  1. You say you get 0xa0 for hDriver which is a valid handle value. 您说您为hDriver得到了0xa0 ,这是一个有效的句柄值。
  2. Right now, you can only use device IO control, because you only have callback for IRP_MJ_DEVICE_CONTROL . 现在,您只能使用设备IO控制,因为您只有IRP_MJ_DEVICE_CONTROL回调。
  3. Try L"\\\\??\\\\Explorer" or L"\\\\GLOBAL??\\\\Explorer" for symbolic link. 尝试使用L"\\\\??\\\\Explorer"L"\\\\GLOBAL??\\\\Explorer"进行符号链接。
  4. You need to use DriverName for IoCreateDevice . 您需要为IoCreateDevice使用DriverName
  5. You are passing incorrect arguments to IoCreateSymbolicLink . 您正在将错误的参数传递给IoCreateSymbolicLink

So your code should become like this: 因此您的代码应如下所示:

...
// initialize driver name
RtlInitUnicodeString(&DriverName, L"\\Device\\Explorer");
// initialize symbolic link
RtlInitUnicodeString(&SymbolName, L"\\??\\Explorer");

IoCreateDevice(pDriverObject, 0, &DriverName, FILE_DEVICE_UNKNOWN,
                 FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
IoCreateSymbolicLink(&SymbolName, &DriverName);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM