280 likes | 467 Views
Fundamentals of COM Mary Kirtland Program Manager COM Team Microsoft Corporation. Agenda. “Fundamentals of COM” components “Fundamentals of MTS” distributed, component-based applications “COM+ Architecture” future directions for COM, MTS. Agenda. Why COM? Programming Model Sample Code
E N D
Fundamentals of COMMary KirtlandProgram ManagerCOM TeamMicrosoft Corporation
Agenda • “Fundamentals of COM” • components • “Fundamentals of MTS” • distributed, component-based applications • “COM+ Architecture” • future directions for COM, MTS
Agenda • Why COM? • Programming Model • Sample Code • Next Steps
Why COM? • Imagine a world where developers can assemble applications from existing, reusable, pieces of code... • “component” -- a reusable, binary, piece of software that provides a service • Scenarios • User-Interface Controls • Line-of-business applications • Manufacturing process control • etc...
Component Architecture • What would the requirements be? • component interoperability • Robust evolution (versioning) • Language independence • Location transparency • Scalability • COM does all of these things! • Tools choose how to expose COM
COM Fundamental PrinciplesWhat the designers of COM were striving for... • Binary components • Any programming language • Any location (in-process, cross-process, cross-machine) • No centralized authority • Zero sacrifice in-proc performance • Simplest model possible • Enable extensibility and adaptability
COM Principles • Rigorous Encapsulation • Black box -- no leakage of implementation details • All object manipulation through strict interfaces • Polymorphism • via multiple interfaces per class • “Discoverable”: QueryInterface
Object 2 COM Programming Model Object 1 Client
COM Identifiers • text strings not unique, slow • Globally Unique Identifiers (GUIDs) are 128-bit integers • statistically guaranteed to be unique • each interface has a unique GUID (IID) • each class has a unique GUID (CLSID) • GUIDs can be mapped to text strings
Interfaces • An interface is a group of related functions that specifies a contract • once published, can never change • establishs a protocol for object communications • Each interface is identified via an Interface Identifier or IID /* IID 69B4985B-312D-11D1-835C-00C04FB957D8 */ interface IWordCount : public IDispatch { HRESULT CountWords(BSTR sentence, LPLONG words); };
IUnknown • Every COM component must implement IUnknown • all COM interfaces derive from IUnknown • Provides... • object lifetime (AddRef, Release) • feature discovery (QueryInterface) interface IUnknown { HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppv); ULONG AddRef(void); ULONG Release(void); };
QueryInterface • essential for multiple independent interfaces • enables run-time discovery of features • component and client communicate in the richest way they share in common • New functionality can be added with full backwards compatibility
IUnknown ISpeakMandarin Object ISpeakFrench ISpeakEnglish QueryInterface (2) Client A QI(ISpeakSpanish) QI(ISpeakFrench) Client B QI(ISpeakMandarin) Client C GetInterfaceList QI(ISpeakEnglish)
Reference Counting • Determines object’s lifetime • All objects keep track of a ‘reference count’ • AddRef increments by one • Release decrements by one • When the implementation of Release sees a reference count of zero it frees the object from memory
COM Classes • named, concrete implementation of one or more interfaces • named by a Class Identifier, or CLSID • associate a class factory with every class • implements IClassFactory • entrypoint to the class
Creating Objects • create a class factory and call IClassFactory::CreateInstance • each class factory creates one type of object • COM APIs hide the details • CoGetClassObject • CoCreateInstance, CoCreateInstanceEx
Object COM Programming Model Component IUnknown Class Factory Client IClassFactory Registration Packaging
Where can a component live? “Marshaling” across process boundary via RPC Client Local Component (EXE) RPC RPC RemoteComponent on another machine Direct function Calls In-ProcessComponent (DLL) RPC RPC “Marshaling” across machine boundary via networked RPC
COM Runtime Components and Applications The COM Runtime Core Services(Monikers, Storage, Data Transfer,...) [D]COM Registry Pluggable Security(SSPI) MS-RPC Pluggable Transports NTLM DCE Kerberos ETC... TCP UDP IPX Tunneled TCP ETC...
COM Runtime • COM library exposes the COM API to applications and provides several default implementations • Implemented as OLE32.DLL • Service Control Manager (SCM) starts server processes/loads server DLLs at activation-time • Implemented in OLE32.DLL/RPCSS.EXE
The SCM • starts server processes and loads DLL implementations at activation-time • finds desired object and gets out of the way • For inprocess objects, no third-party is involved in client->object communications • For out-of-process objects, a client-side proxy sends packets to a server-side stub
Sample Code “Island Hopper” util_WordCount (ATL, C++) util_TakeANumber (VB)
Util_Takeanumber bus_Ad db_Ad db_Invoice bus_Invoice db_Customer bus_Customer db_Category Util_TNUpdate Util_Wordcount MTS TakeaNumber Categories Customers Ads Invoices Island Hopper App. Utilities Accounting Classifieds
Automation IDispatch Type Libraries Monikers Persistence IStorage, IStream Remoting DCOM Security Threading Next Steps
Call To Action • Use COM to build component-based applications today • Understand the programming model and what your tools do for you • Use resources to learn more about COM
Resources • On the web… • http://www.microsoft.com/com/ • http://www.microsoft.com/msdn/ • In print… • “Understanding ActiveX and OLE” (David Chappell, MSPress) • “Inside COM” (Dale Rogerson, MSPress) • “The Component Object Model” (Don Box, Addison-Wesley) • “Professional DCOM” (Richard Grimes, WROX)
™ http://www.microsoft.com/msdn/pdc