我尝试打开设备对象并通过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空间打开对象,必须使用NtCreateFile,但我在驱动程序中创建了一个符号链接,因此CreateFile必须运行良好。
答案 0 :(得分:1)
当有人在设备DeviceObject
上尝试打开文件时,会调用DeviceObject->DriverObject->MajorFunction[IRP_MJ_CREATE]
。现在问题 - 这一点到底在哪里?你没有改变这个细胞。你只启动IRP_MJ_DEVICE_CONTROL
单元格。结果称为默认处理程序 - IopInvalidDeviceRequest
。此例程只需完成状态为STATUS_INVALID_DEVICE_REQUEST
的请求。 win32将其转换为ERROR_INVALID_FUNCTION
。所以你必须得到这个错误。如果我们想要在我们的设备上打开文件 - 需要实施IRP_MJ_CREATE
,IRP_MJ_CLEANUP
和IRP_MJ_CLOSE
最低限度