Introduction to COM and ActiveX Controls

28
Introduction to COM Introduction to COM and ActiveX Controls and ActiveX Controls

description

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 - PowerPoint PPT Presentation

Transcript of Introduction to COM and ActiveX Controls

Page 1: Introduction to COM and ActiveX Controls

Introduction to COM and Introduction to COM and ActiveX ControlsActiveX Controls

Page 2: Introduction to COM and ActiveX Controls

What is an object?What is an object?

In the Fayad sense of the word

Page 3: Introduction to COM and ActiveX Controls

Object Oriented Themes Object Oriented Themes (Object Oriented Modeling and Design, Prentice-Hall, 1991)(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?

Page 4: Introduction to COM and ActiveX Controls

Object oriented themes and Object oriented themes and C++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)

Page 5: Introduction to COM and ActiveX Controls

Wouldn’t it be nice if…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……

Page 6: Introduction to COM and ActiveX Controls

What is COM?What is COM?

COM – Component Object ModelThe basis for all of Microsoft’s component

programming technologyA binary specification for object

interoperability– Define the specification for binary objects

written in dissimilar languages to be used interchangeably

Page 7: Introduction to COM and ActiveX Controls

Where are COM objects used Where are COM objects used on Microsoft Platforms?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?

Page 8: Introduction to COM and ActiveX Controls

Why use COM?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

Page 9: Introduction to COM and ActiveX Controls

Conceptually, how does COM Conceptually, how does COM work?work?

Page 10: Introduction to COM and ActiveX Controls

A glimpse under COM’s hoodA 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?

Page 11: Introduction to COM and ActiveX Controls

COM accomplishes these COM accomplishes these goals by defining: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

Page 12: Introduction to COM and ActiveX Controls

What functions are guaranteed to What functions are guaranteed to be in a COM object?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

Page 13: Introduction to COM and ActiveX Controls

What is an interface?What is an interface?

What is a function or method?What is a function or method?

What’s the difference?What’s the difference?

Page 14: Introduction to COM and ActiveX Controls

Interface = ContractInterface = 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

Page 15: Introduction to COM and ActiveX Controls

IUnknown interfaceIUnknown 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?

Page 16: Introduction to COM and ActiveX Controls

Fishing for functionalityFishing 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.

Page 17: Introduction to COM and ActiveX Controls

GUIDGUID

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

Page 18: Introduction to COM and ActiveX Controls

IClassFactoryIClassFactory

Used to create an instance of a COM objectInherits from IUnknownDefines 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

Page 19: Introduction to COM and ActiveX Controls

ReviewReview

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

Page 20: Introduction to COM and ActiveX Controls

Quick note on where COM Quick note on where COM info is storedinfo 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…

Page 21: Introduction to COM and ActiveX Controls

Shortest distance between Shortest distance between you and writing COM objectsyou 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

Page 22: Introduction to COM and ActiveX Controls

How to use ATL in VC++ 6.0How to use ATL in VC++ 6.0

1. Select File->New->Projects (pane)1. ATL COM AppWizard (from list)

2. Dynamic Link Library (radio button)

3. Finish (button)

2. Select->Insert1. New ATL Object

1. Objects (left list box)

2. Simple Object (right selection box)

Page 23: Introduction to COM and ActiveX Controls

continuedcontinued

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

2. Select OK3. Right click on your newly created COM interface in

the “Class View” (left pane) and select “Add Method”

1. Enter the name of the method2. Write some code in the stub that is created

4. Compile it – as part of the compilation process, the program regsrv32.exe is run to register your object.

Page 24: Introduction to COM and ActiveX Controls

How is your new COM object How is your new COM object packagedpackaged

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

Page 25: Introduction to COM and ActiveX Controls

How do you use your new How do you use your new objectobject

Write a test program!1. Include a few files that were created by ATL for your

project2. Initialize COM libraries in main()3. Create a pointer to IClassFactory for your object4. Use IClassFactory to create an interface pointer to

IUnknown5. Query the IUnknown interface for the interface

implemented by your object6. Call method in your object7. Release all interfaces and un-initialize COM

Page 26: Introduction to COM and ActiveX Controls

Scratching the surfaceScratching 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– …

Page 27: Introduction to COM and ActiveX Controls

By the way…By the way…

What is ActiveX– COM objects.

What are ActiveX controls– COM objects with additional interfaces

implemented beyond the IUnknown interface

Page 28: Introduction to COM and ActiveX Controls

Further ReadingFurther Reading

1. Win32 Platform SDK – Component Services

2. MSDN Technical Articles – Component Object Model

3. DirectX 7 SDK (good examples)

4. Any “how to” book on COM – you’ll need to guide you through the sea of function calls.