首页
社区
课程
招聘
[求助]按寒江独钓上面写的过滤驱动没效果
发表于: 2013-9-6 10:01 5926

[求助]按寒江独钓上面写的过滤驱动没效果

2013-9-6 10:01
5926
---------------CODE-------------
 #ifdef __cplusplus
extern "C" {
#endif
	
#include "ntddk.h"
#include "ntstrsafe.h"
#ifdef __cplusplus
}
#endif

#define MAX_COMCOUNT 20
static PDEVICE_OBJECT s_fltobj[MAX_COMCOUNT]={0};
static PDEVICE_OBJECT s_oldobj[MAX_COMCOUNT]={0};



NTSTATUS MyAttachDevice(PDRIVER_OBJECT pDriver_Object,   //驱动对象
						PDEVICE_OBJECT oldobj,        //原来的设备
						PDEVICE_OBJECT *fltobj,       //过滤设备
						PDEVICE_OBJECT *next)         //下一个设备
{
	NTSTATUS status;
	PDEVICE_OBJECT topDev=NULL;
	status=IoCreateDevice(pDriver_Object,
		                  0,
						  NULL,
						  oldobj->DeviceType,
						  0,
						  FALSE,
						  fltobj);
	if(status!=STATUS_SUCCESS)
		return status;

	if(oldobj->Flags & DO_BUFFERED_IO)
		(*fltobj)->Flags |= DO_BUFFERED_IO;

	if(oldobj->Flags & DO_DIRECT_IO)
		(*fltobj)->Flags |= DO_DIRECT_IO;

	if(oldobj->Characteristics & FILE_DEVICE_SECURE_OPEN)
		(*fltobj)->Characteristics |= FILE_DEVICE_SECURE_OPEN;
	(*fltobj)->Flags|=DO_POWER_PAGABLE;

	topDev=IoAttachDeviceToDeviceStack(*fltobj,oldobj);
	if(topDev!=NULL)
	{
		IoDeleteDevice(*fltobj);
		*fltobj=NULL;
		status=STATUS_UNSUCCESSFUL;
		return status;
	}
	*next=topDev;
	
	(*fltobj)->Flags=(*fltobj)->Flags & ~ DO_DEVICE_INITIALIZING;
	return STATUS_SUCCESS;
}

PDEVICE_OBJECT MyGetComDev(ULONG id/*串口ID*/,NTSTATUS * status)
{
	UNICODE_STRING szComName;
	static WCHAR name[32]={0};
	PFILE_OBJECT fileobj=NULL;
	PDEVICE_OBJECT devobj=NULL;

	memset(name,0,sizeof(WCHAR)*32);

	RtlStringCchPrintfW(name,32,L"\\Deivce\\Serial%d",id);  //打印id到字符串
  
	RtlInitUnicodeString(&szComName,name);     //初始化Uincode_String变量

	*status=IoGetDeviceObjectPointer(&szComName,FILE_ALL_ACCESS,&fileobj,&devobj);
	if(*status==STATUS_SUCCESS)
	{
		ObDereferenceObject(fileobj);
	}
	return devobj;
}

void MyAttachAllDevice(PDRIVER_OBJECT pDriver_Object)
{
	ULONG i;
	NTSTATUS status;
    PDEVICE_OBJECT comdev;
	for(i=0;i<MAX_COMCOUNT;i++)
	{
		comdev=MyGetComDev(i,&status);
		if(comdev==NULL)
			continue;
		MyAttachDevice(pDriver_Object,comdev,&s_fltobj[i],&s_oldobj[i]);
	}
}

