////////////////////////////////////////////driver.h////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#ifdef _cplusplus
extern "C"
{
#endif
#include <ntddk.h>
#ifdef _cplusplus
}
#endif
#define PAGEDCODE code_seg("PAGE")
#define LOCKEDCODE code_seg()
#define INITCODE code_seg("INIT")
#define PAGEDDATE date_seg("PAGE")
#define LOCKEDDATE date_seg()
#define INITDATE date_seg("INIT")
typedef struct _DEVICE_EXTENSION
{
UNICODE_STRING ustrDevName;
UNICODE_STRING ustrSymName;
PDEVICE_OBJECT pdevObj;
} DEVICE_EXTENSION,*PDEVICE_EXTENSION;
NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject);
NTSTATUS HelloDDKWriteRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS HelloDDKReadRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS HelloDDKCloseRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS HelloDDKCreateRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
void UnLoadRoutine(IN PDRIVER_OBJECT pDriverObject);
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////driver.cpp/////////////////////////////////////////////////////
#include "driver.h"
#pragma INITCODE
extern "C"
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,IN UNICODE_STRING ustrRegisterPath)
{
NTSTATUS status = STATUS_SUCCESS;
pDriverObject->DriverUnload = UnLoadRoutine;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = HelloDDKCloseRoutine;
pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKReadRoutine;
pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKWriteRoutine;
pDriverObject->MajorFunction[IRP_MJ_CREATE] = HelloDDKCreateRoutine;
status = CreateDevice(pDriverObject);
return status;
}
#pragma INITCODE
NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject)
{
NTSTATUS status;
PDEVICE_OBJECT pDevObj;
PDEVICE_EXTENSION pDevExt;
UNICODE_STRING devName;
RtlInitUnicodeString(&devName,L"\\DEVICE\\MyDevice");
status = IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&(UNICODE_STRING)devName,FILE_DEVICE_UNKNOWN,0,true,&pDevObj);
if(!NT_SUCCESS(status))
{
return status;
}
// pDevObj->NextDevice = NULL;
pDevObj->Flags |= DO_BUFFERED_IO;
pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
pDevExt->pdevObj = pDevObj;
pDevExt->ustrDevName = devName;
UNICODE_STRING symName;
RtlInitUnicodeString(&symName,L"\\??\\MyOwnDevice");
pDevExt->ustrSymName = symName;
status = IoCreateSymbolicLink(&symName,&devName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(pDevObj);
return status;
}
return STATUS_SUCCESS;
}
#pragma PAGEDCODE
NTSTATUS HelloDDKWriteRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
{
NTSTATUS status = STATUS_SUCCESS;
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(pIrp,IO_NO_INCREMENT);
return status;
}
#pragma PAGEDCODE
NTSTATUS HelloDDKReadRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
{
NTSTATUS status = STATUS_SUCCESS;
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(pIrp,IO_NO_INCREMENT);
return status;
}
#pragma PAGEDCODE