• 基本NT式驱动代码结构


    #include <ntddk.h>

    void DriverUnload(IN PDRIVER_OBJECT DriverObject);
    NTSTATUS MyCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
    NTSTATUS MyDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);

    NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegPath)
    {
    UNICODE_STRING ustrDevName, ustrLinkName;
    PDEVICE_OBJECT pDeviceObj = NULL;
    NTSTATUS status;
    unsigned int i;

    KdPrint(("DriverEntry ... "));

    RtlInitUnicodeString(&ustrDevName, L"\Device\demo0");
    RtlInitUnicodeString(&ustrLinkName, L"\DosDevices\demo0");

    for (i=0; i<IRP_MJ_MAXIMUM_FUNCTION; i++)
    {
    DriverObject->MajorFunction[i] = MyDefaultHandler;
    }

    DriverObject->MajorFunction[IRP_MJ_CREATE] = MyCreateClose;
    DriverObject->MajorFunction[IRP_MJ_CLOSE] = MyCreateClose;

    DriverObject->DriverUnload = DriverUnload;

    status = IoCreateDevice(DriverObject,
    0,
    &ustrDevName,
    FILE_DEVICE_UNKNOWN,
    0,
    FALSE,
    &pDeviceObj);

    if (!NT_SUCCESS(status))
    {
    KdPrint(("IoCreateDevice failed ... "));
    return status;
    }

    if (!pDeviceObj)
    {
    KdPrint(("pDeviceObj == NULL "));
    return STATUS_UNEXPECTED_IO_ERROR;
    }

    pDeviceObj->Flags |= DO_DIRECT_IO;

    status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);

    pDeviceObj->Flags &= ~DO_DEVICE_INITIALIZING;

    return STATUS_SUCCESS;
    }

    VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
    {
    UNICODE_STRING ustrLinkName;

    KdPrint(("DriverUnload ... "));

    RtlInitUnicodeString(&ustrLinkName, L"\DosDevices\demo0");

    IoDeleteSymbolicLink(&ustrLinkName);
    IoDeleteDevice(DriverObject->DeviceObject);
    }

    NTSTATUS MyCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
    {
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
    }

    NTSTATUS MyDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
    {
    Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return Irp->IoStatus.Status;
    }

  • 相关阅读:
    什么是SQLCLR与使用
    SQL Server中使用正则表达式
    YUV格式
    Android官方开发文档Training系列课程中文版:手势处理之ViewGroup的事件管理
    Android中利用5.0系统屏幕录制UI漏洞骗取应用录制屏幕授权
    Android解析编译之后的所有文件(so,dex,xml,arsc)格式
    PageRank 算法--从原理到实现
    机器人视觉初级系列
    深入解析 iOS 开源项目
    微信热补丁 Tinker 的实践演进之路
  • 原文地址:https://www.cnblogs.com/debug-me/p/6159894.html
Copyright © 2020-2023  润新知