Chapter 25

An Introduction to ActiveX


There's been a lot of confusion lately about what exactly comprises ActiveX. The documentation supplied by Microsoft is vague at best and often seems to contradict itself. A good definition (i.e. a definition that makes sense to us mere mortals) is almost impossible to come by. One almost has to wonder whether there's anyone at all who has a solid understanding of the ActiveX technologies. The truth is that ActiveX changes on almost a daily basis, making it hard at any given moment to nail down exactly what ActiveX is. In this chapter, though, you'll get an introduction to the areas of ActiveX that are most relevant to the topics in this book. By the end of this chapter, you should have a good grasp of what Microsoft is trying to accomplish with ActiveX.

OLE

A few years ago, Microsoft decided that it was time to come up with a better way to share data between applications. In answer to this need, Microsoft came up with something called OLE (Object Linking and Embedding). OLE implements two different ways of sharing and editing data between applications.

Object linking enables you to add a reference to another document from within your application's open document; in addition, many applications are able to link to the same document. For example, Figure 25.1 shows three applications linked to the same document. Because there is only one copy of the linked data, whenever that data changes, the contents of each of the documents that contain the linked data also change. You might, for example, have a chart from a spreadsheet program in two different business letters. If you change the chart, the contents of the chart in the two letters also change.


FIG. 25.1

A linked document can be shared by several applications.

With object embedding, an actual copy of the source data is placed into the document (see Figure 25.2). This means that any application that embeds a particular set of data into its documents each has distinct copies. When you change the original, nothing happens to the copies in other documents. Conversely, if you change the embedded data, nothing happens to the original.


FIG. 25.2

An embedded document is a copy of the source data.

OLE 2

A while after creating OLE, Microsoft decided that OLE should be extended to enable applications to, not only share data, but also share functionality. Although Microsoft kept the name OLE for this new, and much more complex, technology, the letters are no longer an acronym, but rather just the word, OLE. That is, in OLE 2, "OLE" no longer stands for Object Linking and Embedding. This is because OLE 2 is so much more than just a way to share data.

OLE 2 enables you to share all kinds of objects between applications, including, not only data objects, but also programmable objects, which enable you to access the functionality of other applications. Programmable objects are created by an application's programmers and then are exposed to the system when the application registers them in the system registry. After an object is registered, any other OLE-capable application can request access to the object and control it in almost the same manner as the application calls its own functions.

Controlling another application's objects is called OLE automation. The application that supplies the programmable object is called an OLE automation server, whereas the application that accesses the object is called an OLE automation client. Figure 25.3 shows the relationship between an OLE automation server and client. Using OLE automation, you can write programs much faster, because you can call upon existing programmable objects to do tasks for which you'd otherwise have to write your own code. Similarly, you can write your own OLE automation servers, which can supply functionality not only to your own applications, but also to applications written by others.


FIG. 25.3

OLE automation enables you to draw upon another application's functionality.

One of the most useful and common types of OLE objects are OLE controls. Although originally conceived as a way for programmers to create their own types of user-interface controls (such as custom buttons, gauges, and knobs), OLE controls quickly grew to include powerful mini-applications that could be embedded into applications. Microsoft's Visual Basic was the first programming environment to use anything like OLE controls. At that time, they were called Visual Basic controls (or VBX controls, for short). Soon, however, the technology improved, and OLE controls (called OCX controls, for short) were born.

OLE and COM

COM (Component Object Model) is the system upon which OLE is built. COM is a specification for creating binary objects that can communicate with each other. You can think of a binary object as executable code that's loaded into the system and whose functions can be called by other executable code. DLLs, for example, are one type of binary object.

COM specifies a strict set of rules that programmers must follow when creating binary objects. These rules define a communications protocol that must be supported by all binary objects. By following these rules carefully, objects can be accessed and manipulated by any OLE-capable application, written in any language that supports OLE. Binary objects, too, can be written in any language, as long as the language supports the data structures needed to create the object according to the COM specification.

