1 / 28

Introduction to COM and ActiveX Controls

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

Download Presentation

Introduction to COM and ActiveX Controls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to COM and ActiveX Controls

  2. What is an object? • In the Fayad sense of the word 

  3. 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?

  4. 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)

  5. 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……

  6. 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

  7. 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?

  8. 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

  9. Conceptually, how does COM work?

  10. 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?

  11. 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

  12. 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

  13. What is an interface? What is a function or method? What’s the difference?

  14. 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 • …

  15. 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?

  16. 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.

  17. 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

  18. 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

  19. 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

  20. 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…

  21. 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

  22. 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)

  23. 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.

  24. 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.

  25. 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

  26. 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 • …

  27. By the way… • What is ActiveX • COM objects. • What are ActiveX controls • COM objects with additional interfaces implemented beyond the IUnknown interface

  28. 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.

More Related