//Pointer to the Driver object
//Pointer to Physical Device Object
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS WdmlAddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo) {
DebugPrint("AddDevice");
NTSTATUS status;
PDEVICE_OBJECT fdo;
// Create our Functional Device Object in fdo
status = IoCreateDevice(DriverObject, sizeof(WDM1_DEVICE_EXTENSION),
NULL,// No Name
FILE_DEVICE_UNKNOWN, 0,
FALSE,// Not exclusive
&fdo);
if (!NT_SUCCESS(status)) return status;
// Remember fdo in our device extension
PWDM1_DEVICE_EXTENSION dx = (PWDM1_DEVICE_EXTENSION)fdo->DeviceExtension;
dx->fdo = fdo;
DebugPrint("FDO is %x",fdo);
// Register and enable our device interface
status = IoRegisterDeviceInterface(pdo, &WDM1_GUID, NULL, &dx->ifSymLinkName);
if (!NT_SUCCESS(status)) {
IoDeleteDevice(fdo);
return status;
}
IoSetDeviceInterfaceState(&dx->ifSymLinkName, TRUE);
DebugPrint("Symbolic Link Name is %T", &dx->ifSymLinkName);
// Attach to the driver stack below us
dx->NextStackDevice = IoAttachDeviceToDeviceStack(fdo.pdo);
// Set fdo flags appropriately
fdo->Flags &= ~DO_DEVICE_INITIALIZING;
fdo->Flags |= DO_BUFFERED_IO;
return STATUS_SUCCESS;
}
///////////////////////////////////////////////////////////////////////
//Wdm1Pnp:
//
//Description:
//Handle IRP_MJ_PNP requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//Various minor codes
//IrpStack->Parameters.QueryDeviceRelations
//IrpStack->Parameters.QueryInterface
//IrpStack->Parameters.DeviceCapabilities
//IrpStack->Parameters.FilterResourceRequirements
//IrpStack->Parameters.ReadWriteConfig
//IrpStack->Parameters.SetLock
//IrpStack->Parameters.QueryId
//IrpStack->Parameters.QueryDeviceText
//IrpStack->Parameters.UsageNotification
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Pnp(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
DebugPrint("PnP %I", Irp);
PWDM1_DEVICE_EXTENSION dx=(PWDMl_DEVICE_EXTENSION)fdo->DeviceExtension;
// Remember minor function
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
ULONG MinorFunction = IrpStack->MinorFunction;
// Just pass to lower driver
IoSkipCurrentIrpStackLocation(Irp);
NTSTATUS status = IoCallDriver(dx->NextStackDevice, Irp);
// Device removed
if (MinorFunction==IRP_MN_REMOVE_DEVICE) {
DebugPrint("PnP RemoveDevice");
// disable device interface
IoSetDeviceInterfaceState(&dx->ifSymLinkName, FALSE);
RtlFreeUnicodeString(&dx->ifSymLinkName);
// unattach from stack
if (dx->NextStackDevice) IoDetachDevice(dx->NextStackDevice);
// delete our fdo IoDeleteDevice(fdo);
}
return status;
}
///////////////////////////////////////////////////////////////////////
//Wdm1Power:
//
//Description:
//Handle IRP_MJ_POWER requests
//
//Arguments:
//Pointer to the FDO
//Pointer to the IRP
//IRP_MN_WAIT_WAKE:IrpStack->Parameters.WaitWake.Xxx
//IRP_MN_POWER_SEOUENCE:IrpStack->Parameters.PowerSequence.Xxx
//IRP_MN_SET_POWER:
//IRP_MN_QUERY_POWER:IrpStack->Parameters.Power.Xxx
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Power(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
DebugPrint("Power %I",Irp);
PWDM1_DEVICE_EXTENSION dx = (PWDM1_DEVICE_EXTENSION)fdo->DeviceExtension;
// Just pass to lower driver
PoStartNextPowerIrp(Irp);
IoSkipCurrentIrpStackLocation(Irp);
return PoCallDriver(dx->NextStackDevice, Irp);
}
#pragma code_seg()// end PAGE section
Listing 4.10 Dispatch.cpp
///////////////////////////////////////////////////////////////////////
//Copyright © 1998 Chris Cant, PHD Computer Consultants Ltd
//WDM Book for R&D Books, Miller Freeman Inc
//
//Wdm1 example
///////////////////////////////////////////////////////////////////////
//dispatch.cpp:Other IRP handlers
///////////////////////////////////////////////////////////////////////
//Wdm1CreateHandle Create/Open file IRP
//Wdm1CloseHandle Close file IRPs
//Wdm1ReadHandle Read IRPs
//Wdm1WriteHandle Write IRPs
//Wdm1DeviceControlHandle DeviceIoControl IRPs
//Wdm1SystemControlHandle WMI IRPs
///////////////////////////////////////////////////////////////////////
//Version history
//27-Apr-991.0.0CCcreation
///////////////////////////////////////////////////////////////////////
#include "wdm1.h"
#include "Ioctl.h"
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//Buffer and BufferSize and guarding spin lock globals (in unpaged memory)
KSPIN_LOCK BufferLock;
PUCHARBuffer = NULL;
ULONGBufferSize = 0;
///////////////////////////////////////////////////////////////////////
//Wdm1Create:
//
//Description:
//Handle IRP_MJ_CREATE requests
//
//Arguments:
//Pointer to our FDO
//Pointer to the IRP
//IrpStack->Parameters.Create.xxx has create parameters
//IrpStack->FileObject->FileName has file name of device
//
//Return Value:
//This function returns STATUS_XXX
NTSTATUS Wdm1Create(IN PDEVICE_OBJECT fdo, IN PIRP Irp) {
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
DebugPrint("Create File is %T", &(IrpStack->FileObject->FileName);
// Complete successfully
return CompleteIrp(Irp,STATUS_SUCCESS,0);
Читать дальше