Presentation is loading. Please wait.

Presentation is loading. Please wait.

Component Object Model. ActiveX and OLE rest on the foundation of COM ActiveX and OLE rest on the foundation of COM Understanding COM is a prerequisite.

Similar presentations


Presentation on theme: "Component Object Model. ActiveX and OLE rest on the foundation of COM ActiveX and OLE rest on the foundation of COM Understanding COM is a prerequisite."— Presentation transcript:

1 Component Object Model

2 ActiveX and OLE rest on the foundation of COM ActiveX and OLE rest on the foundation of COM Understanding COM is a prerequisite to understanding anything else in ActiveX or OLE Understanding COM is a prerequisite to understanding anything else in ActiveX or OLE

3 Components

4 Benefits of Components – Flexibility Program for User A Program for User B

5 Benefits of Components – Component Libraries Component Libraries New Program

6 Benefits of COM – Distributed Components Network

7 Component Object Model How should one chunk of software access the services provided by another chunk of software? How should one chunk of software access the services provided by another chunk of software? COM: A standard approach to access all kinds of software services, regardless of how they are provided COM: A standard approach to access all kinds of software services, regardless of how they are provided COM is transforming the way software is constructed COM is transforming the way software is constructed

8 Benefits of COM Offers the benefits of object orientation Offers the benefits of object orientation Provides consistency Provides consistency Is language independent Is language independent COM defines a binary interface that objects must support COM defines a binary interface that objects must support Simple and efficient versioning Simple and efficient versioning Available on Windows, Windows NT, Macintosh, MVS up to now Available on Windows, Windows NT, Macintosh, MVS up to now DCOM allows COM objects on all kinds of systems to interact DCOM allows COM objects on all kinds of systems to interact

9 Communications between Software OS Application lib Application OS Application

10 Basic COM Concept Binary Code of a Server Class Interface Binary Code of a Client Class COM Library

11 Basic COM Concept COM object Server Interface

12 Basic COM Concept COM object Client

13 Basic COM Concept OS Application lib Application

14 A COM Object COM Object TextTools Client LookUpWord( ) AddToDictionary( ) RemoveFromDictionary( ) ReturnSynonym( )

15 Identifying an Interface Human-readable name Human-readable name ISpellChecker, IThesaurs ISpellChecker, IThesaurs Globally Unique Identifier (GUID) Globally Unique Identifier (GUID) Interface Identifier (IID) Interface Identifier (IID) Each GUID is unique in time and space Each GUID is unique in time and space Timestamp Timestamp Address on the machines network interface card Address on the machines network interface card 16 bits 16 bits

16 GUID Generator

17 Interface Definition Language [ object, uuid( E7CD0D CF- E7CD0D CF ) ] ) ] interface ISpellChecker : IUnknown{ import unknown.idl; HRESULT LookUpWord( [in] OLECHAR word[31], [out] boolean *found ); HRESULT AddToDictionary( [in] OLECHAR word[31]); HRESULT RemoveFromDictionary( [in] OLECHAR word[31]); }

18 Immutability of the Interfaces Once an interface has been implemented in released software, it cannot be changed Once an interface has been implemented in released software, it cannot be changed To add new functionality or to modify existing functionality requires defining an entirely new interface, with a new and different IID To add new functionality or to modify existing functionality requires defining an entirely new interface, with a new and different IID The creator of the software is free to stop supporting the original interface but is absolutely prohibited from changing it The creator of the software is free to stop supporting the original interface but is absolutely prohibited from changing it

19 New Version Problem A COM object that supports ISpellChecker A COM object that supports ISpellChecker Add support for a thesaurus function through IThesaurus Add support for a thesaurus function through IThesaurus Effect on the client using the old version with the old or new version installed Effect on the client using the old version with the old or new version installed Effect on the client using the new version with the new version installed Effect on the client using the new version with the new version installed Effect on the client using the new version with the old version installed Effect on the client using the new version with the old version installed

