180 likes | 305 Views
C omponent O bject M odel , DCOM and Software Components. Nat Brown COM Program Management Microsoft Corporation With additions by R. Cook. How to Compose Applications?. From components written in any language From components executing on multiple machines
E N D
ComponentObjectModel, DCOMandSoftware Components Nat Brown COM Program Management Microsoft Corporation With additions by R. Cook
How to Compose Applications? • From components written in any language • From components executing on multiple machines • Allow an application to delegate UI actions and screen space to components (e.g. embed spreadsheet in a document) • Allow applications to be used as components to build other applications (automation)
What is Automation? • Automation is the ability to control an application from another. • Sometimes referred to as scripting. • For example, script can start Excel, load a spreadsheet, add data, save, quit Excel. • Visual Basic for Applications (VBA) is a scripting language which makes use of automation. • Perl (and PerlScript) makes use of automation.
What is a COM Component? • Supports the IUnknown abstract interface • May support other interfaces • No inheritance or other language dependencies • Every interface has a UNIQUE 128-bit # (uuidgen in Windows and OS/X) • Every copy of an interface is reference-counted, although this does not preclude a high-level implementation of garbage collection
What is a COM Component? • Every class has a UNIQUE 128-bit CLSID • A class (implementation) can support multiple interfaces • A class can have a text name, referred to as a PROGID
What is a COM Component? [C#] namespace EsriSamples { [Guid("97FDB5D7-41D8-4260-BF72-3EB2CDD36747")] [ProgID("ZoomInCmd.ZoomIn")] public class ZoomIn: ICommand { ...
class CMyClass : public Iunknown { //C++ private: ULONG m_cRef; // COM reference count public: STDMETHODIMP QueryInterface(REFIID riid, void** ppvObject) { IUnknown *punk = nullptr; if (riid == IID_IUnknown) punk = static_cast<Iunknown *>(this); // TODO: List other implemented interfaces here. *ppvObject = punk; if (punk == nullptr) return E_NOINTERFACE; punk->AddRef(); return S_OK; } STDMETHODIMP_(ULONG) AddRef() { return ++m_cRef; } STDMETHODIMP_(ULONG) Release() { ULONG cRef = --m_cRef; if (cRef == 0) delete this; return cRef; } };
Interface Definition Language - IDL [ object, uuid(75D873CD-7B63-11D3-9D43-00C0F031CDDE), helpstring("IServer Interface"), pointer_default(unique) ] interface IServer : IUnknown { HRESULT HelloWorld([in, string] char * pszMessage); };
In the same process • Fast, direct function calls Component Client Client Process Server Process • On the same machine • Fast, secure IPC Component COM Client • Across machines • Secure, reliable and flexibleDCE-RPC based DCOM protocol Server Machine Client Machine Component DCE RPC COM COM Client The COM Programming ModelA scalable programming model
TCP, UDP IPX, SPX HTTP Msg-Q DCOM ArchitectureFlexible and extensible Pluggable Transports Client Machine Server Machine D C O M D C O M COM Object Clients
TCP, UDP NT4 Security SSL/ Certificates IPX, SPX NT Kerberos HTTP DCE Security Falcon DCOM ArchitectureFlexible and extensible Pluggable Security Client Machine Server Machine D C O M D C O M COM Object Clients
DCOM ArchitectureEfficient and scalable • Multiplexing - Single Port per-protocol, per server process, regardless of # of objects • Scalable - Connection-Less Protocols like UDP Preferred • Established Connection-Oriented (TCP) Sessions Reused by same client Client Server Client
DCOM ArchitectureEfficient and scalable • Low Bandwidth • Keep-Alive Messages bundled for all connections between Machines Client Machine Server Machine Keep-Alive Traffic for all connections Client #1 Server Logical “Connections” or “Sessions” Client #2
Receiver Class Factory Queue Class Factory DLL Register Connections DLL Register RefCounting Context Security RefCounting Management Configuration Query Interface Thread Pool Query Interface IDispatch Service Logic IDispatch Connection Points Connection Points Synchronization Meta Data Type Info Shared Data Methods Server Methods Component Component Ease-of-Use: First Steps Clients Network Receiver Queue Connections Context Security Management Configuration Thread Pool Service Logic Synchronization Shared Data Server MTS = easier servers Easier components?
Additional Technologies • COM+ • MTS - Microsoft Transaction Server • MSMQ - Microsoft Message Queue • ADS - Active Directory Service • As “distributed registry” • As namespace abstraction • All Microsoft products are COM based: • IIS - Internet Information Server • Exchange • Internet Explorer • Word, Excel, etc.
First Steps Basically there is a general model of use: 1) A typical controller process will request that a COM server generate a COM object. 2) The server is loaded or located, the request is submitted, a response is returned. 3) If request results in a valid COM object then controller process interacts with the object. 4) Destroy COM object.
First Steps Let’s say we need to change the title and subject of a Microsoft Word document 1) Need to somehow run Word 2) Need to load up the document 3) Need to change the title and subject 4) Need to save the document 5) Need to quit Word
First Steps How would we implement such a system? Request a Word application COM object Query the appropriate interface Call a function in the Word application COM object which loads a document. It returns a Word document COM object Modify the Title and Subject properties from the Word document COM object Call into the Word document COM object to save to disk Destroy both the document and application COM objects