void MyDeatchAllMyDevice(PDRIVER_OBJECT pDriver_Object)
{

}
NTSTATUS MyDispatch(PDEVICE_OBJECT pDevice_Objcet,PIRP pIrp)
{
	NTSTATUS status;
	PIO_STACK_LOCATION io_stack;
	io_stack=IoGetCurrentIrpStackLocation(pIrp);
	for(int i=0;i<MAX_COMCOUNT;i++)
	{
		if(s_fltobj[i]==pDevice_Objcet)
		{
			if(io_stack->MajorFunction==IRP_MJ_POWER)
			{
				PoStartNextPowerIrp(pIrp);
				IoSkipCurrentIrpStackLocation(pIrp);
				return PoCallDriver(s_fltobj[i],pIrp);
			}
			if(io_stack->MajorFunction==IRP_MJ_WRITE)
			{
				ULONG Len=io_stack->Parameters.Write.Length; //获取WriteIRp的长度
				PUCHAR buf=NULL;
				if(pIrp->MdlAddress!=NULL)
				{
					buf=(PUCHAR)MmGetSystemAddressForMdlSafe(pIrp->MdlAddress,NormalPagePriority);
				}
				else
				buf=(PUCHAR)pIrp->UserBuffer;
				if(buf==NULL)
					buf=(PUCHAR)pIrp->AssociatedIrp.SystemBuffer;
				for(int j=0;i<Len;j++)
				{
					if(j==10)
						DbgPrint("Comcap:Send Data:\r\n");
					DbgPrint("%2x",buf[j]);
				}
			}
			IoSkipCurrentIrpStackLocation(pIrp);
			return PoCallDriver(s_oldobj[i],pIrp);
		}
		pIrp->IoStatus.Information=0; //表示更改长度
		pIrp->IoStatus.Status=STATUS_INVALID_PARAMETER;
		IoCompleteRequest(pIrp,IO_NO_INCREMENT);
		return STATUS_SUCCESS;
	}
}

#define DELAY_ONE_MICROSECOND -10
#define DELAY_ONE_MILLISECOND DELAY_ONE_MICROSECOND*1000
#define DELAY_ONE_SECOND DELAY_ONE_MILLISECOND*1000
extern "C" void UnLoad(PDRIVER_OBJECT pDriverObject)
{
	LARGE_INTEGER interval;
	for(int i=0;i<MAX_COMCOUNT;i++)
	{
		if(s_oldobj[i]!=NULL)
			IoDetachDevice(s_oldobj[i]);
		interval.QuadPart=(5*DELAY_ONE_MILLISECOND);
		KeDelayExecutionThread(KernelMode,FALSE,&interval);
		for(int j=0;j<MAX_COMCOUNT;j++)
		{
			if(s_fltobj[j]!=NULL)
				IoDeleteDevice(s_fltobj[j]);
		}

	}
}
extern "C" NTSTATUS DriverEntry(PDRIVER_OBJECT pDriver_Object,PUNICODE_STRING pPath)
{
	for(int i=0;i<MAX_COMCOUNT;i++)
	{
		pDriver_Object->MajorFunction[i]=MyDispatch;
	}
	MyAttachAllDevice(pDriver_Object);
	pDriver_Object->DriverUnload=UnLoad;
	return STATUS_SUCCESS;
}


用超级终端,,DbgView没任何反应,DbgView可以接收到其它程序的输出

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (6)
雪    币: 1906
活跃值: (712)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
小白路过,帮顶一个
2013-9-6 10:22
0
雪    币: 110
活跃值: (527)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
3
谦虚了吧
2013-9-6 10:46
0
雪    币: 167
活跃值: (68)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
这里的判断语句太粗心了吧
  if(topDev!=NULL)  //成功就删除设备返回了
2013-9-12 07:48
0
雪    币: 110
活跃值: (527)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
5
[QUOTE=kaykay;1220738]这里的判断语句太粗心了吧
  if(topDev!=NULL)  //成功就删除设备返回了[/QUOTE]

额,好像是有问题,,我明天看下。。谢谢
2013-9-12 19:50
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
代码先复制
2013-9-12 23:10
0
雪    币: 8
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
串口过滤吗,我在虚拟机上面尝试没效果,物理机上就有
2013-9-12 23:29
0
游客
登录 | 注册 方可回帖
返回
//