레거시 드라이버 Skelecton Day+by+day

드라이버 스켈렉톤 코드. (길어서 가려둠.)

#include <wdm.h>

///////////////////////////////////////////////////////////////////////////////
//
// brif : alloc and no free when exit
//
//

#define DRIVER_NAME "AllocAndNoFree "

typedef struct _DEVICE_EXTENSION{
    PDEVICE_OBJECT Self;
    PVOID AllocatedMemory;
}DEVICE_EXTENSION, *PDEVICE_EXTENSION;


NTSTATUS AddDevice(PDRIVER_OBJECT pDriverObject,
                   PDEVICE_OBJECT pPDO/*physical device object*/);
VOID DriverUnload(PDRIVER_OBJECT pDriverObject);
NTSTATUS ANFCreate(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);
NTSTATUS ANFClose(PDEVICE_OBJECT pDeviceObject, PIRP pIrp);




#define ALLOC_SIZE 1024*1024*4 //4K
#define ALLOC_TAG ((ULONG)' FnA')
NTSTATUS DriverEntry(
                     IN PDRIVER_OBJECT pDriverObject,
                     IN PUNICODE_STRING pRegistryPath/*registry service key name*/)
{
    NTSTATUS Status = STATUS_SUCCESS;
    PDEVICE_OBJECT pDeviceObject;
    PDEVICE_EXTENSION pDeviceExtension;
    PCHAR DummyStr = "This Is Alloc and NoFree Example";

    PAGED_CODE();

    
    DbgPrint(DRIVER_NAME"DriverEntry\r\n");

    Status = IoCreateDevice(
        pDriverObject, sizeof(DEVICE_EXTENSION), NULL, FILE_DEVICE_UNKNOWN,
        0, FALSE, &pDeviceObject);

    if(!NT_SUCCESS(Status))
        {
        DbgPrint(DRIVER_NAME"IoCreateDevice Failed");
        return Status;

        }

    //register MJCode//
    //pDriverObject->DriverExtension->AddDevice = AddDevice;
    pDriverObject->DriverUnload = DriverUnload;
    pDriverObject->MajorFunction[IRP_MJ_CREATE] = ANFCreate;
    pDriverObject->MajorFunction[IRP_MJ_CLOSE] = ANFClose;
    //register MJCode//end


    pDeviceExtension = pDeviceObject->DeviceExtension;
    pDeviceExtension->Self = pDeviceObject;
    
    //alloc
    pDeviceExtension->AllocatedMemory = ExAllocatePoolWithTag(
        PagedPool, ALLOC_SIZE, ALLOC_TAG/*alloc and free*/);

    if(pDeviceExtension->AllocatedMemory == NULL)
        {
        DbgPrint(DRIVER_NAME"ExAllocatePoolWithTag Failed");
        Status = STATUS_MEMORY_NOT_ALLOCATED;
        return Status;
        }
    RtlCopyMemory(pDeviceExtension->AllocatedMemory, DummyStr, strlen(DummyStr));
    //alloc//end

    return Status;
}



NTSTATUS AddDevice(
                   PDRIVER_OBJECT pDriverObject,
                   PDEVICE_OBJECT pPDO/*physical device object*/)
{
    NTSTATUS Status = STATUS_SUCCESS;
    DbgPrint(DRIVER_NAME"AddDevice\r\n");

    return Status;
}




NTSTATUS ANFCreate(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
    NTSTATUS Status = STATUS_SUCCESS;

    DbgPrint(DRIVER_NAME"ANFCreate\r\n");
    
    pIrp->IoStatus.Status = Status;
    pIrp->IoStatus.Information = 0;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);

    return Status;
}

NTSTATUS ANFClose(PDEVICE_OBJECT pDeviceObject, PIRP pIrp)
{
    NTSTATUS Status = STATUS_SUCCESS;
    
    DbgPrint(DRIVER_NAME"ANFClose\r\n");
    
    pIrp->IoStatus.Status = Status;
    pIrp->IoStatus.Information = 0;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    
    return Status;
}



VOID DriverUnload(
                  PDRIVER_OBJECT pDriverObject)
{
    PDEVICE_EXTENSION pDeviceExtension;
    PDEVICE_OBJECT pDeviceObject;
    DbgPrint(DRIVER_NAME"DriverUnload\r\n");

    pDeviceObject = pDriverObject->DeviceObject;
    pDeviceExtension = pDeviceObject->DeviceExtension;

    //free//
    if(pDeviceExtension->AllocatedMemory != NULL)
        {
        ExFreePoolWithTag(pDeviceExtension->AllocatedMemory, ALLOC_TAG);
        pDeviceExtension->AllocatedMemory = NULL;
        }
    //free//end

    IoDeleteDevice(pDeviceObject);
    return ;
}

덧글

댓글 입력 영역