|
6楼
发表于 2025-3-19 22:33:34
|
只看该作者
山西省太原市
#include <ntddk.h>
#define DEVICE_NAME L"\\Device\\MyDevice"
#define SYMBOLIC_LINK_NAME L"\\DosDevices\\MyDevice"
UNICODE_STRING DeviceName;
UNICODE_STRING SymbolicLinkName;
PDEVICE_OBJECT DeviceObject;
NTSTATUS CreateClose(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS ReadWrite(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
PVOID buffer = Irp->AssociatedIrp.SystemBuffer;
ULONG length = stack->Parameters.Read.Length;
// 这里可以添加读写逻辑
DbgPrint("Read/Write operation: %lu bytes\n", length);
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = length;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
void UnloadDriver(PDRIVER_OBJECT DriverObject) {
IoDeleteSymbolicLink(&SymbolicLinkName);
IoDeleteDevice(DriverObject->DeviceObject);
DbgPrint("Driver unloaded\n");
}
extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
RtlInitUnicodeString(&DeviceName, DEVICE_NAME);
RtlInitUnicodeString(&SymbolicLinkName, SYMBOLIC_LINK_NAME);
IoCreateDevice(DriverObject, 0, &DeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &DeviceObject);
IoCreateSymbolicLink(&SymbolicLinkName, &DeviceName);
DriverObject->MajorFunction[IRP_MJ_CREATE] = CreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = CreateClose;
DriverObject->MajorFunction[IRP_MJ_READ] = ReadWrite;
DriverObject->MajorFunction[IRP_MJ_WRITE] = ReadWrite;
DriverObject->DriverUnload = UnloadDriver;
DbgPrint("Driver loaded\n");
return STATUS_SUCCESS;
}
#include <windows.h>
#include <stdio.h>
#define DEVICE_NAME "\\\\.\\MyDevice"
int main() {
HANDLE hDevice = CreateFile(DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
printf("Failed to open device\n");
return 1;
}
char buffer[100];
DWORD bytesRead;
if (ReadFile(hDevice, buffer, sizeof(buffer), &bytesRead, NULL)) {
printf("Read %lu bytes: %s\n", bytesRead, buffer);
} else {
printf("Failed to read from device\n");
}
char writeBuffer[] = "Hello from user mode!";
DWORD bytesWritten;
if (WriteFile(hDevice, writeBuffer, sizeof(writeBuffer), &bytesWritten, NULL)) {
printf("Wrote %lu bytes\n", bytesWritten);
} else {
printf("Failed to write to device\n");
}
CloseHandle(hDevice);
return 0;
}
去问AI 那是现在最好的老师 |
|