#pragma INITCODE
extern "C" NTSTATUS DriverEntry (
INPDRIVER_OBJECT pDriverObject,
INPUNICODE_STRING pRegistryPath )
{
NTSTATUS ntStatus;
KdPrint(("DriverB:Enter B DriverEntry\n"));
//注册其他驱动调用函数入口
pDriverObject->DriverUnload =HelloDDKUnload;
pDriverObject->MajorFunction[IRP_MJ_CREATE]= HelloDDKCreate;
pDriverObject->MajorFunction[IRP_MJ_CLOSE]= HelloDDKClose;
pDriverObject->MajorFunction[IRP_MJ_WRITE]= HelloDDKDispatchRoutine;
pDriverObject->MajorFunction[IRP_MJ_READ]= HelloDDKRead;
UNICODE_STRING DeviceName;
RtlInitUnicodeString(&DeviceName, L"\\Device\\MyDDKDeviceA" );
PDEVICE_OBJECT DeviceObject = NULL;
PFILE_OBJECT FileObject = NULL;
//寻找DriverA创建的设备对象
ntStatus =IoGetDeviceObjectPointer(&DeviceName,FILE_ALL_ACCESS,&FileObject,&DeviceObject);
if (!NT_SUCCESS(ntStatus))
{
KdPrint(("DriverB:IoGetDeviceObjectPointer()0x%x\n", ntStatus ));
return ntStatus;
}
//创建自己的驱动设备对象
ntStatus = CreateDevice(pDriverObject);
if ( !NT_SUCCESS( ntStatus ) )
{
ObDereferenceObject( FileObject);
DbgPrint( "IoCreateDevice()0x%x!\n", ntStatus );
return ntStatus;
}
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)pDriverObject->DeviceObject->DeviceExtension;
PDEVICE_OBJECT FilterDeviceObject =pdx->pDevice;
//将自己的设备对象挂载在DriverA的设备对象上
PDEVICE_OBJECT TargetDevice =IoAttachDeviceToDeviceStack( FilterDeviceObject,
DeviceObject );
//将底层设备对象记录下来
pdx->TargetDevice =TargetDevice;
if ( !TargetDevice )
{
ObDereferenceObject( FileObject);
IoDeleteDevice(FilterDeviceObject );
DbgPrint("IoAttachDeviceToDeviceStack() 0x%x!\n", ntStatus );
returnSTATUS_INSUFFICIENT_RESOURCES;
}
FilterDeviceObject->DeviceType= TargetDevice->DeviceType;
FilterDeviceObject->Characteristics= TargetDevice->Characteristics;
FilterDeviceObject->Flags&= ~DO_DEVICE_INITIALIZING;
FilterDeviceObject->Flags |= (TargetDevice->Flags & ( DO_DIRECT_IO|
DO_BUFFERED_IO ) );
ObDereferenceObject( FileObject );
KdPrint(("DriverB:B attached Asuccessfully!\n"));
KdPrint(("DriverB:Leave B DriverEntry\n"));
return ntStatus;
}
#pragma PAGEDCODE
NTSTATUS HelloDDKRead(IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp)
{
KdPrint(("DriverB:Enter BHelloDDKCreate\n"));
NTSTATUS ntStatus = STATUS_SUCCESS;
//将自己完成IRP,改成由底层驱动负责
PDEVICE_EXTENSION pdx =(PDEVICE_EXTENSION)pDevObj->DeviceExtension;
//调用底层驱动
IoSkipCurrentIrpStackLocation (pIrp);
ntStatus= IoCallDriver(pdx->TargetDevice, pIrp);
KdPrint(("DriverB:Leave BHelloDDKCreate\n"));
return ntStatus;
}
VOID
GetDeviceObjectInfo( PDEVICE_OBJECT DevObj )
{
POBJECT_HEADER ObjectHeader;
POBJECT_HEADER_NAME_INFO ObjectNameInfo;
if ( DevObj == NULL )
{
DbgPrint("DevObj is NULL!\n" );
return;
}
// 得到对象头
ObjectHeader = OBJECT_TO_OBJECT_HEADER( DevObj);
if ( ObjectHeader )
{
//查询设备名称并打印
ObjectNameInfo = OBJECT_HEADER_TO_NAME_INFO( ObjectHeader );
if (ObjectNameInfo &&ObjectNameInfo->Name.Buffer )
{
DbgPrint( "Driver Name:%wZ - Device Name:%wZ - Driver Address:0x%x- Device Address:0x%x\n",
&DevObj->DriverObject->DriverName,
&ObjectNameInfo->Name,
DevObj->DriverObject,
DevObj );
}
//对于没有名称的设备,则打印 NULL
else if (DevObj->DriverObject )
{
DbgPrint( "Driver Name:%wZ - Device Name:%S - Driver Address:0x%x -Device Address:0x%x\n",
&DevObj->DriverObject->DriverName,
L"NULL",
DevObj->DriverObject,
DevObj );
}
}
}
VOID
GetAttachedDeviceInfo( PDEVICE_OBJECT DevObj )
{
PDEVICE_OBJECT DeviceObject;
if ( DevObj == NULL )
{
DbgPrint("DevObj is NULL!\n" );
return;
}
DeviceObject =DevObj->AttachedDevice;
while ( DeviceObject )
{
DbgPrint("Attached Driver Name:%wZ,Attached Driver Address:0x%x,AttachedDeviceAddress:0x%x\n",
&DeviceObject->DriverObject->DriverName,
DeviceObject->DriverObject,
DeviceObject );
DeviceObject = DeviceObject->AttachedDevice;
}
}
PDRIVER_OBJECT
EnumDeviceStack( PWSTR pwszDeviceName )
{
UNICODE_STRING DriverName;
PDRIVER_OBJECT DriverObject = NULL;
PDEVICE_OBJECT DeviceObject = NULL;
RtlInitUnicodeString(&DriverName, pwszDeviceName );
ObReferenceObjectByName(&DriverName,
OBJ_CASE_INSENSITIVE,
NULL,
0,
( POBJECT_TYPE ) IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&DriverObject );
if ( DriverObject == NULL )
{
returnNULL;
}
DeviceObject =DriverObject->DeviceObject;
while ( DeviceObject )
{
GetDeviceObjectInfo( DeviceObject );
//判断当前设备上是否有过滤驱动(Filter Driver)
if (DeviceObject->AttachedDevice )
{
GetAttachedDeviceInfo( DeviceObject );
}
//进一步判断当前设备上 VPB 中的设备
if (DeviceObject->Vpb &&DeviceObject->Vpb->DeviceObject)
{
GetDeviceObjectInfo(DeviceObject->Vpb->DeviceObject);
if (DeviceObject->Vpb->DeviceObject->AttachedDevice)
{
GetAttachedDeviceInfo(DeviceObject->Vpb->DeviceObject);
}
}
//得到建立在此驱动上的下一个设备 DEVICE_OBJECT
DeviceObject= DeviceObject->NextDevice;
}