#pragma INITCODE
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,IN PUNICODE_STRING pRegistryPath)
{
pDriverObject->DriverExtension->AddDevice = HelloWDMAddDevice;
pDriverObject->MajorFunction[IRP_MJ_PNP] = HelloWDMPnp;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
pDriverObject->MajorFunction[IRP_MJ_CREATE] =
pDriverObject->MajorFunction[IRP_MJ_READ] =
pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloWDMDispatchRoutine;
pDriverObject->DriverUnload = HelloWDMUnload;
return STATUS_SUCCESS;
}
NTSTATUS HelloWDMAddDeviceCompletion(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp,IN PVOID Context)
{
// return STATUS_SUCCESS;//这个会蓝屏。
return STATUS_MORE_PROCESSING_REQUIRED;
}
#pragma PAGEDCODE
NTSTATUS HelloWDMAddDevice(IN PDRIVER_OBJECT DriverObject,IN PDEVICE_OBJECT PhysicalDeviceObject)
{ //PhysicalDeviceObject这个设备是在电脑->控制面板中添加的新硬件,并不是真实存在的。
//在这个函数中,我想得到一些关于PhysicalDeviceObject这个硬件设备的一些信息。
//如:DevicePropertyEnumeratorName ,Hardware IDs 。这些可以通过IoGetDeviceProperty函数得到。
//但是:Device Instance IDs。The format of this string consists of an instance ID concatenated to a device ID, as follows:
//<device-ID>\<instance-specific-ID>
//Device IDs 与Instance IDs 按MSDN帮助上面的得发送IRP。
//下面为我自己写的。自己创建了一个IRP。
PIO_STACK_LOCATION nextStack;
KEVENT event;//这个用来传给完成实例的。
KEVENT eventForIrp;
KeInitializeEvent(&eventForIrp, NotificationEvent, FALSE);
KeInitializeEvent(&event, NotificationEvent, FALSE);
PIRP pNewIrp=IoAllocateIrp(PhysicalDeviceObject->StackSize,FALSE);
pNewIrp->UserEvent=&eventForIrp;
IO_STATUS_BLOCK status_block;
pNewIrp->UserIosb=&status_block;
pNewIrp->Tail.Overlay.Thread = PsGetCurrentThread();
pNewIrp->AssociatedIrp.SystemBuffer=NULL;
nextStack=IoGetNextIrpStackLocation(pNewIrp); //这个或下面的都试了。
// nextStack=IoGetCurrentIrpStackLocation(pNewIrp);
nextStack->MajorFunction = IRP_MJ_PNP;
nextStack->MinorFunction = IRP_MN_QUERY_ID;
nextStack->Parameters.QueryId.IdType =BusQueryDeviceID;
//设置IRP完成例程,如果不设置,到下面的IoCallDriver函数,马上蓝屏。
IoSetCompletionRoutine(pNewIrp,HelloWDMAddDeviceCompletion,&event,TRUE,TRUE,TRUE);
pNewIrp->IoStatus.Status=STATUS_NOT_SUPPORTED;
status=IoCallDriver(PhysicalDeviceObject,pNewIrp);//调用IoCallDriver;
if (status == STATUS_PENDING)
{
KdPrint(("IoCallDriver return STATUS_PENDING,Waiting ...\n"));
// KeWaitForSingleObject(&event,Executive,KernelMode ,FALSE,NULL);
KdPrint(("KeWaitForSingleObject flish\n"));
}
else if (NT_SUCCESS(status))
{KdPrint(("IoCallDriver NT_SUCCESS return,pNewIrp->IoStatus.Information=%d\n",pNewIrp->IoStatus.Information));}
else{KdPrint(("IoCallDriver return\n"));}
IoFreeIrp(pNewIrp);
//通过上面的方法,我并没有得到Device IDs。求高手。要怎么样才能得到Device IDs 与Instance IDs。
return STATUS_SUCCESS;
}