20 Changing Features to an Interface The objects creator must define a new interface, say ISpellChecker2 that includes the new or changed methods and the COM object continues to support ISpellChecker as before, but it now also support ISpellChecker2 The objects creator must define a new interface, say ISpellChecker2 that includes the new or changed methods and the COM object continues to support ISpellChecker as before, but it now also support ISpellChecker2 Clients that are unaware of the upgrade never ask for a pointer to ISpellChecker2 and so arent affected by the changes – they continue to use ISpellChecker as before Clients that are unaware of the upgrade never ask for a pointer to ISpellChecker2 and so arent affected by the changes – they continue to use ISpellChecker as before

21 IUnknown::QueryInterface COM object Client A B

22 Reference Counting Whenever the object passes out a pointer to one of its interface, it adds 1 to reference count. If one client passes an interface pointer to another client, the client who receives the pointer must invoke the AddRef method using that pointer Whenever the object passes out a pointer to one of its interface, it adds 1 to reference count. If one client passes an interface pointer to another client, the client who receives the pointer must invoke the AddRef method using that pointer The client must call Release on the interface pointer to decrement the reference count when it has finished using the interface The client must call Release on the interface pointer to decrement the reference count when it has finished using the interface An object destroys itself when its reference count drops to 0 An object destroys itself when its reference count drops to 0

23 Binary Format of an Interface AddRef( ) {…} QueryInterface( ) {…} LookUpWord( ) {…} AddToDictionary( ) {…} Release( ) {…} RemoveFromDictionary( ) {…} Pointer to Method 1 Pointer to Method 6 Pointer to Method 2 Pointer to Method 3 Pointer to Method 4 Pointer to Method 5 Client Object vtable

24 COM Classes Class identifier (CLSID) Class identifier (CLSID) An object of a specific class supports a certain set of interfaces An object of a specific class supports a certain set of interfaces Adding interfaces to an object without changing its class is not prohibited by COM Adding interfaces to an object without changing its class is not prohibited by COM An objects class identifies a particular implementation of a group of interfaces An objects class identifies a particular implementation of a group of interfaces Its possible to have many classes supporting the same set of interfaces Its possible to have many classes supporting the same set of interfaces

25 Kinds of COM Servers Machine X Machine Y

26 COM Library The COM library implements a group of functions that supply basic services to objects and their clients The COM library implements a group of functions that supply basic services to objects and their clients The COM librarys services are accessed through ordinary function calls The COM librarys services are accessed through ordinary function calls The names of COM library functions usually begin with the prefix Co- for example, CoCreateInstance The names of COM library functions usually begin with the prefix Co- for example, CoCreateInstance

27 System Registry The classes of all objects that the COM library will be asked to create on this machine must be registered The classes of all objects that the COM library will be asked to create on this machine must be registered Registry mapping includes Registry mapping includes CLSID CLSID Kinds of servers Kinds of servers Pathname for the file containing the servers DLL or executable, or for where to find remote servers executable Pathname for the file containing the servers DLL or executable, or for where to find remote servers executable

28 Creating a Single Object Client COM library Object Server CLSID_X …… CLSID_Y DLL EXE

29 Class Factories Class factory is a kind of objects that can create objects of one specific class Class factory is a kind of objects that can create objects of one specific class Class factories are COM objects in their own right – accessed via their interfaces Class factories are COM objects in their own right – accessed via their interfaces CoCreateInstance uses a class factory that it hides from the client CoCreateInstance uses a class factory that it hides from the client CoCreateInstance uses the methods in the IClassFactory interface CoCreateInstance uses the methods in the IClassFactory interface

30 IClassFactory Interface As a class factory, an object must support the IClassFactory interface As a class factory, an object must support the IClassFactory interface IClassFactory IClassFactory CreateInstance CreateInstance LockServer LockServer IClassFactory2 IClassFactory2 A client calls CoGetClassObject to get a pointer to a class factory A client calls CoGetClassObject to get a pointer to a class factory

31 Using Class Factory Client Class Factory Object Server

