#include <ntddk.h>

void DriverUnload(IN PDRIVER_OBJECT DriverObject);
NTSTATUSMyCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUSMyDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);

NTSTATUSDriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegPath)
{
UNICODE_STRINGustrDevName, ustrLinkName;
PDEVICE_OBJECTpDeviceObj = NULL;
NTSTATUSstatus;
unsigned inti;

KdPrint(("DriverEntry ...\n"));

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 ...\n"));
return status;
}

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

pDeviceObj->Flags |= DO_DIRECT_IO;

status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);

pDeviceObj->Flags &= ~DO_DEVICE_INITIALIZING;

return STATUS_SUCCESS;
}

VOIDDriverUnload(IN PDRIVER_OBJECT DriverObject)
{
UNICODE_STRINGustrLinkName;

KdPrint(("DriverUnload ...\n"));

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

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

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

NTSTATUSMyDefaultHandler(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;
}

05-11 19:53