-
-
[求助]按寒江独钓上面写的过滤驱动没效果
-
发表于:
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可以接收到其它程序的输出
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!