32 Emulation An old class is replaced by a new class with different CLSID An old class is replaced by a new class with different CLSID A COM library function CoTreatAsClass: A COM library function CoTreatAsClass: when used to create an object using the old CLSID will result in instantiating an object with the new CLSID when used to create an object using the old CLSID will result in instantiating an object with the new CLSID (setting up an emulation relationship between the two CLSIDs in the system registry)

33 Initializing COM Objects A client usually asks a newly created objects to initialize itself A client usually asks a newly created objects to initialize itself The objects data must be stored in a persistent way The objects data must be stored in a persistent way The first interface pointer a client requests when a new object is created is usually the one for an interface containing the objects initialization function The first interface pointer a client requests when a new object is created is usually the one for an interface containing the objects initialization function

34 Calling Sequence in VC++ (1/2) CLSID clsid; IClassFactory* pClf; IUnknown* pUnk; CoInitialize(NULL); ::CLSIDFromProgID(LTestTools, &clsid); ::CoGetClassObject(clsid, IID_IClassFactory, (void**) &pClf);

35 Calling Sequence in VC++ (2/2) pClf-> CreateInstance(IID_Iunknown, (void**)&pUnk); pUnk-> QueryInterface(IID_ISpellChecker, (void**)&pSpl); pSpl->LookUpWord( Test, found ); pClf->Release();pUnk->Release();pSpl->Release();CoFreeUnusedLibraries();CoUninitialize();

36 Reusing COM Objects One COM object cant reuse anothers code through inheritance One COM object cant reuse anothers code through inheritance Containment (delegation) Containment (delegation) Aggregation Aggregation

37 Marshaling and Type Information Marshaling makes that the client can invoke the methods in the same way, regardless of where the object is implemented Marshaling makes that the client can invoke the methods in the same way, regardless of where the object is implemented To acquire the information necessary to perform marshaling, and sometimes for other reasons as well, it can be useful to obtain type information about the project To acquire the information necessary to perform marshaling, and sometimes for other reasons as well, it can be useful to obtain type information about the project

38 Accessing a COM Object in an In-Process Server Client Object Client process

39 Accessing a COM Object in a Local Server Proxy Object Client Stub Client process Server Process Single machine

40 Accessing a COM object in a Remote Server Proxy Object Client Stub Client process Server Process Machine X Machine Y

41 Marshaling and Unmarshaling Marshaling Marshaling Packaging a calls parameters into a standard format for transmission Packaging a calls parameters into a standard format for transmission Unmarshaling Unmarshaling Unpackaging from the standard format into a format appropriate for the receiving process Unpackaging from the standard format into a format appropriate for the receiving process Marshaling Code (Marshaler) Marshaling Code (Marshaler) MIDL compiler can read the IDL description of an interface and automatically generate the necessary marshalling code for this interface, contained in a proxy and a stub MIDL compiler can read the IDL description of an interface and automatically generate the necessary marshalling code for this interface, contained in a proxy and a stub

42 Type Information Type information includes a description of everything a client needs to know to use an objects service. For example, the type information of an object includes a list of the interfaces methods and properties, along with a description of the parameters for those methods. Type information includes a description of everything a client needs to know to use an objects service. For example, the type information of an object includes a list of the interfaces methods and properties, along with a description of the parameters for those methods. All type information today should be defined using IDL All type information today should be defined using IDL

43 Type Library Interface and Library definitions In IDL MIDL compiler Proxies StubsType library Other files

44 IDL Example [ object,uuid( E7CD0D CF ) ] E7CD0D CF ) ] interface ISpellChecker : IUnknown{ import unknown.idl; HRESULT LookUpWord( [in] OLECHAR word[31], [out] boolean *found ); HRESULT AddToDictionary( [in] OLECHAR word[31]); HRESULT RemoveFromDictionary( [in] OLECHAR word[31]); }

45 IDL Example [ object,uuid( 5FBDD CF ) ] interface IThesaurus : IUnknown { HRESULT Return Synonym( [in] OLECHAR word[31], [out] OLECHAR synonym[31] ); }

