All Categories :
ActiveX
Chapter 21
Advanced ActiveX Techniques
CONTENTS
The development of ActiveX, Java, and other Internet programming
solutions has brought into vogue the idea of software components.
Instead of designing computer software as a large, self-contained
tool that performs a wide range of tasks, software is broken into
smaller modules called components. These components are created
to accomplish a specific task, and they are designed to be generic
enough to work with a wide range of software. The goal of component-based
programming is to make software design simpler, cheaper, faster,
and more reusable.
Components make the job of software development more manageable,
under the premise that it would be easier to build a fence from
a stack of wooden slats, nails, and posts than it would be if
someone handed you an ax and pointed to a nearby forest.
ActiveX is the latest and greatest incarnation of the Component
Object Model, a standard for software development that Microsoft
has developed in the past five years. In this chapter, you'll
learn about the Component Object Model and some other advanced
topics in regard to ActiveX and VBScript.
ActiveX is built on the Component Object Model, often abbreviated
as COM. Because ActiveX is a refinement of the existing COM standard,
thousands of programmers familiar with COM can learn ActiveX much
more easily.
COM establishes a standard, structured way for programs on the
same desktop to share software components. A component can be
set up to perform a task, and any other software that wants to
use the component can do so by using methods described in the
COM. ActiveX extends this system by making it possible to share
components across the Internet.
Software components-also called objects-present
a different way to organize a computer program. Most software
that is developed without components puts all of its functionality
into a single, self-contained program or set of programs. Figure
21.1 shows NerdStar, a fictional example of a word processing
program developed without using components.
Figure 21.1 : A program with four self-contained functions
and no parts using COM.
NerdStar is a self-contained program that does all of its own
work. It has functions to handle text editing, spell checking,
file handling, and document conversion. If NerdStar needs to accomplish
one of these tasks, it calls upon its own functions.
One of the biggest reasons to develop a program this way is because
everything is fully compatible. Every part of NerdStar works with
every other part (or at least it should!), because each part was
designed in conjunction with the rest.
Most software that has been developed in the past 20 years is
like NerdStar in its approach, containing the functions that it
needs inside a single program or set of programs.
This approach has been highly successful for many years, but it
can prove costly when software needs to be improved, modified,
or used to create other software.
After many years of success with NerdStar, the developers invent
a new spell-checking program, called Spellvis, that is faster
and more comprehensive than the leading brand. The decision is
made to replace NerdStar's spell-checking routine by putting Spellvis
inside a new version of NerdStar. This is not necessarily an easy
task.
The difficulty arises because NerdStar's original spell-checking
function is thoroughly entwined with the other parts of NerdStar.
In a program that is composed of thousands of lines of code, it
will be difficult and time-consuming to find all the lines that
will be affected if the original spell-checking function is removed.
The rest of NerdStar also must be changed in order to work with
the Spellvis code inside it, because Spellvis might not operate
in a similar manner to the original spell checker.
In order to make NerdStar easier to maintain, expand, and use
with other software, it is rewritten using the Component Object
Model. The newly christened NerdStar 95 breaks its text editing,
file handling, and document conversion functions into their own
components, and it also adds Spellvis as its own component. Figure
21.2 shows NerdStar 95, the new version of the word processing
program designed with the Component Object Model. All of its functions
have been developed as components (shown here as plugs), and the
main NerdStar 95 program is a series of sockets. Each component
has the job of accomplishing a specific task or set of tasks.
The main job of the central NerdStar 95 program is to enable the
components to communicate with each other.
Figure 21.2 : A program with all of its components using
COM.
Using Figure 21.2 as a reference, the Component Object Model is
the standard that defines what a plug is, what a socket is, and
how the two connect to each other. When a component follows the
standard and a program follows the standard, they can be used
together.
COM is a comprehensive specification, but it does not add any
overhead to software. There is no COM interpreter or any kind
of controlling routine that must be used between the component
and the program.
Developing software using COM is a significant undertaking, but
NerdStar 95's programmers have an immediate benefit in regard
to Spellvis. The company also has introduced Pagerrific, a Web
page editor that can be used to create, modify, and view HTML
documents.
Pagerrific has its own special text editing, file handling, and
document conversion functions, but the company wants to add Spellvis
to this new program. Using the COM model, the same Spellvis component
used with NerdStar 95 can be plugged into Pagerrific.
Figure 21.3 shows Pagerrific, a program that uses COM for Spellvis
and has self-contained functions for the rest of its tasks.
Figure 21.3 : A program with three self-contained functions
and a Spellvis component using COM.
Because they use COM, the NerdStar 95 developers will benefit
from the following:
- Each of NerdStar 95's components can be used in other software,
perhaps even being marketed to other software developers who want
to add NerdStar 95 functions to their own products.
- Adding new components to NerdStar 95 can be done more easily,
because the relationship between existing components is well-defined.
- Replacing components with better upgrades is easier: If a
new Spellvis is created that is compatible with the old Spellvis,
adding it to NerdStar 95 is simply a matter of swapping components.
- Maintenance of NerdStar 95 requires less work, because bugs
are easier to isolate in the specific component where the problems
occur.
These benefits have been discussed in relation to COM, because
it is the underlying structure of ActiveX, as you will see in
the following section. However, other solutions have been offered
for the development of component software. Many of these are focused
on the programming language, unlike the Component Object Model,
which focuses on the executable programs and can be achieved with
many languages. As an example of a component system that differs
from COM, object-oriented programming languages such as Java can
be used to divide software into self-contained components that
can be plugged into other programs as needed.
Although software components and object-oriented programming have
reached a new peak in popularity, ActiveX is the product of years
of development at Microsoft. In some ways, it can be considered
the fourth step in Microsoft's shift toward component software
development.
The four steps Microsoft took toward ActiveX are as follows:
- First came Dynamic Data Exchange (DDE), which was the original
protocol for messages to be sent between Windows programs.
- Then came Object Linking and Embedding (OLE), a development
of DDE, which provided a way to create visual links between programs
inside an application.
- OLE was followed by the Component Object Model, which has
become an almost universal industry standard for the use and design
of OLE applications.
- Finally, Microsoft arrived at ActiveX, a modified form of
COM that was developed for use in Internet-related software.
COM software components are usable across different languages;
a component written with one language can be used in software
written with another language. ActiveX components, also called
controls, can be used
by programmers writing with Microsoft Visual Basic, Micro Focus
Visual Object COBOL, PowerBuilder, and other popular development
tools. COM has been used as the underlying foundation in applications
to offer 3D graphics, transaction processing, and compound documents.
This last area is one that you might have encountered with word
processing software, as shown in the previous NerdStar example.
One of the areas in which components have been used to great advantage
is in office suite software, which offers a word processor, spreadsheet,
and other software as a unified set-or suite-of
programs. For instance, COM can be used to put a functional spreadsheet
inside a text document. The only new programming required in the
word processing software would be to plug in the spreadsheet's
component.
The COM standard was pioneered by Microsoft, and its progress
has largely been at the initiative of the company. However, Microsoft
announced recently that it was releasing management of COM, the
related Distributed Component Object Model, and ActiveX to an
international standards body-the ActiveX Working Group.
The standards group-which consists of Microsoft customers, software
vendors, and hardware producers-will be in charge of standardizing
these tools and fostering future development. You can find out
more about the group at the following Web site:
http://www.activex.org/
The move was made to provide evidence of Microsoft's support for
open Internet standards. It could give ActiveX the same kind of
push that Java received when Sun Microsystems made the source
code of the language publicly available. COM is an open standard,
documented from top to bottom for the use of software vendors
and other interested parties.
The Component Object Model specification includes a set of standard
Application Programming Interfaces (APIs) and network protocols.
Software components must follow a standard in binary form, but
they can be created with programming languages that can use these
APIs to produce components in that binary form.
One advantage of a system such as COM is that it encourages the
development of objects that can be used by other programmers.
If an application developer needs a special graphics conversion
function and a software component is available that can accomplish
the task, the developer can plug in the component and avoid the
hours required to write new code.
The author of the graphics conversion utility can make the component
available easily in binary form, which removes the significant
difficulty of making source code available to developers. The
focus would be in the right place-developing and enhancing the
graphics conversion component-instead of protecting the code from
being stolen or used incorrectly.
As shown in ActiveX's use of the OBJECT tag, version
control is also possible so that new components are only installed
if needed. The following is an example of a CODE attribute
in an OBJECT tag:
CODE=http://www.ibfake.com/Spellvis.ocx#Version=3
When an ActiveX-enabled browser encounters an attribute with a
version declared in the HTML code, its component download mechanism
only retrieves and installs the component if its version is newer
than versions the user already has. If no version is included
in the CODE attribute, the presence of any version of
the component stops it from being downloaded again.
A new wrinkle on the Component Object Model and ActiveX system
is the Distributed Component Object Model. This was introduced
by Microsoft as a way for software components to communicate with
other components across both the Internet and intranets. Described
as "COM with a longer wire" by Microsoft, DCOM is an
extension of COM for use on a broader scale of multi-user applications.
During an early stage of development, DCOM was called Network
OLE.
As an ActiveX technology, DCOM provides native support for Internet
protocols such as TCP/IP and HTTP, and for the Java language.
Because DCOM is language-neutral, Java applets can use DCOM to
communicate with each other and with ActiveX controls.
Although COM has been described as an approach that is different
from object-oriented programming (OOP), a component system such
as COM embodies the same principles. An object, according to OOP
terminology, is software that includes two things: functions that
indicate how the object will behave, and data that is used to
perform those functions. When you combine data and programming
to do something with the data, an object contains everything it
needs to perform a task. Because of this, when you use an object,
you can focus on what it does, not how it does it.
This is called encapsulation.
The users of an object don't need-and don't have access to-the
internal structure of an object. They can focus on how the object
behaves. Referring back to the NerdStar example, a programmer
who wants to use the Spellvis component only needs to know how
to make it spell check a document.
COM is a system of communication between an object-a component,
in other words-and anything that wants to work with the object.
Using COM, this communication is done using a request called an
interface. A COM interface can be thought of as a contract between
two pieces of software. By creating such a strong standard, COM
and descendants such as ActiveX provide a method for software
to operate together on a large scale.
A lot of Microsoft's Internet strategy involves adapting the company's
existing solutions to the Internet. The contents of this book
are full evidence of this: VBScript is based on the highly successful
Visual Basic programming language, and ActiveX is a successor
to OCX development. Cabinet files (also called Cab files) are
another example of Microsoft adapting one of its tools for the
Net.
Cabinet files are a way to archive Java applets or ActiveX controls
into a single file. The Cabinet file archive is compressed into
a single file, and it can be used to hold all programs, images,
and other files that are used by an applet or ActiveX control.
Also, Cab files can be signed using the same code-signing system
as ActiveX controls. This is true of Java files in addition to
ActiveX controls, providing an extra level of security for Java
programs on the World Wide Web.
The Cab file format is based on Lempel-Ziv compression, one of
the popular compression systems, and Microsoft has made a set
of command-line tools available for use with the Cabinet archival
system. The set of tools is called CABARC, the Cabinet Archiver.
The following Web page, located in the Site Builder section of
Microsoft's site, is the current address where Cab tools can be
downloaded:
http://www.microsoft.com/workshop/java/cab-f.htm
Because Java applets often require the use of several class files,
in addition to any image or data files that are required, the
Cab system is especially useful with that language.
Using CABARC, a Java program can be assembled into a single Cabinet
archive file with a .cab file extension.
This file would be placed on a Web site, and a new parameter is
used with the <APPLET> tag to indicate the location
of the archive.
The new parameter, cabbase, is shown in the following
example of an <APPLET> tag:
<APPLET CODE="LearnToPolka.class" WIDTH=220 HEIGHT=500>
<PARAM NAME="cabbase" VALUE="learn2polka.cab">
</APPLET>
If the cabbase parameter is found in an <APPLET>
tag and the Web browser supports Cabinet files, cabbase
will be used when the applet is loaded. If the cabbase
parameter is not present, the CODE attribute of the <APPLET>
tag will be used to indicate the applet's main class file. At
the time of this writing, only Microsoft Internet Explorer 3.0
supports Cabinet files. Using cabbase does not interfere
with the use of Java applets on browsers that do not support this
archival system. A browser such as Netscape Navigator 2.02 would
ignore the cabbase parameter and use the CODE
and CODEBASE attribute to find the main Java class file
to load.
This .cab file contains one or more files that are all
downloaded together as a single compressed file. This Cabinet
file should only include files that must be downloaded, and should
not include any supporting programs such as dynamic link libraries
(DLLs). These DLLs might already be present on the user's system,
so they would be redundant to download in a Cabinet archive.
The most recent version of the CABARC tool operates in
a manner similar to archival utilities such as PKZIP, LHArc, and
others. It also supports the use of an information file that controls
how the .cab file will be installed. This file has the
.inf file extension, and it is included in the Cabinet
archive.
As stated previously, COM software components such as ActiveX
controls can be developed with several different programming languages.
The most common choice, if Web pages on the subject are any indication,
is Microsoft Visual C++. Using Visual C++, COM software can be
written using one of three development libraries: the ActiveX
Template Library, Microsoft Foundation Class Library, or the BaseCtl
framework. ActiveX controls can use a variety of programming languages
from Microsoft for component design in addition to Visual C++-Visual
Basic, Visual J++, and even Word or Excel's programming languages.
ActiveX control programming is beyond the scope of this book,
but if you're interested in developing your own controls, this
section provides the information you need to get started.
Developing new controls for ActiveX requires the use of the ActiveX
Software Development Kit (SDK) from Microsoft. It can be downloaded
from the following Web page:
http://www.microsoft.com/intdev/sdk/
Using the ActiveX SDK requires the following software:
- The final release of Microsoft Internet Explorer 3.0
- A compiler that is compatible with the ActiveX SDK, which
includes Visual C++ 4.1 or Visual C++ 4.2 with a patch that is
being made available from Microsoft
- Windows 95 or the final version of Windows NT 4.0
- The Win32 SDK for Windows NT 4.0
After downloading the ActiveX SDK, you end up with a single executable
file, activex.exe, that will unpack itself. Instead of
clicking the mouse to run this file, you need to use a command-line
prompt or Run command from the Start menu to add an argument.
After you have moved the activex.exe to the root file
directory in which you want it to be stored, you can unpack the
file by entering the following:
activex.exe -d
You can then install the SDK by running the SETUPSDK.EXE
program. Documentation for the development kit can be found in
HTML form in the following subdirectory: \INetSDK\Help\Default.htm.
The following example demonstrates the fact that ActiveX technology
has been with us for some time. You will take a Grid OCX file,
which you can find on the CD-ROM, and add it into a Layout control,
just like any other ActiveX control. The Grid control is normally
used as a custom control in Visual Basic 4. Figure 21.4 shows
the control you will use in the VB4 environment. If you have your
own copy of VB4, you can use the grid32.ocx file from
your windows/system directory.
Figure 21.4 : The grid32.ocx control
in the VB4 development environment.
Using VB4 custom controls is not really recommended if you are
building a Web page for public consumption. However, you might
find that when building intranet applications in which you are
trying to mirror the functionality of a current VB application,
you could be forced into using specialized OCX files.
For a start, OCX files are somewhat "fatter" than their
ActiveX brethren, increasing download time across the Web. OCX
files do not have the Authentication and Security certification
that ActiveX files have, and as you will see later in this example,
this can cause alarm among users faced with what can appear to
be a potential virus warning! Finally, although you will see how
to include a CodeBase variable for the control, OCX files
are best used from the local drive-in this case, from windows/system.
If you are building an intranet application, it is very likely
that the users of the application will already have the controls
you include loaded onto their machine to enable the windows-based
version of the application to operate.
So, now you are ready to start creating an application using a
VB4 custom control.
- The first thing to do is open the ActiveX Control Pad, and
select New HTML Layout from the File menu.
- To add the control to your toolbox, select the second page
of the toolbox and right-click anywhere on the page.
- Select Additional Controls from the pop-up menu. The additional
controls dialog is now displayed. (See Figure 21.5.)
Figure 21.5 : Select the Grid control in the Additional
Controls dialog.
- Scroll down the list until you reach Grid Control, check the
box to the left of the name (as shown in Figure 21.5), and click
OK to load the control into your toolbox.
- Add the Grid control to the layout by selecting it from the
toolbox and then clicking on the layout and dragging it to the
required size.
- You need to change only a few properties for this example.
First, change the BackColor property to white.
- Now change the Rows property value to 6.
- Change the Cols property value to 6.
- Optionally, you can set the CodeBase property to
be that of the Web location of the .ocx file, as in the
following example:
http://www.mydomain.com/subdir/grid32.ocx
In any case, during testing it's best to leave this blank
so that your local copy is used. This gives you the added advantage
of not needing to be online to test and debug the application.
Your Layout should now look like the one in Figure 21.6.
Figure 21.6 : The Grid control added to your Layout.
- Save the layout as grid.alx.
- The next thing you need to do is declare several global constants
and arrays. You might remember from previous chapters that the
Script Wizard enables you to add Global Variables. So launch the
Script Wizard.
- Right-click anywhere in the right Actions pane.
- Select New Global Variable from the pop-up menu.
- Enter ColHeads(5) in the New Global Variable dialog
box. This declares a Global Single Dimension array.
- Repeat steps 12 and 13 to add another single-dimension array
to store the Row Titles.
RowTitles(5)
- Now declare the following multidimension array in the same
way:
DataVals(5,5)
- Declare the following Global Constants, like this:
OFF_RENT
HEAT_LIGHT
SALARIES
VEHICLES
JAN
FEB
MAR
APR
MAY
This example simply displays a predefined data set in the
array. The arrays you just declared are used to hold the column
headers, row titles, and cell data. The arrays are populated and
the data is added to the Grid control via the Grid control's OnLoad
event.
- In the left events pane, select Layout1 and then
its OnLoad event.
- Enter the following code in the event handler:
ColHeads(JAN)="January"
ColHeads(FEB)="February"
ColHeads(MAR)="March"
ColHeads(APR)="April"
ColHeads(MAY)="May"
RowTitles(OFF_RENT)="Office Rent"
RowTitles(HEAT_LIGHT)="Heat & Light"
RowTitles(SALARIES)="Salaries"
RowTitles(VEHICLES)="Vehicles"
RowTitles(5)="TOTAL"
DataVals(JAN,OFF_RENT) = 1000
DataVals(JAN,HEAT_LIGHT) = 70
DataVals(JAN,SALARIES) = 10000
DataVals(JAN,VEHICLES) = 500
DataVals(FEB,OFF_RENT) = 1000
DataVals(FEB,HEAT_LIGHT) = 80
DataVals(FEB,SALARIES) = 11100
DataVals(FEB,VEHICLES) = 500
DataVals(MAR,OFF_RENT) = 1000
DataVals(MAR,HEAT_LIGHT) = 80
DataVals(MAR,SALARIES) = 12000
DataVals(MAR,VEHICLES) = 600
DataVals(APR,OFF_RENT) = 1000
DataVals(APR,HEAT_LIGHT) = 70
DataVals(APR,SALARIES) = 12000
DataVals(APR,VEHICLES) = 600
DataVals(MAY,OFF_RENT) = 1000
DataVals(MAY,HEAT_LIGHT) = 60
DataVals(MAY,SALARIES) = 14000
DataVals(MAY,VEHICLES) = 600
OFF_RENT = 1
HEAT_LIGHT = 2
SALARIES = 3
VEHICLES = 4
JAN = 1
FEB = 2
MAR = 3
APR = 4
MAY = 5
Grid1.ColWidth(0) = 1000
Grid1.Row = 0
For x = 1 To 5
Grid1.ColWidth(x) = 700
Grid1.Col = x
Grid1.Text = ColHeads(x)
Next
Grid1.Col = 0
For y = 1 To 5
Grid1.Row = y
Grid1.Text = RowTitles(y)
Next
For x = 1 To 5
totval = 0
Grid1.Col = x
For y = 1 To 4
Grid1.Row = y
Grid1.Text = DataVals(x,y)
totVal = totval + DataVals(x,y)
Next
Grid1.Row = 5
Grid1.Text = totVal
Next
Your event handler should now look like the one in Figure
21.7.
Figure 21.7 : The Layout1_OnLoad event
handler in the Script Wizard.
- Click OK and the Script Wizard generates the appropriate
VBScript code.
- Save the .alx file. Your next step is to create the
HTML file that will control the Layout you just built.
- Open a new HTML template by selecting New HTML from the File
menu.
- Add a bgcolor="white" element to the <BODY>
tag.
- Add a <CENTER> tag under the <BODY>
tag.
- With your cursor directly under the <CENTER>
tag, select Insert HTML Layout from the Edit menu.
- Select grid.alx from the dialog and press OK. The
HTML declarations for the Layout are now automatically added to
the HTML file, as shown in Figure 21.8.
Figure 21.8 : The layout control added to the HTML file.
- Save the HTML file as grid.htm.
As I mentioned earlier, standard .ocx files do not have
the code authentication certificate that is present in true ActiveX
controls. This can lead to the browser automatically refusing
the control on security grounds. However, there is a way to configure
your browser to allow certain controls to be downloaded and used
at your discretion.
- Open MSIE3.0 and select View|Options.
- Click the Security tag (shown in Figure 21.9).
Figure 21.9 : The MSIE 3.0 security options.
- Click the Safety Level button in the Active Content section.
This displays the range of ActiveX safety options available to
you.
- Check the middle (Medium Security) option, as shown in Figure
21.10.
Figure 21.10: The MSIE ActiveX options.
- Click OK, and click OK again, so that you can run the file.
As the file is loading, the warning message shown in Figure 21.11
is displayed. Click OK.
Figure 21.11: The security warning dialog.
The final result of all the steps is shown in Figure 21.12.
Figure 21.12: The VB4 Grid OCX control Web page.
ActiveX controls allow the creation of Web applications that mirror
the functionality of Windows applications, with the added advantages
of ease of distribution and, eventually, cross-platform usability.
Information Technology departments around the world are already
gearing up to face the challenge of converting desktop windows
applications into Web applications to be operated via the company's
intranet infrastructure.
With the next version of MSIE just around the corner, in which
the desktop and Web (whether via an intranet or Internet) become
a seamless single platform, it is important that you understand
how your Windows applications can be transformed into Web applications.
In this chapter, you have learned the basics of COM and ActiveX,
and you've also seen how easy it is to treat OCX custom controls
as though they are ActiveX controls-even if this is a stopgap
measure until the control you require is made available as a fully
Internet-aware ActiveX control.
Unless you feel the need to lie down in a darkened room for awhile,
you can read the following chapters and find out how to put your
newfound knowledge to good use:
- To discover the fundamentals of ActiveX, see Chapter 1 "Getting
to Grips with ActiveX."
- To learn about how ActiveX controls are used in conjunction
with VBScript, see Chapter 12, "Using VBScript with ActiveX
Controls."
- To learn more about using the ActiveX Control Pad with forms,
see Chapter 13, "Implementing an ActiveX Menu Control."
- To learn how to use Java applets with VBScript, see Chapter
15, "Using Java Applets with VBScript."
- To learn more about converting Visual Basic applications for
use on the Web, see Chapter 22, "Converting a Visual Basic
Application for the Web."
- To learn more about intranet-type business applications, see
Chapter 23, "Real-Life Examples IV."
Q: | I'm not sure what you mean by the statement that software components must follow a standard in binary form. What does this Component Object Model requirement mean by binary compatibility?
|
A: | This means that the executable file (or files) associated with the component must fully support the COM standard. ActiveX controls, and other COM software, can be created with a number of different languages, so the source code of a COM object will vary widely from the source code of another COM object. However, the end result of those two COM objects will be highly similar, because the executable form of these components must follow the COM standard completely. This executable form is also called the binary form of a program, so ActiveX and COM feature binary compatibility.
|
Q: | Does ActiveX replace the Component Object Model?
|
A: | ActiveX is a streamlined form of the COM standard intended for Internet and intranet use. It has several revisions from COM to make it more suitable for Internet programming, such as reduced file size to speed up remote downloading. It does not, however, fully supplant the COM as a component development solution.
|
Q: | Where can I find ActiveX controls to begin working with and learning from?
|
A: | Microsoft maintains a gallery of sample controls, and there is also a C|Net Web site that features ActiveX news and a directory of controls. The Web addresses of these two sites are as follows:
http://www.microsoft.com/activex/gallery/
http://www.activex.com
|