Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
2
Добавлен:
01.05.2025
Размер:
5 Мб
Скачать
☆

14.2 The Component Object Model

Most APIs in the DirectX SDK are composed of objects and interfaces based on the COM. The COM is a foundation for an object-based system that focuses on reuse of interfaces, and it is the model at the heart of OLE programming. It is also an interface specification from which any number of interfaces can be built. It is an object model at the operating-system level.

Many DirectX APIs are instantiated as a set of OLE objects. You can consider an object to be a black box that represents the hardware and requires communication with applications through an interface. The commands sent to and from the object through the COM interface are called methods. For example, the IDirectDraw2::GetDisplayMode method is sent through the IDirectDraw2 interface to get the current display mode of the display adapter from the DirectDraw object.

Objects can bind to other objects at run time, and they can use the implementation of interfaces provided by the other object. If you know an object is an OLE object, and if you know which interfaces that object supports, your application (or another object) can determine which services the first object can perform. One of the methods all OLE objects inherit, the QueryInterface method, lets you determine which interfaces an object supports and creates pointers to these interfaces.

IUnknown Interface

All COM interfaces are derived from an interface called IUnknown. This interface provides DirectX with control of the object's lifetime and the ability to navigate multiple interfaces. IUnknown has three methods:

  • AddRef, which increments the object's reference count by 1 when an interface or another application binds itself to the object.

  • QueryInterface, which queries the object about the features it supports by requesting pointers to a specific interface.

  • Release, which decrements the object's reference count by 1. When the count reaches 0, the object is deallocated.

AddRef and Release maintain an object's reference count. For example, if you create a DirectDrawSurface object, the object's reference count is set to 1. Every time a function returns a pointer to an interface for that object, the function then must call AddRef through that pointer to increment the reference count. You must match each AddRef call with a call to Release. Before the pointer can be destroyed, you must call Release through that pointer. After an object's reference count reaches 0, the object is destroyed and all interfaces to it become invalid.

QueryInterface determines whether an object supports a specific interface. If an object supports an interface, QueryInterface returns a pointer to that interface. You then can use the methods contained in that interface to communicate with the object. If QueryInterface successfully returns a pointer to an interface, it implicitly calls AddRef to increment the reference count, so your application must call Release to decrement the reference count before destroying the pointer to the interface.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]