精易论坛

标题: 有没有驱动读写的教程 [打印本页]

作者: 愤怒的Rabbit    时间: 2024-12-4 15:21
标题: 有没有驱动读写的教程
驱动方面,完全零基础。   一直想学习驱动读写,网上教程也很多,但是网上教程百花齐放。
每个都不一样,这个看看,那个看看,最后感觉自己不知道看了个啥....
特来求助论坛各位大佬,给点零基础能看得懂的吧

作者: 愤怒的Rabbit    时间: 2024-12-4 15:27
感觉会驱动的人好多,都哪里看得教程啊
作者: hongqingfeng    时间: 2024-12-4 15:27
愤怒的Rabbit 发表于 2024-12-4 15:27
感觉会驱动的人好多,都哪里看得教程啊

哔哩哔哩,要么自己线下报班
作者: 愤怒的Rabbit    时间: 2024-12-4 15:31
hongqingfeng 发表于 2024-12-4 15:27
哔哩哔哩,要么自己线下报班

bilibili太多了,有的讲的很精简,但有的讲的又过于啰嗦,不知道看哪种好
作者: hongqingfeng    时间: 2024-12-4 15:34
愤怒的Rabbit 发表于 2024-12-4 15:31
bilibili太多了,有的讲的很精简,但有的讲的又过于啰嗦,不知道看哪种好

见仁见智,不好评判
作者: 承易    时间: 2025-3-19 22:33
#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 那是现在最好的老师




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4