⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statusr.c

📁 查看NT状态
💻 C
字号:
// Simple driver that demonstrates dynamically loading and unloading
         
#include "ntddk.h"
#include "statusRioctl.h"

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

NTSTATUS statusROpen(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp );

NTSTATUS statusRClose(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp );


NTSTATUS statusRDeviceControl (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp );

NTSTATUS statusRInternalDeviceControl (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp );



VOID statusRUnload(
    IN PDRIVER_OBJECT DriverObject );

NTSTATUS DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath )
{

    PDEVICE_OBJECT deviceObject = NULL;
    NTSTATUS status;
    UNICODE_STRING uniNtNameString;
    UNICODE_STRING uniWin32NameString;

    KdPrint( ("statusR: Entered the statusR driver!\n") );

    //
    // Create counted string version of our device name.
    //

    RtlInitUnicodeString( &uniNtNameString, NT_DEVICE_NAME );

    //
    // Create the device object
    //

    status = IoCreateDevice(
                 DriverObject,
                 0,                     // We don't use a device extension
                 &uniNtNameString,
                 FILE_DEVICE_UNKNOWN,
                 0,                     // No standard device characteristics
                 FALSE,                 // This isn't an exclusive device
                 &deviceObject
                 );

    if ( NT_SUCCESS(status) )
    {

        //
        // Create dispatch points for create/open, close, unload.
        //

        DriverObject->MajorFunction[IRP_MJ_CREATE] = statusROpen;
        DriverObject->MajorFunction[IRP_MJ_CLOSE] = statusRClose;

        DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = statusRDeviceControl;

        DriverObject->DriverUnload = statusRUnload;

        KdPrint( ("statusR: just about ready!\n") );

        //
        // Do buffered I/O.  I.e., the I/O system will copy to/from user data
        // from/to a system buffer.
        //

        deviceObject->Flags |= DO_DIRECT_IO;

        //
        // Create counted string version of our Win32 device name.
        //

        RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );

        //
        // Create a link from our device name to a name in the Win32 namespace.
        //

        status = IoCreateSymbolicLink( &uniWin32NameString, &uniNtNameString );

        if (!NT_SUCCESS(status))
        {
            KdPrint( ("statusR: Couldn't create the symbolic link\n") );

            IoDeleteDevice( DriverObject->DeviceObject );
        }
        else
        {
            KdPrint( ("statusR: All initialized!\n") );
        }
    }
    else
    {
        KdPrint( ("statusR: Couldn't create the device\n") );
    }
    return status;
}

NTSTATUS statusROpen(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp )
{
    KdPrint( ("statusR: Opened!!\n") );


    //
    // No need to do anything.
    //

    //
    // Fill these in before calling IoCompleteRequest.
    //
    // DON'T get cute and try to use the status field of
    // the irp in the return status.  That IRP IS GONE as
    // soon as you call IoCompleteRequest.
    //


    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;

    IoCompleteRequest( Irp, IO_NO_INCREMENT );

    return STATUS_SUCCESS;
}

NTSTATUS statusRClose(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp )
{

    KdPrint( ("statusR: Closed!!\n") );

    //
    // No need to do anything.
    //

    //
    // Fill these in before calling IoCompleteRequest.
    //
    // DON'T get cute and try to use the status field of
    // the irp in the return status.  That IRP IS GONE as
    // soon as you call IoCompleteRequest.
    //

    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;



    IoCompleteRequest( Irp, IO_NO_INCREMENT );

    return STATUS_SUCCESS;
}

NTSTATUS  statusRDeviceControl(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp )
{


    NTSTATUS  ret = STATUS_SUCCESS;

    PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);

 //   ULONG  *pOutBuffer;
    ULONG   *pInBuffer;

    NTSTATUS the_NTSTATUS;

    KdPrint( ("statusR: Device Control!!\n") );



    pInBuffer = (ULONG *)Irp->AssociatedIrp.SystemBuffer;  // for buffered i/o
//    pOutBuffer = (ULONG *)Irp->UserBuffer;    // for buffered i/o



    switch(irpStack->Parameters.DeviceIoControl.IoControlCode)
    {

        case  IOCTL_FAIL_WITH:
             KdPrint( ("StatusR: Device Control  --  failing with status = %Lx\n", *pInBuffer) );
             ret = *pInBuffer;
             break;

        default:

             ret = STATUS_SUCCESS;

    }
    //
    // Fill these in before calling IoCompleteRequest.
    //

    Irp->IoStatus.Status = ret;
    Irp->IoStatus.Information = 0;

    IoCompleteRequest( Irp, IO_NO_INCREMENT );


    return ret;
}

VOID statusRUnload( IN PDRIVER_OBJECT DriverObject)
{
    UNICODE_STRING uniWin32NameString;

    //
    // All *THIS* driver needs to do is to delete the device object and the
    // symbolic link between our device name and the Win32 visible name.
    //
    // Almost every other driver ever witten would need to do a
    // significant amount of work here deallocating stuff.
    //

    KdPrint( ("statusR: Unloading!!\n") );

    //
    // Create counted string version of our Win32 device name.
    //

    RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );

    //
    // Delete the link from our device name to a name in the Win32 namespace.
    //

    IoDeleteSymbolicLink( &uniWin32NameString );

    //
    // Finally delete our device object
    //

    IoDeleteDevice( DriverObject->DeviceObject );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -