Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 3 The Component Object Model and DirectX © 2008 Cengage Learning EMEA.

Similar presentations


Presentation on theme: "CHAPTER 3 The Component Object Model and DirectX © 2008 Cengage Learning EMEA."— Presentation transcript:

1 CHAPTER 3 The Component Object Model and DirectX © 2008 Cengage Learning EMEA

2 LEARNING OBJECTIVES In this chapter you will learn about: – –The Component Object Model – –COM objects and interfaces – –GUIDs and IIDs – –Writing a comprehensive COM program – –DirectX and the Component Object Model – –DirectX’s HRESULT value – –DirectX interface creation and queries

3 THE COMPONENT OBJECT MODEL The Component Object Model (COM) is an object-oriented programming model used for inter-object process interaction. Looking at COM from a higher level reveals it as a software paradigm revolving around the idea that different software components should be pluggable and interchangeable.

4 THE COMPONENT OBJECT MODEL COM is thus based on the idea of software componentry, that is, the idea that software components should be defined in an interchangeable manner. COM was developed so that objects could be created in a language neutral fashion. Language neutral objects have the benefit that they can be deployed in a vast array of environments.

5 THE COMPONENT OBJECT MODEL COM also permits the reuse of objects without any concern for the specific implementation of these objects. Components thus require properly defined, stand-alone interfaces that can be accessed independently from their implementations. COM objects are language neutral due to the objects themselves being in charge of their own creation and destruction.

6 THE COMPONENT OBJECT MODEL COM features a function, QueryInterface, which is responsible for casting between the different interfaces of an object – thus giving the client access to specified interfaces. This function is part of the basic interface all other interfaces are derived from – IUnknown.

7 THE COMPONENT OBJECT MODEL Another interesting feature of COM objects is that new features can be added to a COM object without the functionality of the old object being impaired in the process. Furthermore, COM objects can be replaced without the need to recompile the application program.

8 THE COMPONENT OBJECT MODEL COM was, before the advent of.NET, a major Microsoft software development platform and as a consequence a number of technologies were built on it. One such technology is the DirectX Application Programming Interface. All the DirectX components are built on top of the Component Object Model.

9 The COM Object and Interfaces A COM object can be described as either one or more C++ classes implementing a series of interfaces. Each interface is basically a collection of functions, and interfaces are used to communicate with COM objects. COM-utilizing components can be accessed via the functions contained in these interfaces – thus facilitating inter-process communication.

10

11 The COM Object and Interfaces COM object can contain any number of interfaces, with each interface defining a number of functions. COM components are required to implement the standard IUnknown interface (every COM interface is thus derived from IUnknown). The IUnknown interface can thus be described as the foundation of any interface being created.

12 The COM Object and Interfaces The IUnknown interface defines three functions: – –QueryInterface – –Release – –AddRef.

13 The COM Object and Interfaces COM interfaces are reflective, transitive and symmetric.

14 GUIDs and IIDs Each COM component can be accessed using one or more interfaces. Each interface associated with a component is uniquely identified via interface IDs (IIDs). These identifiers, called Globally Unique Identifiers (GUIDs), are unique 128-bit values used by the QueryInterface function to access the interface of a COM object.

15 GUIDs and IIDs

16 Defining a COM Object The COM object depicted in Figure 3-1 and 3-2 has four interfaces: IRenderer, I3DSound, IInput, and INetworking. We can implement these interfaces in the following manner (notice how each interface is derived from the IUnknown class):

17 Defining a COM Object /* the renderer interface */ struct IRenderer : IUnknown { virtual void InitGraphicsSystem(int renderer) = 0; virtual inter SetDisplaySize(int x, int y) = 0;...} /* the 3D audio interface */ struct I3DSound : IUnknown { virtual void InitSoundSystem(int device) = 0; virtual bool PlayAudio(int x, int y) = 0;...}

18 Defining a COM Object /* the input interface */ struct IInput : IUnknown { virtual void InitInputSystem(int controller) = 0; virtual bool HandleStickInput(float x, float y, float z)= 0;...} /* the network interface */ struct INetworking : IUnknown { virtual void InitNetworkingSystem(int nic) = 0; virtual bool Ping(int ip)= 0;...}

19 Defining a COM Object The COM class provides an implementation for each of the IUnknown interface functions as well as the above declared COM interfaces: class GameEngine: public IRenderer,I3DSound,IInput,INetworking { private: /* declare all the private types */

20 Defining a COM Object public: /* implement IUnknown's QueryInterface function */ virtual HRESULT __stdcall QueryInterface(const IID& iid, (void **) storage) { /* the actual implementation – must be reflective, transitive and symmetric */ } /* implement IUnknown's Addref function */ virtual ULONG __stdcall Addref() { /* the actual implementation – increase the interface reference count for each call */ reference count for each call */} //etc… [see textbook for the complete listing]

21 A Comprehensive COM Program #include #include #include //provides the set of constants used in COM applications using namespace std; /* GUIDs */ // {C2F7148C-6EA5-41b4-9FB6-B4BBA4C0488F} const GUID GUID_IFunctionOne = { 0xc2f7148c, 0x6ea5, 0x41b4, { 0x9f, 0xb6, 0xb4, 0xbb, 0xa4, 0xc0, 0x48, 0x8f } }; // {D7974908-331E-4f7c-BAB8-6711A9D1C0CC} const GUID GUID_IFunctionTwo = { 0xd7974908, 0x331e, 0x4f7c, { 0xba, 0xb8, 0x67, 0x11, 0xa9, 0xd1, 0xc0, 0xcc } };

22 A Comprehensive COM Program // {83926E15-FDF5-4fd0-B26D-A9F6E428B7A8} const GUID GUID_IFunctionThree = { 0x83926e15, 0xfdf5, 0x4fd0, { 0xb2, 0x6d, 0xa9, 0xf6, 0xe4, 0x28, 0xb7, 0xa8 } }; /* the IFunctionOne interface */ interface IFunctionOne: IUnknown { virtual void __stdcall functionOne(void) = 0; }; /* the IFunctionTwo interface */ interface IFunctionTwo: IUnknown { virtual void __stdcall functionTwo(void) = 0; }; /* the IFunctionThree interface */ interface IFunctionThree: IUnknown { virtual void __stdcall functionThree(void) = 0; };

23 A Comprehensive COM Program class COMObject: public IFunctionOne, IFunctionTwo, IFunctionThree {public: /* the COM object constructor */ COMObject() : reference_counter(0) {} /* the COM object destructor */ ~COMObject() {} private: /* declare IUnknown’s functions */ virtual HRESULT __stdcall QueryInterface(const GUID &guid, void **storage); virtualvoid __stdcall functionOne(void); virtual void __stdcall functionTwo(void); virtual void __stdcall functionThree(void); virtual ULONG __stdcall AddRef(); virtual ULONG __stdcall Release(); /* the reference counter */ int reference_counter; };

24 A Comprehensive COM Program void COMObject::functionOne(void){} void COMObject::functionTwo(void){} void COMObject::functionThree(void){} HRESULT __stdcall COMObject::QueryInterface(const GUID &guid, void **storage) { /* start by calling the IUnknown base interface with IDD_IUnknown, the ID of the IUnknown interface */ IDD_IUnknown, the ID of the IUnknown interface */ if(guid == IID_IUnknown) { /* cast the 'this' pointer to the IUnknown interface requested */ *storage = (IFunctionOne*)this; } /* the IFunctionOne interface has been requested */ if(guid == GUID_IFunctionOne) { /* cast the 'this' pointer to the IFunctionOne interface requested */ interface requested */ *storage = (IFunctionOne*)this; }

25 A Comprehensive COM Program /* the IFunctionTwo interface has been requested */ else if(guid == GUID_IFunctionTwo) { /* cast the 'this' pointer to the IFunctionTwo interface requested */ interface requested */ *storage = (IFunctionTwo*)this; } /* the IFunctionThree interface has been requested */ else if(guid == GUID_IFunctionThree) { /* cast the 'this' pointer to the IFunctionThree interface requested */ interface requested */ *storage = (IFunctionThree*)this; } /* the requested interface could not be found */ else{ *storage = NULL; /* QueryInterface should return the error E_NOINTERFACE if it fails */ E_NOINTERFACE if it fails */return(E_NOINTERFACE);}

26 A Comprehensive COM Program /* cast the pointer to IUnknown, subsequently calling the AddRef function so that the reference count can be AddRef function so that the reference count can be incremented */ incremented */ ((IUnknown *)(*storage))->AddRef(); /* QueryInterface should return NOERROR if it succeeds */ return(NOERROR);} ULONG __stdcall COMObject::AddRef() { /* increment the reference count */ reference_counter = reference_counter + 1; /* return this counter for debugging purposes */ return(reference_counter);}

27 A Comprehensive COM Program ULONG __stdcall COMObject::Release() { /* decrement the reference count */ reference_counter = reference_counter - 1; /* check whether the counter isn't ‘0’ */ if(reference_counter == 0) { /* the interface pointer is now invalid, so destroy it */ delete this; return 0; }else /* return this counter for debugging purposes */ return(reference_counter);}

28 A Comprehensive COM Program IUnknown *CoCreateInstance() { /* create the COM object using a pointer to any of our interfaces – used IFunctionOne in this case*/ interfaces – used IFunctionOne in this case*/ IUnknown *comObj = (IFunctionOne *)new(COMObject); /* increment the reference count */ comObj->AddRef(); /* return the create COM object */ return(comObj);}

29 A Comprehensive COM Program int main() { /* the main COM object */ IUnknown *mainCOMObject = CoCreateInstance(); IFunctionOne *pInterfaceOne = NULL; IFunctionTwo *pInterfaceTwo = NULL; IFunctionThree *pInterfaceThree = NULL; /* query all three interfaces */ mainCOMObject->QueryInterface(GUID_IFunctionOne, (void **)&pInterfaceOne); mainCOMObject->QueryInterface(GUID_IFunctionTwo, (void **)&pInterfaceOne); mainCOMObject->QueryInterface(GUID_IFunctionThree, (void **)&pInterfaceThree); /* release the interfaces */ pInterfaceOne->Release(); /* release the main COM object */ mainCOMObject->Release(); return 0; }

30 DIRECTX AND THE COMPONENT OBJECT MODEL DirectX is, at its most basic level, comprised of a number of COM objects. A DirectX function call indirectly creates a COM object, returning the interface of the object. – –This is done by passing the address of an interface pointer to the DirectX function.

31 DIRECTX AND THE COMPONENT OBJECT MODEL When creating an interface (ID3D10Device and ID3D10DepthStencilView in this case), we start by declaring a pointer to the interface ( notice the last parameter receiving the address of the ID3D10DepthStencilView interface pointer ): ID3D10Device* pID3D10Device; ID3D10DepthStencilView* pDepthStencilView; pID3D10Device->CreateDepthStencilView( pDepthStencilBuffer &depthstencilviewDescription, &pDepthStencilView);

32 DIRECTX AND THE COMPONENT OBJECT MODEL Values DirectX’s HRESULT Values DirectX Interface Creation and Queries


Download ppt "CHAPTER 3 The Component Object Model and DirectX © 2008 Cengage Learning EMEA."

Similar presentations


Ads by Google