Download COM in CMSC 433: Interfaces, Ref Counting, and QueryInterface - Prof. Alan L. Sussman and more Study notes Programming Languages in PDF only on Docsity!
CMSC 433, Alan Sussman, U. Maryland (via
CMSC433, Spring 2001
COM - Component Object Model
Alan Sussman April 26, 2001
CMCS 433, Spring 2001 - Alan Sussman 2
Component Object Model
- Language independent
- OS independent (in theory)
- Way to allow components to be designed, deployed, upgraded - Need to interact with code written after you were deployed
CMCS 433, Spring 2001 - Alan Sussman 3
Immutable interfaces
- Interact through interfaces
- No direct access to fields
- Interfaces must never be changed
- Interfaces assigned a GUID
- avoid name clashes
- allow versioning by assigning a new GUID
CMCS 433, Spring 2001 - Alan Sussman 4
COM
- A binary compatibility standard
Client IStream (^) Method 1 Method 2 Method 3 Method 4
CMCS 433, Spring 2001 - Alan Sussman 5
Multiple interfaces
- Components can implement multiple interfaces
- Different interfaces may correspond to different entry points to object - C++ multiple inheritance - adaptors
CMCS 433, Spring 2001 - Alan Sussman 6
Multiple interfaces
Client IStream^ Method 1 Method 2 Method 3 Method 4 IPrint (^) Method 1 Method 2 Method 3 Method 4
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Spring 2001 - Alan Sussman 7
Interfaces in COM
- Similar to interfaces in Java
- Interfaces have a 128 bit Unique ID
- immutable, never changed, no collisions
- In writing COM code, always use interface pointers/references
CMCS 433, Spring 2001 - Alan Sussman 8
Reference counting
- COM objects are reference counted
- each object keeps track of the number of pointers to it
- When ref count goes to zero, element deletes itself
- Cycles can be a problem
- Remembering where to put all increments and decrements can be a problem
CMCS 433, Spring 2001 - Alan Sussman 9
Each interface counted separately
- Each entry point/interface to a COM object is ref counted separately - allows an adaptor to be garbage collected
CMCS 433, Spring 2001 - Alan Sussman 10
IUnknown
- All COM interfaces must extend IUnknown
- HRESULT QueryInterface(const IID& iid, void ** ppv)
- ULONG AddRef() // inc ref count
- ULONG Release() // dec ref count
CMCS 433, Spring 2001 - Alan Sussman 11
Query interface
- Like a C++ dynamic cast
- Do you support this interface?
- If so, give me back a pointer of that kind
- incrementing the ref count for that interface
- Else, signal failure
CMCS 433, Spring 2001 - Alan Sussman 12
QueryInterface rules
- You always get the same IUnknown
- You can get an interface if you got it before
- You can get the interface you have
- You can get back to where you started
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Spring 2001 - Alan Sussman 19
Last time
- COM features
- all accesses through interfaces
- can create new ones, but can’t change existing ones
- use GUIDs for uniqueness
- all objects ref counted – each interface separately
- since objects can have multiple interfaces
- all objects extend IUnknown , return HRESULT
- must implement QueryInterface (), AddRef(), Release()
- use CoCreateInstance or class factory to create objects
CMCS 433, Spring 2001 - Alan Sussman 20
FooBar
- interface IFoo : public IUnknown { … }
- interface IBar : public IUnknown { … }
- class Foo : public IFoo { … }
- class Bar : public IBar { … }
- class FooBar : public Foo, public Bar { … }
CMCS 433, Spring 2001 - Alan Sussman 21
Implementing QueryInterface
STDMETHODIMP QueryInterface(const IID& iid, void **ppv) { if (iid == IID_IUnknown || iid == IID_IFoo) ppv = static_cast< Foo>(this); else if ( iid == IID_IBar) *ppv = static_cast<Bar *>(this); else { *ppv = null; return E_NOINTERFACE; } reinterpret_cast<IUnknown >(ppv)->AddRef(); return S_OK; }
CMCS 433, Spring 2001 - Alan Sussman 22
Things to note
- If you support 100 interfaces, cascaded if statements are going to get expensive - can’t use case statements (UIID’s aren’t ints) - could use custom hashtable
- Separate ref counts
- could put call to AddRef in each branch
- would eliminate reinterpret_cast
- but would increase code size
CMCS 433, Spring 2001 - Alan Sussman 23
I want everything
- Why can’t I ask
- what is the list of all of the interfaces you support?
- What would you do with the list of all interfaces a component supports?
- Can use component categories
CMCS 433, Spring 2001 - Alan Sussman 24
Component categories
- Assigned a GUID
- Corresponds to a set of interfaces
- If a component is registered as member of a category
- instances of that component support all of those interfaces
- will still need to use QueryInterface to move between interfaces
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Spring 2001 - Alan Sussman 25
Categories in Java
- Just define a Mega-interface
- An interface that extends all of the interfaces in the category
- Ask if class/component implements that
- Can use reference of Megainterface type to invoke all methods from any interface in category
- No casting needed
CMCS 433, Spring 2001 - Alan Sussman 26
Component reuse
- How to reuse components?
- Base class (implementation inheritance)
- Containment (have as a member)
- Delegation - Some methods get directly forwarded
- Adaptor - Some methods get translated
- Aggregation
CMCS 433, Spring 2001 - Alan Sussman 27
Aggregation
- Say I have a component Bar
- which uses a component Foo
- Foo implements the IFoo interface
- Bar also implements the IFoo interface
- by handing things off to its Foo
- Could handle by delegation
- but that adds an additional level of indirection
CMCS 433, Spring 2001 - Alan Sussman 28
Using Aggregation
- When someone asks a Bar for its IFoo interface - just hand them a reference to your Foo - handles all IFoo function calls
- But what if you invoke QueryInterface on the IFoo reference and ask for an IBar interface?
CMCS 433, Spring 2001 - Alan Sussman 29
Delegation/Forwarding
IBar
Bar
Foo
IUnknown
IFoo
IUnknown
inner
CMCS 433, Spring 2001 - Alan Sussman 30
Aggregation
Bar
Foo
IUnknown
IFoo
outer
IUnknown
IBar
inner
CMSC 433, Alan Sussman, U. Maryland (via
COM and software components
CMCS 433, Spring 2001 - Alan Sussman 38
Topics
CMCS 433, Spring 2001 - Alan Sussman 39
Marshalling/Serialization
- COM allows objects to be marshalled
- same as Serialization in Java
- Need to give extra data in IDL file
- IDL = interface definition language
CMCS 433, Spring 2001 - Alan Sussman 40
Example IDL for Marshalling
interface IY : IUnknown { HRESULT fCount([out] int* sizeArray); HRESULT fArrayIn([in] int sizeIn, [in, size_is(sizeIn)] int arrayIn[]); HRESULT fArrayOut([in] int maxSize, [in, size_is(maxSize)] int arrayOut[], [out] int* sizeOut); }
CMCS 433, Spring 2001 - Alan Sussman 41
COM Threads
- Free threads
- similar to Java threads, must use explicit synchronization
- Apartment threads
- COM objects can be grouped into an apartment
- Each apartment has a designated thread
CMCS 433, Spring 2001 - Alan Sussman 42
Apartment threads
- Single thread for entire apartment
- Call from a free thread, or from a different apartment, is marshalled - like an RMI call - Apartment thread must have a message loop to receive and dispatch calls
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Spring 2001 - Alan Sussman 43
Apartment threads
- Simple synchronization model
- backwards compatible with WIN32?
- Similar to having a single synchronization object for an entire set of components
- Still have potential problems such as deadlock
CMCS 433, Spring 2001 - Alan Sussman 44
Servers in COM
- COM objects don’t have to be local
- can make a remote call (like Java RMI)
- A COM object can be
- in process
- in process, different apartment
- same machine, separate process
- different machine
CMCS 433, Spring 2001 - Alan Sussman 45
Advantages of COM servers
- Seg fault (illegal pointer dereference) only kills one process
- OS services can be provided as COM services