#endif
#include "wdm.h"
#ifdef __cplusplus
}
#endif
///////////////////////////////////////////////////////////////////////
//DebugPrint and Guid headers
#include "DebugPrint.h"
#include "GUIDs.h"
///////////////////////////////////////////////////////////////////////
//Spin lock to protect access to shared memory buffer
extern KSPIN_LOCK BufferLock;
extern PUCHAR Buffer;
///////////////////////////////////////////////////////////////////////
//Our device extension
typedef struct _WDM1_DEVICE_EXTENSION {
PDEVICE_OBJECT fdo;
PDEVICE_OBJECT NextStackDevice;
UNICODE_STRIN GifSymLinkName;
} WDM1_DEVICE_EXTENSION, *PWDM1_DEVICE_EXTENSION;
///////////////////////////////////////////////////////////////////////
// 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();
}
///////////////////////////////////////////////////////////////////////
# pragma code_seg() // end PAGE section
Listing 4.9 Pnp.cpp
///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//pnp.cpp:Plug and Play and Power IRP handlers
///////////////////////////////////////////////////////////////////////
//Wdm1AddDeviceAdd device routine
//Wdm1PnpPNP IRP dispatcher
//Wdm1PowerPOWER IRP dispatcher
///////////////////////////////////////////////////////////////////////
//Version history //27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#define INITGUID// initialize WDM1_GUID in this module
#include "wdm1.h"
#pragma code_seg("PAGE")// start PAGE section
///////////////////////////////////////////////////////////////////////
//Wdm1AddDevice:
//
//Description:
//Cope with a new Pnp device being added here.
//Usually just attach to the top of the driver stack.
//Do not talk to device here!
//
//Arguments:
Читать дальше