|  | Click Here!

Search the site:
EXPERT SEARCH

| Programming Languages
| Databases
| Security
| Web Services
| Network Services
| Middleware
| Components
| Operating Systems
| User Interfaces
| Groupware & Collaboration
| Content Management
| Productivity Applications
| Hardware
| Fun & Games

EarthWeb sites
Crossnodes
Datamation
Developer.com
DICE
EarthWeb.com
EarthWeb Direct
ERP Hub
Gamelan
GoCertify.com
HTMLGoodies
Intranet Journal
IT Knowledge
IT Library
JavaGoodies
JARS
JavaScripts.com
open source IT
RoadCoders
Y2K Info

- 6 — ActiveX Controls

- What Are ActiveX Controls and Their Advantages?

- How Java Applets Work
- How ActiveX Controls Work Differently

- Creating ActiveX Controls from Scratch
- Using ActiveX Controls Within Web Pages
- What ActiveX Controls Enable
- Moving On

#
6 — ActiveX Controls

by Warren Ernst

The key to giving Web pages computing ability is giving them direct access to precompiled modules with the smarts mentioned in the first chapter of this book, then letting these modules run within the Web browser viewing the enabled Web pages. Of course, there are different strategies you can take to create, embed, and run these modules, with the earliest and most popular being Java applets embedded and enabled within HTML files and JavaScript acting as a sort of glue to bind it all together.

Although the ActiveX strategy of enabling Web pages doesn't take away your ability to use Java applets and JavaScript, it does give you another option, one with benefits beyond those of Java and JavaScript, with the benefit of allowing them to be reused for your other software projects. It's also an option that gives Web pages the ability to use OLE for communicating with the rest of the browser's system. This option is to use ActiveX controls as embedded smart modules for your Web pages (and other programs to boot). This chapter reviews the benefits of using ActiveX controls (formerly called OLE controls) within Web pages, how they are created, how they are used within Web pages, and some imaginative uses for them.

###
What Are ActiveX Controls and Their Advantages?

Simply put, ActiveX controls are modular programs designed to give a specific piece of functionality to a parent application. These parent applications, called ActiveX control "containers," are just programs that supply the environment for an ActiveX control to run. Right now, Microsoft's Internet Explorer 3.0 is such a container, but there are others. Ultimately, popular Microsoft applications like Word and Excel will be ActiveX containers. The control-container relationship is not that much different from the relationship between Java applets and Web browsers.

####
How Java Applets Work

Java applets are "compiled" into "bytecode," a state sort of halfway between a full executable and source code, that will run wherever there is a Java Virtual Machine, which in this case are the Netscape, HotJava, and Internet Explorer Web browsers. Since they aren't compiled into full executables, and are instead interpreted as soon as they are received, this means they're quite slow. However, just-in-time Java compilers are in the works that will compile the bytecode Java programs into native executables that run much faster.

The Java Virtual Machine completely shelters the Java applet from the rest of the computer system, which has both benefits and drawbacks. Since the Virtual Machine assigns memory and interprets bytecode on the fly, Java applets are platform independent and unable to perform actions that might have the potential to damage things, such as directly access portions of memory or write to a disk—it's supposedly impossible to create a virus in a Java applet, and I haven't seen any evidence to the contrary. Unfortunately, this same Virtual Machine limits what Java applets can do: They can't read or write files on the user's local machine, they have no standard way to communicate between themselves, and they can't really interact with the Web page they are based in. Java applets are truly self-contained nuggets of computational ability.

####
How ActiveX Controls Work Differently

ActiveX controls are, in fact, completely compiled executables that rely on another system for basic input and output, namely the ActiveX container. Microsoft's Internet Explorer 3.0 is the containing program you're most familiar with right now—and the Ncompass ActiveX Netscape plug-in module is also popular—but containers aren't limited merely to Web browsers. Current environments with ActiveX control containers include Visual Basic, Visual C++, Visual FoxPro, and Microsoft Access, which means that controls (either custom-created controls or pre-existing ones) used to add smarts to Web pages can also be reused to add smarts to the other programs you create. This is a big advantage of ActiveX controls over Java applets; sure, technically you can create standalone Java applications, but the ability of ActiveX controls to seamlessly integrate into existing products is currently a significant step beyond Java.

