miniFilter 内核层与应用程序通信

重点说下FltSendMessage() FilterGetMessage() 和 FilterReplyMessage() 这三个函数

 

首先:内核层的结构体

//  Defines the commands between the utility and the filter
typedef enum _NPMINI_COMMAND {
	ENUM_PASS = 0,
	ENUM_BLOCK
} NPMINI_COMMAND;	

//  Defines the command structure between the utility and the filter.
typedef struct _COMMAND_MESSAGE {
	NPMINI_COMMAND 	Command;  
	ULONG ulValue;
} COMMAND_MESSAGE, *PCOMMAND_MESSAGE;

 应用层通讯需要用到的结构体

 1 typedef struct _COMMAND_MESSAGE {
2 NPMINI_COMMAND Command;
3 ULONG ulValue;
4 } COMMAND_MESSAGE, *PCOMMAND_MESSAGE;
5
6
7 typedef struct _USERCOMMAND_MESSAGE {
8 FILTER_MESSAGE_HEADER messageHeader;
9 COMMAND_MESSAGE cmdMessage;
10 }USERCOMMAND_MESSAGE, *PUSERCOMMAND_MESSAGE;
11
12 typedef struct _USERCOMMAND_MESSAGE_REPLAY
13 {
14 FILTER_REPLY_HEADER replayHeader;
15 COMMAND_MESSAGE cmdMessage;
16
17 }USERCOMMAND_MESSAGE_REPLAY,*PUSERCOMMAND_MESSAGE_REPLAY;


内核里的调用

View Code
 1 //发送的数据
2 COMMAND_MESSAGE gCmdMessageSend;
3
4 gCmdMessageSend.Command=ENUM_PASS;
5 gCmdMessageSend.ulValue=100;
6
7 //接收的数据
8 COMMAND_MESSAGE gCmdMessageGet;
9
10 timeout.HighPart=10000000;
11
12
13 ULONG ulReplayLength=sizeof(COMMAND_MESSAGE);
14
15 status=FltSendMessage(gFilterHandle,&gClientPort,&gCmdMessageSend,sizeof(COMMAND_MESSAGE),&gCmdMessageGet,&ulReplayLength,NULL);
16
17
18
19 KdPrint(("after FltSendMessage %x,%d,%d \n",status,ulReplayLength,gCmdMessageGet.ulValue));

这里FltSendMessage必须等到FilterGetMessage和FilterReplyMessage全部返回正确才能够继续运行 否则挂起

应用层的调用

View Code
 1 USERCOMMAND_MESSAGE data;
2 USERCOMMAND_MESSAGE_REPLAY dataReplay;
3
4 while(TRUE)
5 {
6 data->cmdMessage.ulValue=10;
7 HRESULT hr= FilterGetMessage(g_hPort,(PFILTER_MESSAGE_HEADER)data,sizeof(USERCOMMAND_MESSAGE),NULL);
8
9
10 if (hr==S_OK)
11 {
12 CString str;
13 str.Format(_T("%d"),data->cmdMessage.ulValue);
14
15 dataReplay->replayHeader.MessageId=data->messageHeader.MessageId;
16
17 //返回数据
18 dataReplay->cmdMessage.ulValue=10;
19 hr=FilterReplyMessage(g_hPort,(PFILTER_REPLY_HEADER)dataReplay,sizeof(COMMAND_MESSAGE)+sizeof(FILTER_REPLY_HEADER));
20
21
22 }
23 }



posted @ 2012-03-15 11:53  ljinshuan  阅读(3778)  评论(0编辑  收藏  举报