|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 27 Creating Code Components (OLE Automation)
- If you need an immediate solution to:
- Using A Code Component From A Client Application
- Creating An Object From A Class
- Using A Code Components Properties And Methods
- Creating A Code Component
- Setting A Code Components Project Type: In-Process Or Out-Of-Process
- Adding A Property To A Code Component
- Adding A Get/Let Property To A Code Component
- Adding A Method To A Code Component
- Passing Arguments To A Code Component Method
- Passing Optional Arguments To A Code Component Method
- Testing A Code Component With A Second Instance Of Visual Basic
- Creating And Registering An In-Process Code Component
- Creating And Registering An Out-Of-Process Code Component
- Using The Class Initialize Event
- Using The Class Terminate Event
- Global Objects: Using Code Components Without Creating An Object
- Destroying A Code Component Object
- Using Forms From Code Components
- Creating Dialog Box Libraries In Code Components
- Designing Multithreaded In-Process Components
- Designing Multithreaded Out-Of-Process Components
In Depth
You can think of a code componentformerly called OLE automation serversas a library of objects, ready to be used by other applications (called client applications). For example, you might have a terrific routine to sort records that you want to use in a dozen different programs. You can put that routine in a code component, register it with Windows, and then youre free to use the routine in that code component in other programsjust as with an ActiveX control, the other programs can call the code components methods and properties.
A code component is like an ActiveX control or document that doesnt create a visual display in a host application. Code components provide access to methods and properties through code, not a direct graphical user interface (although code components themselves can support a user interfacefor example, you might have a library of dialog boxes that form a code component).
In fact, code components are the third part of the standard ActiveX object set (collectively called ActiveX components) that Visual Basic supports: ActiveX controls, ActiveX documents, and code components. You build code components as ActiveX EXEs or ActiveX DLLs, much as you do with ActiveX controls or documents (the difference is that the DLLs are in-process code components, and the EXEs are out-of-process code components).
Code Components: Classes And Objects
When you create a code component, you add code in class module(s), and when you register the code component with Windows, you make the class(es) available to client applications. Those applications, in turn, can add a reference to your code component and create an object of the class they want to use with the New, CreateObject, or other Visual Basic instruction (well see in detail how to create objects later in this chapter). When the client application has an object corresponding to one of your classes, it can use that classs properties and methods.
Thats how it worksits all about reusing your code. You get an object corresponding to a class in a code component like this, where were creating an object, objCalendar, of the class CalendarClass from the hypothetical code component named PlannerCodeComponent:
Dim objCalendar As Object
Set objCalendar = CreateObject("PlannerCodeComponent.CalendarClass")
...
Then you can use that objects properties and methods to give you access to the code in the class, something like this:
Dim objCalendar As Object
Set objCalendar = CreateObject("PlannerCodeComponent.CalendarClass")
objCalendar.Days = 365 'Use a property
intWorkDays = objCalendar.CalculateWorkDays 'Use a method
...
TIP: In fact, code component objects can themselves contain other objects (called dependent objects), and you create object variables of those types using the code component objects methods.
If youve created your code component as an ActiveX EXE, that code component is an out-of-process server and runs separately from the client application; if youve created your code component as an ActiveX DLL, that code component is an in-process server, which means itll run as part of the client applications process.
Theres even a way to use code components without creating an object. To make it easy to create code components that can be used with desktop tools like the Microsoft Office Suite, Visual Basic allows you to label objects in a code component as global, which means theyre part of a global object. In practice, this means that you dont have to create an object to use the methods and properties of this code componentyou just add a reference to the code component in the client application, and you can use the components properties and methods as though they were part of the client application.
A client application and an in-process component share the same memory space, so calls to the methods of an in-process code component can use the clients stack to pass arguments. Thats not possible for an out-of-process component; there, method arguments must be moved across the memory boundary between the two processes, which is called marshaling.
Code Components And Threads
When you start working with another application, as code components do, you share threads of execution with that application in a joint process. As a default, the Microsoft Component Object Model (COM) deals with this situation by serializing thread operations. That is, the operations are queued and processed, one at a time, until they all have been completed.
That process is safe, and you dont have to think about it except for performance issues. In a multithreading operating environment, serialization protects single-threaded objects from overlapping client operations. However, serializing single-threaded components also means that operations can be blocked, and can stay blocked for some time, which can be frustrating (especially when short operations are blocked by long ones).
Visual Basic has two ways to avoiding blocked calls: multithreading and SingleUse objects.
|