框架对象简介Introduction to Framework Objects

Windows 驱动程序框架(WDF)提供给驱动程序的接口是基于对象的接口。The interfaces that Windows Driver Frameworks (WDF) provides to drivers are object-based. 框架定义了多个对象。The framework defines several objects. 这些对象导出驱动程序可以访问的方法(函数)和属性(数据)。These objects export methods (functions) and properties (data) that drivers can access. 框架对象还可通过提供事件回调函数来启动事件,这些事件由驱动程序支持。Framework objects also initiate events, which drivers can support by providing event callback functions.

基于框架的驱动程序绝不会直接访问框架对象。Framework-based drivers never directly access framework objects. 相反,驱动程序通过句柄来引用对象,驱动程序将该对象作为输入传递给对象方法。Instead, drivers reference the objects by handles, which the driver passes as input to object methods.

所有框架对象具有以下特征:All framework objects have the following characteristics:

引用计数Reference count
框架维护对每个对象的引用数的计数。The framework maintains a count of the number of references to each object. 当框架创建对象时,它会将对象的引用计数设置为1。When the framework creates an object, it sets the object's reference count to one. 当框架完成使用对象时,它会递减引用计数。When the framework has finished using an object, it decrements the reference count. 在引用计数递减为零时,框架无法删除对象,因此,驱动程序可以通过增加对象的引用计数来阻止删除该对象。The framework cannot delete the object until the reference count is decremented to zero, so drivers can prevent the deletion of an object by incrementing its reference count.

上下文空间Context space
基于框架的驱动程序可以为驱动程序接收或创建的每个框架对象创建特定于对象的上下文空间。Framework-based drivers can create object-specific context space for every framework object that the driver receives or creates. 驱动程序应将所有特定于对象的数据存储在对象的上下文空间中。Drivers should store all object-specific data in an object's context space. 有关上下文空间的详细信息,请参阅框架对象上下文空间For more information about context space, see Framework Object Context Space.

删除回调函数Deletion callback functions
驱动程序可以注册框架在删除对象时调用的回调函数。Drivers can register callback functions that the framework calls when it is deleting an object. 回调函数可以删除驱动程序分配的资源,例如对象特定的内存分配。The callback functions can remove driver-assigned resources, such as object-specific memory allocations. 有关这些回调函数的详细信息,请参阅框架对象生命周期For more information about these callback functions, see Framework Object Life Cycle.

父对象Parent object
所有框架对象都可以有一个父对象。All framework objects can have a parent object. 框架为大多数对象指定默认的父对象。The framework designates a default parent object for most objects. 当驱动程序创建对象时,它可以指定重写对象的默认父对象的父对象。When a driver creates an object, it can designate a parent object that overrides the object's default parent object. 为了指定对象的父对象,驱动程序将对象的WDF_对象ParentObject成员设置_属性结构。To designate an object's parent object, drivers set the ParentObject member of the object's WDF_OBJECT_ATTRIBUTES structure. (对于几个对象类型,驱动程序不能覆盖默认的父对象。)当框架或驱动程序删除父对象时,框架还会删除父对象的子对象。(For a few object types, drivers cannot override the default parent object.) When the framework or a driver deletes a parent object, the framework also deletes the parent object's children.

有关 WDF 定义的所有对象的概述,请参阅框架对象的摘要For an overview of all of the objects that are defined by WDF, see Summary of Framework Objects.