chapter 5.4: 创建对象

来源:互联网 发布:c 网络验证源码 编辑:程序博客网 时间:2024/06/05 07:42
UMDF:先按需要创建callback object,再调用create方法
KMDF:先初始化object attributes structure和object configuration structure,再调用create方法

创建UMDF对象
UMDF对每种object提供了不同的创建方法:device, file, I/O request, memory, queue object,过程:
    1. 若driver对某个object支持callback,则新分配一个对应的callback object
    2. 创建对应的object,并关联callback object
对象的创建方法需要callback object的IUnknown interface来确定callback object支持那种callback interface
Fx2_Driver的ControlQueue.cpp的创建一个queue object的代码:
queue = new CMyControlQueue(Device); . . . //Code omittedIUnknown *callback = QueryIUnknown();hr = m_Device->GetFxDevice()->CreateIoQueue( callback,                                             Default,                                             DispatchType,                                             PowerManaged,                                             FALSE,                                             &fxQueue                                             );callback->Release();if (FAILED(hr))  {    . . . //Error handling omitted;    }fxQueue->Release();
    代码中create的参数:callback object interface指针,四个queue-specific参数,和一个指向变量的指针(接收IWDFIoQueue interface的地址)
    driver然后释放了由QueryIUnknown在IUnknown interface中获取的引用,这是由于framework device object已经维护了它的引用
    driver释放了CreateIoQueue使用的fxQueue object的引用,这是由于framework object tree已经维护了该对象的引用

创建KMDF对象
过程:
    1.按需要初始化configuration structure
    2.按需要初始化sttribute structure
    3.调用创建方法

KMDF object configuration structure
名称:WDF_[Object]_CONFIG, 维护驱动对该对象的event callback function指针,及其他内容
KMDF定义了WDF_[Object]_CONFIG_INIT来初始化configuration structure,但不是所有object都具有configuration以及init函数

KMDF Object Attribute structure
*不是所有的参数都能应用在所有对象上

Field

Description

ContextSizeOverride

Size of the context area, which overrides the value in ContextTypeInfo>ContextSize. This field is useful for context areas that have variable sizes.

ContextTypeInfo

Pointer to the type information for the object context area.

EvtCleanupCallback

Pointer to a callback routine that is invoked to clean up the object before it is deleted. The object might still have references.

EvtDestroyCallback

Pointer to a callback routine that is invoked when the reference count reaches zero for an object that is marked for deletion.

ExecutionLevel

Maximum IRQL at which KMDF can invoke certain object callbacks.

ParentObject

Handle for the object's parent.

Size

Size of the structure in bytes.

SynchronizationScope

Level at which certain callbacks for this object are synchronized. This field applies only to driver, device, and file objects.

名称:WDF_OBJECT_ATTRIBUTES 
函数:
    WDF_OBJECT_ATTRIBUTES_INIT:设置了关于同步和执行级别的变量(哪些callback可以同步调用,它们最高的IRQL级别,默认为父对象的配置)
    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(设置了object context area信息)
    WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(设置了object context area信息)
可以使用参数默认值:driver定义的WDF_NO_OBJECT_ATTRIBUTES,即WDF定义的NULL。若driver定义了context area,则必须设置参数

KMDF对象创建方法
命名:Wdf[Object]Create 返回handle
处理过程:
    1.为object和context area分配**nonpaged pool**
    2.初始化对象attributes
    3.为对象context area清零
    4.配置对象:保存event callback指针,及其他属性
若处理失败,framework会删除对象
示例代码:Osrusbfx2 driver的Device.c
NTSTATUS                       status = STATUS_SUCCESS;WDFDEVICE                      device;WDF_IO_QUEUE_CONFIG            ioQueueConfig;WDF_IO_QUEUE_CONFIG_INIT(&ioQueueConfig, WdfIoQueueDispatchSequential);ioQueueConfig.EvtIoRead = OsrFxEvtIoRead;ioQueueConfig.EvtIoStop = OsrFxEvtIoStop;status = WdfIoQueueCreate( device,                           &ioQueueConfig,                           WDF_NO_OBJECT_ATTRIBUTES,                           &queue // queue handle                         );if (!NT_SUCCESS (status)) {    . . . //Error handling omitted}
图解:
    代码创建了一个queue object
    1.通过WDF_IO_QUEUE_CONFIG_INIT初始化configuration structure,向它传递一个如何分配请求的参数。
    2.在configuration structure中注册event callback
    3.由于没有设置其他object attribute,所以不需要初始化attribute structure
    4.最终调用WdfIoQueueCreate创建对象。参数如下:
        1.queue相关的device handle
        2.指向初始化过的queue configuration structure指针
        3.WDF_NO_OBJECT_ATTRIBUTE常数,默认的参数。
        4.一个方法返回queue一个handle的location
要通过annotate callback function来帮助SDV进行编译分析
原创粉丝点击