以文件句柄调用其它驱动程序

这篇博文是 以文件句柄形式调用其它驱动程序

驱动程序调用驱动程序的方式有很多..

本篇博文是 同步调用  ,其它方式迟一点再讨论


先上效果图


接下来的几篇博文都需要一个被测试的驱动...先准备这个



#define DEVICE_NAME			"\\Device\\DRIVERTEST_DeviceName"
#define SYMLINK_NAME		"\\DosDevices\\DRIVERTEST_DeviceName"
PRESET_UNICODE_STRING(usDeviceName, DEVICE_NAME);
PRESET_UNICODE_STRING(usSymlinkName, SYMLINK_NAME);

typedef struct{

	IRP *currentPendingIRP;
	KTIMER kTimer;
	KDPC  Dpc;
}DEVICE_EXTENSION;
	KdPrint(("被测试的驱动  DriverEntry   hahah  ternsoft.com"));
	DEVICE_EXTENSION* pDex=(DEVICE_EXTENSION*)pdoDeviceObj->DeviceExtension;
	KeInitializeTimer(&pDex->kTimer);
	KeInitializeDpc(&pDex->Dpc,CustomDpc,pdoDeviceObj);
在DriverEntry初始化一些设置



NTSTATUS DRIVERTEST_DispatchRead(
								 IN PDEVICE_OBJECT		DeviceObject,
								 IN PIRP					Irp
								 )
{
	KdPrint(("DRIVERTEST_DispatchRead"));
// 	NTSTATUS status = STATUS_SUCCESS;
// 	Irp->IoStatus.Status = status;
// 	Irp->IoStatus.Information = 0;
// 	IoCompleteRequest(Irp, IO_NO_INCREMENT);
// 	return status;
	
	DEVICE_EXTENSION *pDex=(DEVICE_EXTENSION*)DeviceObject->DeviceExtension;
	pDex->currentPendingIRP=Irp;
	LARGE_INTEGER liTimeout=RtlConvertLongToLargeInteger(-10*3000000);//3秒
	IoMarkIrpPending(Irp);//将IRP设置为挂起
	KeSetTimer(&pDex->kTimer,liTimeout,&pDex->Dpc);	
	return STATUS_PENDING;

}
VOID
CustomDpc(
		  IN struct _KDPC  *Dpc,
		  IN PVOID  DeferredContext,
		  IN PVOID  SystemArgument1,
		  IN PVOID  SystemArgument2
		  ){

			  KdPrint(("CustomDpc"));

			  DEVICE_OBJECT *pDevObj=(DEVICE_OBJECT*)DeferredContext;
			  DEVICE_EXTENSION *pDex=(DEVICE_EXTENSION*)pDevObj->DeviceExtension;
			  IRP *pIrp=pDex->currentPendingIRP;
			  KdPrint(("完成IRP_MG_READ irp"));
			  pIrp->IoStatus.Status=STATUS_SUCCESS;
			  pIrp->IoStatus.Information=0;
			  IoCompleteRequest(pIrp,IO_NO_INCREMENT);

}
上面那些是被测试驱动程序的部分,,,下面是测试程序A的部分



	KdPrint(("开始读"));
	HANDLE hDevice;
	OBJECT_ATTRIBUTES  ObjectAttributes;
	UNICODE_STRING ustrDeviceName;
	RtlInitUnicodeString(&ustrDeviceName,L"\\Device\\DRIVERTEST_DeviceName");
	InitializeObjectAttributes(&ObjectAttributes,&ustrDeviceName,OBJ_CASE_INSENSITIVE,NULL,NULL);
	IO_STATUS_BLOCK  IoStatusBlock;
	NTSTATUS status2=ZwCreateFile(&hDevice,GENERIC_ALL|SYNCHRONIZE,&ObjectAttributes,&IoStatusBlock,NULL,FILE_ATTRIBUTE_NORMAL,FILE_SHARE_READ,FILE_OPEN_IF,FILE_SYNCHRONOUS_IO_NONALERT,NULL,0);
	if (NT_SUCCESS(status2))
	{
		ZwReadFile(hDevice,NULL,NULL,	NULL,&IoStatusBlock,NULL,0,NULL,NULL);
	}
	ZwClose(hDevice);
	KdPrint(("读结束"));
注意,要先运行被测试程序,再运行测试程序A


评论
文章信息
作者: admin
目录: driver
创建: 2014-03-29
更新: 2014-03-29
如果文章对你有帮助,请博主喝杯咖啡:-)