280 likes | 409 Views
Introduction to COM and ActiveX Controls. What is an object?. In the Fayad sense of the word . Object Oriented Themes (Object Oriented Modeling and Design, Prentice-Hall, 1991). Abstraction: what should an object do, not how an object is implemented
E N D
What is an object? • In the Fayad sense of the word
Object Oriented Themes (Object Oriented Modeling and Design, Prentice-Hall, 1991) • Abstraction: what should an object do, not how an object is implemented • Encapsulation: what should be accessible to other objects and what shouldn’t • Couple behavior with data: object contains data and operations to perform on that data • Sharing: code reuse through aggregation and inheritance • Does a car known what kind wheel it has?
Object oriented themes and C++ • We create C++ class to • Encapsulate (class)ifications of data • Abstract our client code from operations on classifications of data • Build classifications of data that are not specific to any application but instead are generic and therefore reusable. • …(and the list goes on)
Wouldn’t it be nice if… • You could write a C++ class that could be used in a program written in Java, Visual Basic, VB Script, Delphi, COBOL, HTML, etc? • You could write a C++ class that could be loaded into a program at runtime rather than build in at compile time? • You could write a C++ class that runs on a computer other than the client program using it? • Your virtual sandbox is about to become much larger……
What is COM? • COM – Component Object Model • The basis for all of Microsoft’s component programming technology • A binary specification for object interoperability • Define the specification for binary objects written in dissimilar languages to be used interchangeably
Where are COM objects used on Microsoft Platforms? • More accurately, where isn’t COM used? • Operating system calls (most have COM interfaces) • DirectX (Direct Draw, Direct Show, Direct Play, …) • MAPI: messaging application programming interface • SSPI: security support provider interface (Kerberos, SSL, NTLM, etc) • Object Linking and Embedding: How is it that an Excel table can be inserted into a Word document or web page?
Why use COM? • Write an API once using COM and it is available to every development environment. • Software can be upgraded without recompiling • Software can be extended beyond it original capabilities without recompiling
A glimpse under COM’s hood • (basically) All programs are a stream of instructions that are executed sequentially • What if you could take a binary file that is know to contain a COM object and be guaranteed that certain functions begin at specific offsets in that file? • If you knew the offset of a function into a file, how would you call it, pass values to it, and get return values from it?
COM accomplishes these goals by defining: • A binary interface specification for all COM objects • Offsets into BLOBs (binary large objects) where functions can be found • Programming interfaces that all COM objects must support • Guarantees certain functionality always exists • Data types for use with COM objects • Values passed to and from objects • Mechanisms for registering and publishing a COM objects capabilities with the operating system
What functions are guaranteed to be in a COM object? • Remember, in many situations COM objects aren’t loaded into a program until after the program is running. • These for will need functions to: • Identify what the object is • Identify what the object can do • Handle acquiring memory (instantiating) • Handle releasing memory (de-allocating) • COM defines interfaces for these operations
What is an interface? What is a function or method? What’s the difference?
Interface = Contract • Once an interface is defined, it NEVER changes • Once an interface is defined, it NEVER changes • Once an interface is defined, it NEVER changes • …
IUnknown interface • By definition, a COM object is not a COM object unless it implements the IUnknown interface • IUnknown defines the interface to three functions: • IUnknown::QueryInterface() • IUnknown::AddRef() • IUnknown::Release() • COM defines them, YOU implement them! • Do you think the IUnknown interface will ever change?
Fishing for functionality • IUnknown::QueryInterface() • Use it to ask an object what it can do • How? Pass it an identifier for an interface and a pointer • if it supports the interface, it will assign the pointer to the requested interface and return S_OK • If it doesn’t support the interface, it will return E_NOINTERFACE • Don’t worry about the details now, just the concept of what QueryInterface is for.
GUID • Globally Unique Identifiers • Every COM object is assigned a (almost) globally unique 16 byte value • Use this value to identify the COM object • Same format is used to define CLSID and IID • Class Identifier • Interface Identifier
IClassFactory • Used to create an instance of a COM object • Inherits from IUnknown • Defines the interfaces: • IClassFactory::CreateInstance() • List a C++ constructor on steroids • IClassFactory::LockServer() • Allows a COM object to be locked in memory so additional requests for object instances are fast
Review • COM defines interfaces for binary objects that programs can interface with • All COM objects implement the IUnknown interface • Used to the determine interfaces an object supports • IClassFactory interface is used to create instances of an object
Quick note on where COM info is stored • COM objects are registered with the operating system • Information is stored in the registry • CLSID of the object • Name of the object • Where the file is that stores the object • What interfaces the object supports (IMPORTANT) • Other things…
Shortest distance between you and writing COM objects • ATL: Active Template Library • A set of templates that implement a framework to create COM objects of every type • Implements IUnknown and IClassFactory • Implements function to register COM object with operating system • Allows you to focus on implementing the functionality of the object and not the mundane tasks of implementing COM detains. • Code generator
How to use ATL in VC++ 6.0 • Select File->New->Projects (pane) • ATL COM AppWizard (from list) • Dynamic Link Library (radio button) • Finish (button) • Select->Insert • New ATL Object • Objects (left list box) • Simple Object (right selection box)
continued • Enter the name of the new COM object ( based on the name you enter, the Wizard will suggest names for the classes and type libraries it will create for you) • Select OK • Right click on your newly created COM interface in the “Class View” (left pane) and select “Add Method” • Enter the name of the method • Write some code in the stub that is created • Compile it – as part of the compilation process, the program regsrv32.exe is run to register your object.
How is your new COM object packaged • <projectname>.dll • The DLL is self registering (thanks to ATL) • Use the regsvr32.exe program to register the dll on other computers • Use regedit.exe to make sure the object was registered correctly • HKEY_CLASSES_ROOT->(project name) • In the key you will see the directory the dll is located in and the CLSID that identifies you new object.
How do you use your new object • Write a test program! • Include a few files that were created by ATL for your project • Initialize COM libraries in main() • Create a pointer to IClassFactory for your object • Use IClassFactory to create an interface pointer to IUnknown • Query the IUnknown interface for the interface implemented by your object • Call method in your object • Release all interfaces and un-initialize COM
Scratching the surface • COM is huge in every sense of the word • Server types • Threading models • DCOM • Object aggregation • Dual interfaces • Early binding (our example) vs. Late binding (used by VB) • Searching the registry for objects • …
By the way… • What is ActiveX • COM objects. • What are ActiveX controls • COM objects with additional interfaces implemented beyond the IUnknown interface
Further Reading • Win32 Platform SDK – Component Services • MSDN Technical Articles – Component Object Model • DirectX 7 SDK (good examples) • Any “how to” book on COM – you’ll need to guide you through the sea of function calls.