Controls are precompiled into native code, so although they won't work across platforms like Java applets, they will run much faster, opening up more possibilities for control-based Web applications than Java could. Of course, this adds the difficulty of contending with different development efforts to support multiple platforms. Controls can communicate much better with both the Web page they're based in and the rest of your system than Java can. By using Visual Basic scripting in a Web page, controls can communicate through the methods, properties, and events you're probably already familiar with from other Windows programs, meaning controls can immediately alter a Web page currently in view or manipulate desktop applications with OLE.

ActiveX controls also use a different security scheme than Java applets, so there is no need to impose a "no local file input/output" limitation on them, which allows even more sophistication. Java applets are "secure" because there's essentially no way they can do anything meaningful to your system, since they run within a sheltered Java Virtual Machine. ActiveX controls' security comes from "code-signing", which allows you (or your programs) to determine the source of the control and whether the code has been modified since it was signed by its creator. In other words, once the validity of a control is confirmed, it is given more system access, permitting more functionality than a Java applet could ever have. You can think of code signing as the logical equivalent of signing your name on a legal document; your signature guarantees that the agreement is indeed based on your commitment. Likewise, your code signature guarantees that you created the ActiveX control. This might not seem important on an individual level, but think about code signing in terms of major commercial ActiveX controls; you need to know that a control has come directly from Microsoft and not from the hands of a virus developer.

Figure 6.1 graphically summarizes the different abilities of ActiveX controls versus Java applets.

Figure 6.1. The abilities of ActiveX controls compared to those of Java applets.

###
Creating ActiveX Controls from Scratch

It's possible to creating ActiveX controls from scratch with different methods and languages, but the foundation for creating them is OLE, which means that Microsoft programming tools are usually the best tools to use. In fact, you can think of ActiveX controls as OLE controls extended with Internet communications abilities. The specifications of these abilities are available from Microsoft in the ActiveX Development Kit, available both as a CD-ROM set available from the Microsoft Developer Network or as a download directly from the Internet. To download the kit, try http://www.microsoft.com/intdev/sdk/ and follow the download links.

Currently, the only programming environment for writing ActiveX controls from scratch is Microsoft's Visual C++, but this should soon change to include the Java language and possibly Visual Basic or Borland's Delphi and Borland C++. If you are already using Microsoft's Visual C++ to create OLE-enabled programs, then you are practically creating ActiveX controls already. Just use Visual C++'s own OLE control development facilities and the Microsoft Foundation Class Library (MFC), and the result will be fairly small control executables that are ready to run, as long as the user has the correct MFC dynamic link library (DLL) installed. At this point, the MFC DLL is almost a megabyte in size, which translates into a single 5-minute download at 28.8 modem speeds, but it has to be transferred and installed only once. This required download is like needing the Visual Basic runtime library DLL before you can run Visual Basic Applications. Microsoft plans on releasing a streamlined—that is, smaller—compatible MFC DLL in the future, so controls created with the current MFC will still work.

From a programming perspective, using MFC to create ActiveX controls has many benefits, particularly inheriting your control from a pre-existing set of MFC classes that handle much of the overhead involved in managing an ActiveX control. I've heard enough horror stories from the early days of OLE programming to know that any standard code you can reuse is well worth the effort involved in learning how to use it. Beyond that, writing the control in C++ instead of C makes the process more logical and ultimately fits the tools to the problem better, since ActiveX controls are indeed objects.

You can also use the ActiveX Template Library (ATL) with Visual C++ to take some of the "down and dirty" OLE coding out of creating controls, but you would need a thorough understanding of Microsoft's Component Object Model (COM) and the ActiveX control architecture. However, if you have this experience or are willing to roll up your sleeves and learn it, you can create very small, fast controls that won't require the MFC DLL. The ActiveX Template Library is available at http://www.microsoft.com/visualc/v42/atl/default.htm, as is its license agreement.

If it seems like there's more work involved in creating ActiveX controls, that's because there is, but the results from this extra work are program modules/objects you can reuse across a wide range of applications, whether it's Web pages, Visual Basic programs, FoxPro/Access database programs, or almost anything else.

###
Using ActiveX Controls Within Web Pages

ActiveX controls, whether custom-created for a particular task or taken "off the shelf," are placed in a Web page the same way—by using the <OBJECT> HTML tag. This tag is organized to specify first the control's identification, then which data the control should use, then settings for the control's appearance and behavior, and finally some of the control's initial parameters. Listing 6.1 demonstrates the use of the <OBJECT> tag.

