home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 Component’s Properties And Methods
Creating A Code Component
Setting A Code Component’s 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 component—formerly called OLE automation servers—as 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 you’re free to use the routine in that code component in other programs—just 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 doesn’t 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 interface—for 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 (we’ll 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 class’s properties and methods.

That’s how it works—it’s all about reusing your code. You get an object corresponding to a class in a code component like this, where we’re 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 object’s 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 object’s methods.

If you’ve 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 you’ve created your code component as an ActiveX DLL, that code component is an in-process server, which means it’ll run as part of the client application’s process.

There’s 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 they’re part of a global object. In practice, this means that you don’t have to create an object to use the methods and properties of this code component—you just add a reference to the code component in the client application, and you can use the component’s 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 client’s stack to pass arguments. That’s 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 don’t 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.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.