Beruflich Dokumente
Kultur Dokumente
• Language independent
COM - Component Object Model • OS independent (in theory)
• Way to allow components to be designed,
deployed, upgraded
– Need to interact with code written after you
were deployed
1
Interfaces in COM Reference counting
• Similar to interfaces in Java • COM objects are reference counted
– no variables – each object keeps track of the number of
• Interfaces have a 128 bit Unique ID pointers to it
– immutable, never changed, no collisions • When ref count goes to zero, element
• In writing COM code, always use interfaces deletes itself
pointers/references • Cycles can be a problem
• Remembering where to put all increments
and decrements can be a problem
2
HRESULT Creating objects in COM
• COM doesn’t understand exceptions • Each component has a CLSID (class ID)
• So almost all methods return an HRESULT • Can call CoCreateInstance
– numerical indication of success or a specific • Can get Class Factory, then create instances
error directly
• Each DLL has a function that can return
class factories for all classes that can be
created by that DLL
FooBar
• interface IFoo : public IUnknown { … }
COM, part 2 • interface IBar : public IUnknown { … }
• class Foo : public IFoo { … }
• class Bar : public IBar { … }
• class FooBar : public Foo, public Bar { … }
3
Implementing QueryInterface Things to note
STDMETHODIMP QueryInterface(const IID& iid, • If you support 100 interfaces, cascaded if
void **ppv) {
if (iid == IID_IUnknown || iid == IID_IFoo) statements are going to get expensive
*ppv = static_cast<Foo*>(this); – can’t use case statements (UIID’s aren’t ints)
else if (iid == IID_IBar)
*ppv = static_cast<Bar *>(this); – could use custom hashtable
else {
*ppv = null; return E_NOINTERFACE; • Separate ref counts
} – could put call to AddRef in each branch
reinterpret_cast< IUnknown *>(*ppv)->AddRef();
return S_OK; – would eliminate reinterprete_cast
} – but would increase code size
4
Aggregation Using Aggregation
• Say I have a component Bar • When someone asks a Bar for its IFoo
– which uses a component Foo interface
– just hand them a reference to your Foo
• Foo implements the IFoo interface
– handles all IFoo function calls
• Bar also implements the IFoo interface • But what if you invoke QueryInterface on
– by handling things off to its Foo the IFoo reference and ask for an IBar
• Could handle by delegation interface?
– but that adds an additional level of indirection
Delegation/Forwarding Aggregation
IUnknown IUnknown
IUnknown IUnknown
inner inner
IBar IBar
outer
IFoo IFoo
Foo Foo
Bar Bar
5
Automation interfaces are a pain Dual interfaces
• All argument types must be one of a • Support both Dispatch invocation and
predefined VARIANT list regular method invocation
– primitive types • Code for dispatch invocation can be built
• how do you pass a 1? automatically
– long, byte, short, ushort, ulong, int, uint
– if you limit yourself to VARIANT argument
– IUnknown and IDispatch types
types
• No method overloading
• No way to ask the types of a method
Topics
• Marshalling
COM and software components • Threads
6
Marshalling/Serialization Example IDL for Marshalling
• COM allows objects to be marshalled • interface IY : IUnknown {
HRESULT fCount([out] int * sizeArray);
– same as Serialization in Java HRESULT fArrayIn([in] int sizeIn,
• Need to give extra data in IDL file [in, size_is(sizeIn) int arrayIn[]);
HRESULT fArrayOut([in] int maxSize,
– IDL = interface definition language [in, size_is(maxSize)] int arrayOut[],
[out] int * sizeOut);
}
7
Advantages of COM servers Java Beans
• Seg fault only kills one process • A Java-based Software component
• OS services can be provided as COM technology
services – not the only way to do components in Java
• A Java Bean is a reusable software
component that can be manipulated visually
in a builder tool
8
Tools and patterns Properties
• If a pattern is simple • If a component supports functions:
– automatic tools can understand it – public void setMyValue(int v)
– extract pattern information – public int getMyValue()
– generate code • It has a MyValue property of type int
• For boolean types, getter function can be
named is<Prop>()
• interface TempChangeListener {
void tempChanged(TempChangedEvent e);
}
9
Event Adapters Bound properties
• Easy to construct event adapters • Can set things up so that changes to bean
– For example, an adapter that receives property are indicated by an event
temperatureChanged events, and generates – events are a subtype of
temperatureIncreased and java.beans.PropertyChangeEvent
temperatureDecreasedEvents – Listeners implement PropertyChangeListener
– One Listener for all change events on the bean
• may optionally support listeners for specific
properties
10