///////////////////////////////////////////////////////////////////////
// Forward declarations of global functions
VOID Wdm1Unload(IN PDRIVER_OBJECT DriverObject);
NTSTATUS Wdm1Power(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Pnp(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo);
NTSTATUS Wdm1Create(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Close(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Write(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1Read(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1DeviceControl(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS Wdm1SystemControl(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
///////////////////////////////////////////////////////////////////////
// NTSTATUS CompleteIrp(PIRP Irp, NTSTATUS status, ULONG info);
///////////////////////////////////////////////////////////////////////
Listing 4.8 Init.cpp
///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//init.cpp:Driver initialization code
///////////////////////////////////////////////////////////////////////
//DriverEntryInitialisation entry point
//Wdm1UnloadUnload driver routine
///////////////////////////////////////////////////////////////////////
//Version history //27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#include 'wdm1.h'
#pragma code_seg('INIT') // start INIT section
///////////////////////////////////////////////////////////////////////
//DriverEntry:
//
//Description:
//This function initializes the driver, and creates
//any objects needed to process I/O requests.
//
//Arguments:
//Pointer to the Driver object
//Registry path string for driver service key
//
//Return Value:
//This function returns STATUS_XXX
extern 'C'
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {
NTSTATUS status = STATUS_SUCCESS;
#if DBG
DebugPrint Init('Wdm1 checked');
#else
DebugPrintInit('Wdm1 free');
#endif
DebugPrint('RegistryPath is %T', RegistryPath);
// Export other driver entry points…
DriverObject->DriverExtension->AddDevice = Wdm1AddDevice;
DriverObject->DriverUnload = Wdm1Unload;
OriverObject->MajorFunction[IRP_MJ_CREATE] = Wdm1Create;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = Wdm1Close;
DriverObject->MajorFunction[IRP_MJ_PNP] = Wdm1Pnp;
DriverObject->MajorFunction[IRP_MJ_POWER] = Wdm1Power;
DriverObject->MajorFunction[IRP_MJ_READ] = Wdm1Read;
DriverObject->MajorFunction[IRP_MJ_WRITE] = Wdm1Write;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = Wdm1DeviceControl;
DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROl] = Wdm1SystemControl;
//Initialise spin lock which protects access to shared memory buffer
KeInitializeSpinLock(&BufferLock);
DebugPrintMsg('DriverEntry completed');
return status;
}
#pragma code_seg() // end INIT section
///////////////////////////////////////////////////////////////////////
//Wdm1Unload
//
//Description:
//Unload the driver by removing any remaining objects, etc.
//
//Arguments:
//Pointer to the Driver object
//
//Return Value:
//None
#pragma code_seg('PAGE') // start PAGE section
VOID Wdm1Unload(IN PDRIVER_OBJECT DriverObject) {
// Free buffer (do not need to acquire spin lock)
if (Buffer!=NULL) ExFreePool(Buffer);
DebugPrintMsg('WdmlUnload');
DebugPrintClose();
}