46 IDL Example [uuid( B253E CF ), version(1.0)] library TextToolsLib { importlib (stdole32.tlb); [uuid(B2ECFAA CF )] coclass CoTextTools { interface ISpellChecker; interface IThesaurus; }}

47 Automation Making applications programmable Making applications programmable Programmability through macro languages Programmability through macro languages Cryptic Cryptic No standard exists No standard exists One choice for an application One choice for an application Automation Automation General programmability with COM General programmability with COM Use developers favorite programming tools to build applications on top of the functions already provided by existing software Use developers favorite programming tools to build applications on top of the functions already provided by existing software

48 Automation Example Database Spreadsheet Word processor Charting Automation script: Extract current data Tabulate and sum Chart the results Place chart in report Send electronic copies of report to staff

49 IDispatch and dispinterfaces Client Invoke( ) { switch (DISPID) 1: MethodX( ); 2: MethodY( ); 3: MethodZ( ); } IDispatch:: Invoke(DISPID) IDispatch vtable Object dispinterface

50 Clients and Servers Client Object Application IDispatch IServices IDispatch

51 dispinterfaces and Marshaling Using a dispinterface for marshaling requires only a single proxy and stub Using a dispinterface for marshaling requires only a single proxy and stub The parameters for a dispinterface method are packaged into a variant The parameters for a dispinterface method are packaged into a variant The client passes the variant along with the DISPID on the call to IDispatch::Invoke The client passes the variant along with the DISPID on the call to IDispatch::Invoke The object that implements the dispinterface unpackages the variant The object that implements the dispinterface unpackages the variant Results are wrapped in a variant by the object and then returned to and unwrapped by the client Results are wrapped in a variant by the object and then returned to and unwrapped by the client

52 A Visual Basic Example Sub SpellCheck( ) Dim Obj As Object Set Obj = CreateObject(Excel.Application) Word = InputBox(Enter word) If Obj.CheckSpelling(Word) Then MsgBox( Valid word ) Else MsgBox( Word not found ) End If End Sub

53 Dual Interfaces Client Invoke( ) { switch (DISPID) 1: MethodX( ); 2: MethodY( ); 3: MethodZ( ); } IDispatch:: Invoke(DISPID) vtable Object dispinterface

54 Dual Interfaces [object, uuid( E7CD0D CF ), dual] ), dual] Interface ISpellChecker : IDispatch { import unknown.idl; import oaidl.idl; HRESULT LookUpWord([in] BSTR word, [out] boolean *found); HRESULT AddToDictionary( [in] BSTR word); HRESULT RemoveFromDictionary( [in] BSTR word); }

55 Dual Interfaces [uuid( B623E CF ), version(1.0)] library SpellCheckerLib { importlib (stdole32.tlb); [uuid(B23EFAA CF )] coclass CoSpellChecker { interface ISpellChecker; }}

56 Persistence and Monikers An objects persistent data is information about the objects state thats saved between instantiations of the object An objects persistent data is information about the objects state thats saved between instantiations of the object A mechanism that lets objects store and retrieve their persistent data is called a persistence service A mechanism that lets objects store and retrieve their persistent data is called a persistence service An objects client typically controls when the objects persistent data is loaded and saved An objects client typically controls when the objects persistent data is loaded and saved

57 Structured Storage Root storage Storage Stream Disk file (Compound file)

58 Using Structured Storage COM object Stream Storage

59 Moniker A moniker is a name for a specific object instance, one particular combination of CLSID and persistent data A moniker is a name for a specific object instance, one particular combination of CLSID and persistent data A moniker is an object that supports the IMoniker interface A moniker is an object that supports the IMoniker interface Each moniker has its own persistent data, which contains everything the moniker needs to start and initialize the single object instance the moniker identifies Each moniker has its own persistent data, which contains everything the moniker needs to start and initialize the single object instance the moniker identifies

60 Using a Moniker Client Moniker Object Server A IMoniker


Download ppt "Component Object Model. ActiveX and OLE rest on the foundation of COM ActiveX and OLE rest on the foundation of COM Understanding COM is a prerequisite."

Similar presentations


Ads by Google