www.pudn.com > windows2000XP_WDM_DeviceDriverDevelopment_WuAnHe_C > CharFilterDevice.cpp


// CharFilterDevice.cpp 
// Implementation of CharFilterDevice device class 
// 
// Generated by DriverWizard version DriverStudio 3.1.0 (Build 1722) 
// Requires Compuware's DriverWorks classes 
// 
 
#include  
#include  
#include "CharFilterDevice.h" 
 
#include "CharSampleioctl.h" 
 
//////////////////////////////////////////////////////////////////////// 
//  CharFilterDevice::CharFilterDevice 
// 
//	Routine Description: 
//		This is the constructor for the Filter Device Object. 
//		It is derived from KWdmFilterDevice, which builds in automatic 
//	    dispatching of subfunctions of IRP_MJ_POWER and IRP_MJ_PNP to 
//		virtual member functions. 
// 
//	Parameters: 
//		Pdo - Physical Device Object - this is a pointer to a system 
//			device object that represents the physical device. 
// 
//		Unit - Unit number. This is a number to append to the device's 
//			base device name to form the Logical Device Object's name 
// 
//	Return Value: 
//		None    
// 
//	Comments: 
//		The object being constructed contains a data member (m_Lower) of type 
//		KPnpLowerDevice. By initializing it, the driver binds the Filter  
//		Device Object to the device stack. 
// 
CharFilterDevice::CharFilterDevice(PDEVICE_OBJECT Pdo, ULONG Unit) : 
	KWdmFilterDevice(Pdo, NULL)	//其继承基类是 KWdmFilterDevice 
{ 
 
	// Check constructor status 
    if ( !NT_SUCCESS(m_ConstructorStatus) ) 
	{ 
	    return; 
	} 
 
	// Remember our unit number 
	m_Unit = Unit; 
 
	// Initialize the lower device 
	m_Lower.Initialize(this, Pdo); 
 
	// Attach the filter 
	NTSTATUS status = AttachFilter(&m_Lower);  //将过滤器设备对象附着到设备堆栈上 
 
	// Check the status 
	if ( !NT_SUCCESS(status) ) 
	{ 
		m_ConstructorStatus = status; 
		return; 
	} 
 
	// Initialize the Filter Power Policy settings  
	SetFilterPowerPolicy();		//过滤器驱动程序的PnP政策设置函数 
 
	// Initialize the Filter PnP Policy settings  
	SetFilterPnpPolicy();		//过滤器驱动程序的电源管理政策设置函数 
} 
 
 
//////////////////////////////////////////////////////////////////////// 
//  CharFilterDevice::DeviceControl 
// 
//	Routine Description: 
//		Handler for IRP_MJ_DEVICE_CONTROL 
// 
//	Parameters: 
//		I - Current IRP 
//  
//	Return Value: 
//		None 
// 
//	Comments: 
//		This implementation simply forwards the IRP to the lower device. 
// 
NTSTATUS CharFilterDevice::DeviceControl(KIrp I)	 
{	//DeviceControl IRP过滤例程 
	switch (I.IoctlCode()) 
	{ 
		case CHARSAMPLE_IOCTL_800: 
			nin=I.IoctlInputBufferSize();	//要转换的数字数目,如3个数字 
			cin=new (NonPagedPool) CHAR[nin];//申请一个内存,用于存放数字 
			if ( cin == NULL ) 
			{ 
				return STATUS_INSUFFICIENT_RESOURCES; 
			} 
			nout=I.IoctlOutputBufferSize();	//输出缓冲区大小,如6个字节 
			cout=new (NonPagedPool) CHAR[nout]; //申请一个内存,用于存放中文 
			if ( cout == NULL ) 
			{ 
				delete cin; 
				return STATUS_INSUFFICIENT_RESOURCES; 
			} 
			strncpy(cin,(PCHAR)I.IoctlBuffer(),nin);//将数字字符拷贝到cin中 
			return PassThrough(I, LinkTo(IrpCompletionRoutine), this); 
			//将IRP传递下去,同时链接一个IRP完成例程。 
		default: 
			// Unrecognized IOCTL request 
			return PassThrough(I); 
	} 
} 
 
 
//////////////////////////////////////////////////////////////////////// 
//  CharFilterDevice::IrpCompletionRoutine 
// 
//	Routine Description: 
//		Completion Handler for IRPs 
// 
//	Parameters: 
//		I - IRP just completed by lower driver 
// 
//	Parameters: 
//		NTSTATUS - STATUS_SUCCESS 
// 
//	Comments: 
//		This routine is called when the lower driver completes the request 
// 
NTSTATUS CharFilterDevice::IrpCompletionRoutine(KIrp I) 
{	//DeviceControl IRP完成例程 
	strncpy(cout,(PCHAR)I.IoctlBuffer(),2);	//将转换后的中文字符拷贝到cout中 
	cout += 2;	//调整cout指针,+2是因为一个中文占2个字节空间 
	nin  -= 1;	//完成一个转换后,nin便减1 
 	if (nin == 0) 
	{	// nin减至0,表示转换工作全部完成 
		cout -= nout;	//调整cout指针,指向缓冲区的头 
		strncpy((PCHAR)I.IoctlBuffer(),cout,nout);  
		//将转换后的全部中文字符拷贝到应用程序的缓冲区中 
		I.Information() = nout; 
		if (cout) delete cout;	//删除cout 
		cin -= (nout - 2)/2; 	//调整cin指针,指向缓冲区的头 
		if (cin) delete cin; 	//删除cin 
		return I.Status();		//完成IRP 
	} 
	else 
	{ 
		cin++;	//调整cin指针,指向下一个数字 
		strncpy((PCHAR)I.IoctlBuffer(),cin,1); 
		//将下一个数字拷贝到应用程序的缓冲区中 
		PassThrough(I, LinkTo(IrpCompletionRoutine), this); 
		//将IRP再次传递下去,同时链接IRP完成例程。 
		return STATUS_MORE_PROCESSING_REQUIRED;	//表示IRP尚未完成 
	} 
}