Simple Driver Sample



#include <ntddk.h>

#define MY_DEVICE_NAME     L"\\Device\\MYDRIVER"
#define MY_DOS_DEVICE_NAME L"\\DosDevices\\MyDriver"


NTSTATUS MyDoSomething(IN PDEVICE_OBJECT oDevice,IN PIRP irp){
    DbgPrint("DoSomething\n");
    return STATUS_SUCCESS;
}

VOID MyUnload(IN PDRIVER_OBJECT oDriver){
    UNICODE_STRING s;
    PDEVICE_OBJECT oDevice = oDriver->DeviceObject;
    RtlInitUnicodeString( &s, MY_DOS_DEVICE_NAME );
    IoDeleteSymbolicLink( &s );
    IoDeleteDevice( oDevice );
    DbgPrint("MyUnload Exit\n");
    return;
}


NTSTATUS DriverEntry(IN PDRIVER_OBJECT  oDriver,IN PUNICODE_STRING RegistryPath){
    NTSTATUS status;
    UNICODE_STRING DeviceName;   
    UNICODE_STRING DosDeviceName;  
    PDEVICE_OBJECT oDevice;
    status = STATUS_SUCCESS
    
    DbgPrint("DriverEntry Enter \n");
    (void) RtlInitUnicodeString(&DeviceName, MY_DEVICE_NAME);
    status = IoCreateDevice(oDriver,0,&DeviceName,FILE_DEVICE_UNKNOWN,0,(BOOLEAN) FALSE,&oDevice);
    if (!NT_SUCCESS(status)){
        return status;
    }
    (void)RtlInitUnicodeString( &DosDeviceName, MY_DOS_DEVICE_NAME );
    status = IoCreateSymbolicLink((PUNICODE_STRING) &DosDeviceName,(PUNICODE_STRING) &DeviceName);
    if (!NT_SUCCESS(status)){
        IoDeleteDevice(oDevice);
        return status;
    }
    oDriver->MajorFunction[IRP_MJ_CREATE]=
    oDriver->MajorFunction[IRP_MJ_CLOSE] = 
    oDriver->MajorFunction[IRP_MJ_READ] = 
    oDriver->MajorFunction[IRP_MJ_CLEANUP] = MyDoSomething;
    oDriver->DriverUnload = MyUnload;
    DbgPrint("DriverEntry Exit = %x\n", status);
    return status;
}

1
Hosted by www.Geocities.ws