What are these COM rules and requirements? If you're really interested, you can pick up a copy of Kraig Brockschmidt's Inside OLE, published by Microsoft Press. This 1,200-page tome shows how to program OLE from the ground up. You could also pick up copies of Microsoft's OLE programmer's references, which, together (there are two volumes), offer a couple of thousand more pages of OLE information.

Or, you could just be happy with knowing that OLE is built upon something called COM, and let MFC handle the rest.

Programmers new to OLE often get COM and OLE mixed up. Simply, COM is a specification that dictates how binary objects must be created and how they must communicate. The COM specification ensures that all binary objects in the system are compatible. OLE, on the other hand, describes a way in which COM objects can be used to share data and functionality.

OLE and ActiveX

You can imagine the confusion OLE has caused as the technology has improved. First, OLE was meant only to provide a better way to share data between applications, using object linking and object embedding. Then, suddenly, a whole new dimension was added to OLE, as Microsoft improved the technology to include programmable objects. Now, yet again, Microsoft has extended OLE with a whole new set of abilities in an attempt to make the technology current with and applicable for Internet applications. To keep the name OLE at this point would have caused even more confusion, so Microsoft decided to go with a new name. ActiveX was born.

So, what exactly is ActiveX? Basically, ActiveX is an extended version of OLE 2 (OLE 3, if you will). Because ActiveX is built upon OLE 2, much of the terminology is transferable between the two. The following list shows how some common OLE terms translate into ActiveX terms:

More than just the terminology has changed, of course. For example, whereas OLE controls were designed to be used within a closed system, ActiveX controls can provide interactive and dynamic content to Web pages, which means that the concept of OLE controls has now been extended to include the entire Internet.

ActiveX Today

As you've just discovered, ActiveX provides all the abilities of OLE, as well as extends those abilities with new features that better align the technology with Microsoft's vision for the Internet. Most importantly, ActiveX makes possible two new kinds of applications:

A prime example of the first type of application in the previous list is a Web browser that can use ActiveX controls to embed mini-applications into Web pages. Such a Web page could contain ActiveX controls that do anything from play a game of Hangman to calculate a budget.

An example of the second type of application in the previous list might be a spreadsheet that can save its data anywhere on the Internet. When such applications become available, you'll be able to use the Internet as an actual extension of your local system, almost as if the Internet were a gigantic hard drive. Imagine almost instantly transferring a document directly from your word processor to another user's email.

Microsoft isn't the only company struggling to harness the power of the Internet. Sun Microsystems is frantically developing its Java technology to fulfill many of the same goals. Java applets, for example, are similar to ActiveX controls in that they enable Web pages to contain mini-applications and so give Web browsers badly needed computing power. Java applets, however, are implemented very differently from ActiveX controls, and, although Java applets can be a powerful addition to Web pages, ActiveX is much wider in scope, giving many types of applications new Internet access abilities. Still, if you like, you can get the best of both worlds by using both ActiveX controls and Java applets in your Web pages.

ActiveX and MFC

As you must have figured out by now, this book concentrates on getting the most out of Visual C++ 5.0's Microsoft Foundation Classes. Because MFC already encapsulates much of OLE 2, it also supports much of ActiveX. That is, not much has changed when it comes to the basic programming of ActiveX client and server applications and using automation to control programmable objects (now called ActiveX components).

The remaining chapters of this part of the book offer an introduction to the ActiveX basics. You'll learn to use AppWizard and MFC to create ActiveX clients (in this case, container applications), ActiveX servers, and ActiveX controls. You'll also learn about automation. The list below describes these important elements of ActiveX and OLE programming:

In the following chapters, you'll discover how to use MFC to create all of the types of programs shown in the previous list. Along the way, you'll learn, not only how to create your own ActiveX controls, but also how to embed them in your Web pages and so expose them to millions of Internet users.