Listing 6.1. The ActiveX control, embedded with the <OBJECT> tag, can be placed in either the <BODY> or <HEAD> section.

    <HTML>

    <HEAD>

    <TITLE> Page Title </TITLE>

    </HEAD>

    <BODY BACKGROUND="sjy.jpg" BGCOLOR="#ffffff" TEXT="#000000">

    <H1>First Heading</H1>

    <HR>

    <OBJECT CLASSID="clsid:{7142BA01-8BDF-11cf-9E23-0000E8A7440}"

          ID=View_control HEIGHT=190 WIDTH=300 BORDER=4 ALIGN="left" HSPACE="10" VSPACE=0>

          <PARAM NAME="Image" VALUE="http://www.bdiamond.com/surround/demo/images/marin.svh">

    </OBJECT>

    …Page Continues

Chapter 12, "Controlling the Internet Information Server Through ActiveX ISAPI Filters," explains the <OBJECT> tag in detail, but there are a couple points worth noting here. First, the CLASSID is a number custom-generated for the control at the time of the control's creation and is a unique identification number for the control. This number is guaranteed to be unique among all controls; this is imperative because thousands of controls will eventually be co-existing on the Internet. If you were to use this same control in different areas of the page, it would continue to use the same ID number. This is the identification number used by your operating system's program registry, which contains other control settings (such as hard drive location, among other things). A similar identification name is given to the control as its ID, which is the English (as opposed to numeric) name used by scripts for manipulating and controlling the object interactively from the page. Once the object is specified and its location set, parameters used by the control for its operation are specified by the settings in the <PARMA> tag. Generally, when using off-the-shelf controls, sample <OBJECT> tags are listed to make inserting them into HTML files easier.

When an ActiveX-enabled Web browser encounters an ActiveX control in an <OBJECT> tag, it either downloads the control from its specified location in the <OBJECT> tag or refers to its internal list of saved controls in the operating system registry and executes it within its content area. Currently, only controls that are downloaded separately and registered manually, or controls that ship and install automatically with Internet Explorer 3.0, can be used in Web pages, but Microsoft has proposed several ActiveX control-downloading mechanisms to the Internet community. A discussion of the proposals for on-the-fly downloading of new ActiveX controls can be found at http://www.microsoft.com/intdev/signcode/codedwld.htm.

###
What ActiveX Controls Enable

ActiveX controls make Web pages possible that haven't been seen before because they enable a completely new, almost "live" interaction. They can permit interactive data manipulation within Web pages when combined with VBScripting, as shown in the Web page at http://nihal02.cdc.deakin.edu.au/cdcsoft/emadWeb/xlabel.htm, pictured in Figure 6.2.

Figure 6.2. This Web page combines a VBScript and the ActiveX Label control.

With this page, manipulating the form's radio buttons and fields sends parameter data directly to the ActiveX control handling the text in the box through OLE. In this simple test page, only a text string is being manipulated, but other ActiveX controls could manage three-dimensional images, live information from a remote source on the Internet, or a business application.

ActiveX controls can also permit new multimedia file formats that you can interact with directly in Web pages or any other ActiveX container program. For example, Black Diamond Consulting has created the Surround Video SVH format, an image format that can be rotated 360 degrees (as if you were turning around and viewing the surrounding landscape) by using embedded hypertext links and an interactive ActiveX control. By downloading and installing these controls, jumping to the sample page lets you view all of San Francisco Bay (from any direction from a single vantage point) directly from the Explorer Web Browser. (See Figure 6.3.) To try it yourself, jump to Black Diamond's home page at http://www.bdiamond.com/, follow the Surround Video links, , download the Surround Video ActiveX controls for yourself, and follow the installation instructions. Once you do, jump to http://www.bdiamond.com/surround/demo/demo2.htm and drag the mouse around the image of San Francisco Bay. You'll soon see the bay like you never have before!

Figure 6.3. Dragging the mouse within the view of San Francisco lets you get a complete 360-degree image.

However, should you want to add this 360-degree viewing function within your own programs (even those that don't necessarily use the Internet), you could do so easily if you're using Visual Basic or another Microsoft programming language that incorporates the ActiveX control-containing ability.

###
Moving On

This chapter has reviewed the building blocks of ActiveX features: the ActiveX control. In the next chapter, "ActiveX Scripting," you learn how controls and scripts can be combined into living documents that aren't bound to a particular format or file viewer.

| Use of this site is subject certain Terms & Conditions. Copyright (c) 1996-1999 EarthWeb, Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Please read our privacy policy for details.