无法通过 CreateFile 打开设备,ERROR_INVALID_FUNCTION

Can`t open device by CreateFile, ERROR_INVALID_FUNCTION

我尝试打开设备对象并通过 CreateFile 和 DeviceIoControl 向我的驱动程序发送请求,但是 CreateFile 失败并显示 ERROR_INVALID_FUNCTION,如果我尝试在 WinObj 中打开设备 - 我得到同样的错误,那么如何解决这个问题是什么意思?

test_driver.c

#include "test_driver.h"

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {
    NTSTATUS status;
    PDEVICE_OBJECT deviceObject;
    UNICODE_STRING dNameString, dLinkString;

    RtlInitUnicodeString(&dNameString, NT_DEVICE_NAME);
    status = IoCreateDevice(DriverObject, sizeof(65533), &dNameString, FILE_DEVICE_UNKNOWN, 0, FALSE, &deviceObject);

    if (!NT_SUCCESS(status)) {
        return status;
    }

    RtlInitUnicodeString(&dLinkString, DOS_DEVICE_NAME);
    status = IoCreateSymbolicLink(&dLinkString, &dNameString);

    if (!NT_SUCCESS(status)) {
        IoDeleteDevice(deviceObject);
        return status;
    }

    DriverObject -> MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverDeviceControl;
    DriverObject -> DriverUnload = DriverUnload;

    DbgPrint("Driver loaded!");
    return STATUS_SUCCESS;
}

NTSTATUS DriverDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
    ULONG ioControlCode;
    PULONG ioBuffer;
    PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);

    ioBuffer = Irp -> AssociatedIrp.SystemBuffer;
    ioControlCode = irpStack -> Parameters.DeviceIoControl.IoControlCode;
    Irp -> IoStatus.Status = STATUS_SUCCESS;
    Irp -> IoStatus.Information = 0;

    switch (ioControlCode) {
        case TEST_SMTH:
            ioBuffer[0] = (ULONG)DriverEntry;
            Irp -> IoStatus.Information = 4;
            break;

        default:
            Irp -> IoStatus.Status = STATUS_INVALID_PARAMETER;
            break;
    }

    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    DbgPrint("Request complete!");
    return Irp -> IoStatus.Status;
}

VOID DriverUnload(IN PDRIVER_OBJECT DriverObject) {
    UNICODE_STRING dLinkString;

    RtlInitUnicodeString(&dLinkString, DOS_DEVICE_NAME);
    IoDeleteSymbolicLink(&dLinkString);
    IoDeleteDevice(DriverObject -> DeviceObject);
    DbgPrint("Driver unloaded!");
}

test_driver.h

#include <ntddk.h>

#define NT_DEVICE_NAME  L"\Device\testDrv" 
#define DOS_DEVICE_NAME L"\DosDevices\testDrv"

#define FIRST_IOCTL_INDEX 0x800
#define FILE_DEVICE_testDRV 0x00008000
#define TEST_SMTH CTL_CODE(FILE_DEVICE_testDRV, FIRST_IOCTL_INDEX + 101, METHOD_BUFFERED, FILE_ANY_ACCESS)

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);
NTSTATUS DriverDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
VOID DriverUnload(IN PDRIVER_OBJECT DriverObject);

节目来源:

HANDLE device = CreateFile("\\.\testDrv", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

我知道从 NT space 打开对象需要使用 NtCreateFile,但我在我的驱动程序中创建了一个符号 link,所以 CreateFile 一定能正常工作。

当有人尝试打开设备 DeviceObject 上的文件时,将调用 DeviceObject->DriverObject->MajorFunction[IRP_MJ_CREATE]。现在的问题——这一点到哪里去了?你不改变这个单元格。你只初始化 IRP_MJ_DEVICE_CONTROL 单元格。结果称为默认处理程序 - IopInvalidDeviceRequest。此例程简单地完成状态为 STATUS_INVALID_DEVICE_REQUEST 的请求。 win32 将其转换为 ERROR_INVALID_FUNCTION。所以你必须得到这个错误。如果我们想在我们的设备上打开文件 - 需要实现 IRP_MJ_CREATEIRP_MJ_CLEANUPIRP_MJ_CLOSE 最小值