ni.com understanding com/activex jeff paulter staff software engineer thurs aug 17 10:15-11:30 a.m.,...
TRANSCRIPT
ni.comni.com
Understanding COM/ActiveX
Jeff Paulter
Staff Software Engineer
Thurs Aug 17
10:15-11:30 a.m., 1:45-3:00 p.m.
Ash (10A)
ni.comni.com
OutlineOutline
What is ActiveX? Why use COM/ActiveX What is COM? What are ActiveX/COM
• Servers• Objects• Interfaces• More
What is ActiveX? Why use COM/ActiveX What is COM? What are ActiveX/COM
• Servers• Objects• Interfaces• More
ni.comni.com
What is ActiveX all about?What is ActiveX all about?
Demonstrations• Document objects• Automation• Controls• Containers
Demonstrations• Document objects• Automation• Controls• Containers
ni.comni.com
What is ActiveX?What is ActiveX?
The name for technologies built on top of COM
Examples include:• Document objects• ActiveX controls• Automation• OLE for process control (OPC)
Grew out of OLE
The name for technologies built on top of COM
Examples include:• Document objects• ActiveX controls• Automation• OLE for process control (OPC)
Grew out of OLE
ni.comni.com
Motivation for COM/ActiveXMotivation for COM/ActiveX
Component software development• Reuse of binary software components• Create and use components from various
languages• Update components without recompiling or
breaking existing clients
Object-based model Distributed software Location transparency
Component software development• Reuse of binary software components• Create and use components from various
languages• Update components without recompiling or
breaking existing clients
Object-based model Distributed software Location transparency
ni.comni.com
What About DLLs?What About DLLs?
Calling conventions can vary DLLs are not object based (Flat API) Versioning can be difficult Memory management concerns Linkage issues
Calling conventions can vary DLLs are not object based (Flat API) Versioning can be difficult Memory management concerns Linkage issues
ni.comni.com
What is COM?What is COM?
Component object model Binary and network standard Run-time system
• Calls across network• Location transparency• Security
Component object model Binary and network standard Run-time system
• Calls across network• Location transparency• Security
ni.comni.com
COM ServersCOM Servers
Can be DLLs or EXEs Provide objects for clients
• A single server can provide implementations for more than one object
• The set of objects that a server provides and the relationships between them is referred to as an object hierarchy
Are activated by the COM run-time services
Can be DLLs or EXEs Provide objects for clients
• A single server can provide implementations for more than one object
• The set of objects that a server provides and the relationships between them is referred to as an object hierarchy
Are activated by the COM run-time services
ni.comni.com
Object HierarchyObject Hierarchy
Top Level Objects
(Creatable)
Lower Level Objects
(Non-Creatable)
ni.comni.com
COM ObjectsCOM Objects
Clients view objects as black boxes Clients communicate with objects only
through interfaces Objects can handle more than one interface
Clients view objects as black boxes Clients communicate with objects only
through interfaces Objects can handle more than one interface
ObjectInterface2
Interface1
Interface3
ni.comni.com
COM InterfacesCOM Interfaces
Sets of related methods Immutable contracts Implementations not described
• NO member data (functions only)
COM defines a standard for in-memory layout of interfaces
Sets of related methods Immutable contracts Implementations not described
• NO member data (functions only)
COM defines a standard for in-memory layout of interfaces
ni.comni.com
Interface BenefitsInterface Benefits
Compiler neutral Language neutral Object implementations can change without
affecting clients• Clients never need to be recompiled
Versioning
Compiler neutral Language neutral Object implementations can change without
affecting clients• Clients never need to be recompiled
Versioning
ni.comni.com
Sample InterfacesSample Interfaces
IAnimal• Eat• Sleep• Walk
IDog : IAnimal• Bark• Fetch• ChaseCat
IAnimal• Eat• Sleep• Walk
IDog : IAnimal• Bark• Fetch• ChaseCat
IGuard• SoundAlarm• AttackBadGuy
IGuard• SoundAlarm• AttackBadGuy
ni.comni.com
IUnknown InterfaceIUnknown Interface
Every COM interface inherits from IUnknown IUnknown includes functionality needed by
every object• Provides a mechanism for clients to obtain one
interface from another interface• Provides object lifetime manangement
Every COM interface inherits from IUnknown IUnknown includes functionality needed by
every object• Provides a mechanism for clients to obtain one
interface from another interface• Provides object lifetime manangement
ni.comni.com
IUnknown InterfaceIUnknown Interface
Mechanism to get from one interface on an object to another interface on the object
HRESULT QueryInterface (REFIID riid, void **ppvObject);
Mechanism to manage object lifetimes• Clients do not delete COM objects directly• COM objects are responsible for deleting themselves• COM objects are reference counted
ULONG AddRef ( );
ULONG Release ( );
Mechanism to get from one interface on an object to another interface on the object
HRESULT QueryInterface (REFIID riid, void **ppvObject);
Mechanism to manage object lifetimes• Clients do not delete COM objects directly• COM objects are responsible for deleting themselves• COM objects are reference counted
ULONG AddRef ( );
ULONG Release ( );
ni.comni.com
Registration of ServersRegistration of Servers
Servers are registered with the system
HKEY_CLASSES_ROOT
CLSID
{E312522E-A7B7-11D1-A52E-0000F8751BA7}Default = “Guard Dog Object” InprocServer32 = "D:\Examples\AnimalLib\
AnimalLib.dll"
AnimalLib.GuardDogCLSID
Default = “{E312522E-A7B7-11D1-A52E-0000F8751BA7}”
Servers are registered with the system
HKEY_CLASSES_ROOT
CLSID
{E312522E-A7B7-11D1-A52E-0000F8751BA7}Default = “Guard Dog Object” InprocServer32 = "D:\Examples\AnimalLib\
AnimalLib.dll"
AnimalLib.GuardDogCLSID
Default = “{E312522E-A7B7-11D1-A52E-0000F8751BA7}”
ni.comni.com
Event InterfacesEvent Interfaces
Interface provides communication from the server back to the client
Server defines an interface that it calls to notify the client that an event occurred
Client supplies server with a pointer to the interface
Client’s code is executed when the server calls methods in the event interface
Interface provides communication from the server back to the client
Server defines an interface that it calls to notify the client that an event occurred
Client supplies server with a pointer to the interface
Client’s code is executed when the server calls methods in the event interface
ni.comni.com
Example Event InterfaceExample Event Interface
IDogEvents• AmHungry• WantAttention• NeedToGoOutside
IDogEvents• AmHungry• WantAttention• NeedToGoOutside
ni.comni.com
IDispatch InterfaceIDispatch Interface
Automation servers must handle IDispatch ActiveX controls must handle IDispatch IDispatch gives scripting clients access to
COM objects • Microsoft Office• Internet Explorer• Old versions of Visual Basic
Automation servers must handle IDispatch ActiveX controls must handle IDispatch IDispatch gives scripting clients access to
COM objects • Microsoft Office• Internet Explorer• Old versions of Visual Basic
ni.comni.com
IDispatch InterfaceIDispatch Interface
IDispatch::Invoke provides access to an entire interface through a single function
Clients use the Invoke function to call all methods• Specify method by method ID• Pass parameters in a single large structure
Invoke understands only Automation types• Yes: int, double, enum, boolean, structures• Yes: BSTR, SafeArray, Variant• No: unions, C style arrays, C style strings
IDispatch::Invoke provides access to an entire interface through a single function
Clients use the Invoke function to call all methods• Specify method by method ID• Pass parameters in a single large structure
Invoke understands only Automation types• Yes: int, double, enum, boolean, structures• Yes: BSTR, SafeArray, Variant• No: unions, C style arrays, C style strings
ni.comni.com
COM/ActiveX ProgrammingCOM/ActiveX Programming
Many environments provide some tools to make writing COM servers and clients easier• Wrappers for accessing COM servers• Wizards to create skeleton servers or controls• Tools to add interfaces, methods, properties• Free implementations of some standard interfaces
Measurement Studio• Visual Basic, Visual C++, LabWindows/CVI
LabVIEW™
Many environments provide some tools to make writing COM servers and clients easier• Wrappers for accessing COM servers• Wizards to create skeleton servers or controls• Tools to add interfaces, methods, properties• Free implementations of some standard interfaces
Measurement Studio• Visual Basic, Visual C++, LabWindows/CVI
LabVIEW™
ni.comni.com
ActiveX and NI ProductsActiveX and NI Products
ActiveX controls• Measurement Studio
Containers• LabVIEW, BridgeVIEW™
Automation servers• LabVIEW, Measurement Studio, HiQ™, DataSocket™
Clients• LabVIEW, BridgeVIEW, Measurement Studio, TestStand™
ActiveX controls• Measurement Studio
Containers• LabVIEW, BridgeVIEW™
Automation servers• LabVIEW, Measurement Studio, HiQ™, DataSocket™
Clients• LabVIEW, BridgeVIEW, Measurement Studio, TestStand™
ni.comni.com
COM/ActiveX ResourcesCOM/ActiveX Resources
Essential COM• Don Box (Addison-Wesley, 1998)
Inside COM• Dale Rogerson (Microsoft Press, 1997)
Various MSDN Articles NI Product Examples
Essential COM• Don Box (Addison-Wesley, 1998)
Inside COM• Dale Rogerson (Microsoft Press, 1997)
Various MSDN Articles NI Product Examples