minifilter 与用户态的通信

来源:互联网 发布:董小飒淘宝零食店 编辑:程序博客网 时间:2024/06/11 01:05

关于minifilter的一些资料

Windows驱动_文件系统微小过滤驱动之一初识MiniFilter

http://blog.csdn.net/z18_28_19/article/details/12979743

还有系列的资料通过上面的连接可以找到原文的一个系列。


FilterConnectCommunicationPort opens a new connection to a communication server port that is created by a file system minifilter.

HRESULT FilterConnectCommunicationPort(  _In_     LPCWSTR               lpPortName,  _In_     DWORD                 dwOptions,  _In_opt_ LPCVOID               lpContext,  _In_     WORD                  dwSizeOfContext,  _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,  _Out_    HANDLE                *hPort);

Parameters

lpPortName [in]

Pointer to a NULL-terminated wide-character string containing the fully qualified name of the communication server port (for example, L"\\MyFilterPort").

dwOptions [in]

Connection options for the communication port. Prior to Windows 8.1, this value is set to 0.

ValueMeaning
FLT_PORT_FLAG_SYNC_HANDLE

The handle returned in hPort is for synchronous I/O. This flag is available starting with Windows 8.1.

 

lpContext [in, optional]

Pointer to caller-supplied context information to be passed to the kernel-mode minifilter's connect notification routine. (See the ConnectNotifyCallback parameter in the reference page forFltCreateCommunicationPort.) This parameter is optional and can be NULL.

dwSizeOfContext [in]

Size, in bytes, of the structure that the lpContext parameter points to. If the value of lpContext is non-NULL, this parameter must be nonzero. If lpContext is NULL, this parameter must be zero.

lpSecurityAttributes [in, optional]

Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. For more information about the SECURITY_ATTRIBUTES structure, see the Microsoft Windows SDK documentation. This parameter is optional and can be NULL. If this parameter is NULL, the handle cannot be inherited.

hPort [out]

Pointer to a caller-allocated variable that receives a handle for the newly created connection port if the call to FilterConnectCommunicationPort succeeds; otherwise, it receives INVALID_HANDLE_VALUE.

Return value

FilterConnectCommunicationPort returns S_OK if successful. Otherwise, it returns an error value.

Remarks

FilterConnectCommunicationPort opens a connection to a minifilter's communication server port on behalf of a user-mode application. The application uses the resulting connection port handle to communicate with the minifilter.

After it successfully calls FilterConnectCommunicationPort, the application can send messages to the minifilter through the connection port by calling FilterSendMessage. It can also receive and reply to messages from the minifilter by calling FilterGetMessage and FilterReplyMessage, respectively. The connection port handle returned in the hPort parameter is passed as the first parameter toFilterSendMessageFilterGetMessage, and FilterReplyMessage.

Any handle that is obtained from FilterConnectCommunicationPort must eventually be released by calling CloseHandle.

Requirements

Target platform

Universal

Header

Fltuser.h (include Fltuser.h)

Library

FltLib.lib

DLL

FltLib.dll

See also

CloseHandle
FilterGetMessage
FilterSendMessage
FilterReplyMessage
FltBuildDefaultSecurityDescriptor
FltCloseClientPort
FltCloseCommunicationPort
FltCreateCommunicationPort
FltFreeSecurityDescriptor
SECURITY_ATTRIBUTES

minifilter 与用户态的通信

驱动层的步骤
1. 创建通信端口
  FltCreateCommunicationPort 
    对于安全对象,必须设置OBJ_KERNEL_HANDLE。
    ServerPort 监听客户端连接请求的端口。
    第三个参数ObjectAttributes 通过InitializeObjectAttributes初始化,其中包含了端口名称。方便应用层打开。
    ConnectNotifyCallback 用户态连接回调,这里对多个连接进行一些区别操作。
    比如ClientPort,表示用户态与内核建立的新连接的客户端端口句柄。

    minifilter必须把该句柄传递FltSendMessage之类的函数,作为第二个参数。

    与FltCreateCommunicationPort返回的ServerPort 不同。
    并且一般在DisconnectNotifyCallback 中调用FltCloseClientPort释放。

    DisconnectNotifyCallback 客户端所有连接端口中断,或者minifilter卸载时的回调。
    ***MessageNotifyCallback 用户态消息处理回调。
    用户态通过FilterReplyMessage发送的消息,都在这里处理。
2. 关闭通信端口
  FltCloseCommunicationPort
====================================================================
应用层的步骤
1. 创建连接
   FilterConnectCommunicationPort 打开一个新的通信服务器端口的连接。该微端口在驱动中创建。
     端口名类似L"\\MyFilterPort"
     应用程序通过返回的端口句柄与minifilter通信。
2. 发送数据
   FilterSendMessage 发送message给内核minifilter
     message发送到minifilter的消息通知回调函数中,在这里处理消息。

    这些回调函数在内核创建通信端口时指定  MessageNotifyCallback。
    该操作是同步的。调用者处于等待状态,直到消息被传递并收到minifilter的replay。

    当然如果希望有replay,那么outbuffer参数不能为空。
3. 接受数据
   FilterGetMessage 从minifilter取得一个message
     注意参数lpMessageBuffer,必须包含FILTER_MESSAGE_HEADER 结构。
     如果是同步操作,会一直等待直到收到消息。
     如果是异步操作,返回ERROR_IO_PENDING,通过重叠结构的事件来得知消息是否被传递。

   FilterReplyMessage
     注意参数lpReplyBuffer,必须包含FILTER_REPLY_HEADER 结构。

   ***特别注意,FltSendMessage 与FilterReplyMessage的buffersize,由于padding的缘故,需要精确指定大小
   typedef struct _REPLY_STRUCT
   {
          FILTER_REPLY_HEADERHeader;
          MY_STRUCTUREData;// The structure to be sent to the minifilter.
   } REPLY_STRUCT, *PREPLY_STRUCT;

   sizeof(REPLY_STRUCT) 可能比sizeof(FILTER_REPLY_HEADER) + sizeof(MY_STRUCT)大。
   所以建议使用后面的方式。


原创粉丝点击