请教,关于minifilter中StreamContext的用法
[复制链接]
我想在PostCreate中设置上下文,在PreRead/PostRead或PreWrite/PostWrite能得到该上下文.
顺序:
1.在FLT_CONTEXT_REGISTRATION中定义。如:
{ FLT_STREAM_CONTEXT,
0,
CleanupStreamContext,
sizeof(FILE_CONTEXT),
CONTEXT_TAG },
2.InstanceSetup中,没做FltAllocateContext和FltSetStreamContext
3.FltGetStreamContext函数,
Comments
FltGetStreamContext retrieves a context that was set for a file stream by a given minifilter driver instance.
FltGetStreamContext increments the reference count on the context that the Context parameter points to. When this context pointer is no longer needed, the caller must decrement its reference count by calling FltReleaseContext. Thus every successful call to FltGetStreamContext must be matched by a subsequent call to FltReleaseContext.
不好意思,英语不太好,FltGetStreamContext成功调用,是否需要FltReleaseContext或FltReferenceContext,因为别的函数可能需要用到它(看4或5)
复制代码
4.PostCreate根据需要跟踪的文件,设置FltAllocateContext及FltSetStreamContext。
我参照ctx例子,程序大概的意思如下:
typedef struct _FILE_CONTEXT
{
BOOLEAN EncryptFlagExist;
} FILE_CONTEXT, *PFILE_CONTEXT;
PFILE_CONTEXT fileCtxPtr ;
NTSTATUS status ;
status = FltGetStreamContext( FltObjects->Instance,
FltObjects->FileObject,
&streamContext );
if (!NT_SUCCESS(status)&&(status==STATUS_NOT_FOUND))
{
status = FltAllocateContext( Globals.Filter,
FLT_STREAM_CONTEXT,
sizeof(FILE_CONTEXT),
PagedPool,
&fileCtxPtr );
if (!NT_SUCCESS( status ))
{
KdPrint(("[Ctx]: Failed to create stream context"));
}
else
{
fileCtxPtr->EncryptFlagExist= IsEncrypted;
status=FltSetStreamContext (FltObjects->Instance,
FltObjects->FileObject,
FLT_SET_CONTEXT_REPLACE_IF_EXISTS,
fileCtxPtr ,
NULL);
if (!NT_SUCCESS(status)) FltReleaseContext( fileCtxPtr ); [color=#6666CC]//是否需要这句,因为在PreRead或PreWrite需要用到它[/color]
}
}
else
{
//FltReleaseContext( fileCtxPtr ); [color=#6666CC]//同样,是否需要这句,因为在PreRead或PreWrite需要用到它[/color]
}
复制代码
5.在PreRead中,程序如下:
PFILE_CONTEXT fileCtxPtr ;
NTSTATUS status ;
status = FltGetStreamContext( FltObjects->Instance,
FltObjects->FileObject,
&fileCtx );
if (!NT_SUCCESS(status)) {
KdPrint(("PreRead: Error getting stream context, status=%x\n",status) );
//需要FltReleaseContext(fileCtx)还是FltReferenceContext(fileCtx)吗?
return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
else
{
//使用fileCtx 的值
.....
*CompletionContext = fileCtx;
//之后需要FltReleaseContext(fileCtx)还是FltReferenceContext(fileCtx)吗?
return FLT_PREOP_SUCCESS_WITH_CALLBACK;
}
复制代码
6.在PreClose或PreCleanup中,清除该PFILE_CONTEXT如何做?
VOID CleanupFileContext(
__in PFLT_CONTEXT Context,
__in FLT_CONTEXT_TYPE ContextType
)
{
这样做吗?
PFILE_CONTEXT fileCtx=Context;
FltReleaseContext(fileCtx);
还是要
if (ContextType==FLT_STREAM_CONTEXT)
FltDeleteContext(Context);
}
复制代码