A Guide to the Perplexed

Dan Appleman


CONTENTS

Introduction

PART 1  Core Technologies

Chapter 1   ActiveX Myths

Chapter 2   ActiveX: A Historical (but Technical) Perspective

Chapter 3   Objects and Visual Basic

Chapter 4   The Component Object Model: Interfaces, Automation, and Binding

Chapter 5   Aggregation and Polymorphism

Chapter 6   The Life and Times of an ActiveX Component

Chapter 7   ActiveX Components: What's in a Name?

PART 2  Code Components

Chapter 8   The Project

Chapter 9   Creating and Testing Components

Chapter 10   Code and Classes-Beyond the Manuals

Chapter 11   Events

Chapter 12   Collecting Objects

Chapter 13   Object Lifetime

Chapter 14   Multithreading

Chapter 15   Bringing It All Together: The StockQuote Server

PART 3  ActiveX Controls

Chapter 16   ActiveX Control Fundamentals

Chapter 17   The UserControl Object

Chapter 18   The Extender and Ambient Objects

Chapter 19   The Wonderful World of Properties

Chapter 20   Property Pages and Others

Chapter 21   ActiveX Controls and the Internet

Chapter 22   Advanced Techniques

PART 4  ActiveX Documents

Chapter 23   ActiveX Document Fundamentals

Chapter 24   ActiveX Documents and the Internet

PART 5  Selected Topics

Chapter 25   Versioning

Chapter 26   Licensing and Distribution

Credits


HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : [email protected]


PublisherStacy Hiquet
Associate PublisherSteven Sayre
Acquisitions EditorLysa Lewallen
Development/Copy EditorCandace Crane
Technical ReviewerLeslie Taylor
Production EditorsBarbara Dahl and Edith Rex
ProofreaderJeff Barash
Cover Illustration and DesignMagan Gandt
Technical IllustrationMina Reimer
Book DesignGary Suen
Page LayoutBruce Lundquist
IndexerValerie Robbins


Copyright © 1997 by Macmillan Computer Publishing USA. All rights reserved.

All other product names and services identified throughout this book are trademarks or registered trademarks of their respective companies. They are used throughout this book in editorial fashion only and for the benefit of such companies. No such uses, or the use of any trade name, is intended to convey endorsement or other affiliation with the book.

No part of this publication may be reproduced in any form, or stored in a database or retrieval system, or transmitted or distributed in any form by any means, electronic, mechanical photocopying, recording, or otherwise, without the prior written permission of Macmillan Computer Publishing USA, except as permitted by the Copyright Act of 1976, and the End-User License Agreement at the back of this book, and except that program listings may be entered, stored, and executed in a computer system.

EXCEPT FOR THE LIMITED WARRANTY COVERING THE PHYSICAL DISC PACKAGED WITH THIS BOOK AS PROVIDED IN THE END-USER LICENSE AGREEMENT AT THE BACK OF THIS BOOK, THE INFORMATION AND MATERIAL CONTAINED IN THIS BOOK ARE PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY WARRANTY CONCERNING THE ACCURACY, ADEQUACY, OR COMPLETENESS OF SUCH INFORMATION OR MATERIAL OR THE RESULTS TO BE OBTAINED FROM USING SUCH INFORMATION OR MATERIAL. NEITHER MACMILLAN COMPUTER PUBLISHING USA NOR THE AUTHOR SHALL BE RESPONSIBLE FOR ANY CLAIMS ATTRIBUTABLE TO ERRORS, OMISSIONS, OR OTHER INACCURACIES IN THE INFORMATION OR MATERIAL CONTAINED IN THIS BOOK, AND IN NO EVENT SHALL MACMILLAN COMPUTER PUBLISHING USA OR THE AUTHOR BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF SUCH INFORMATION OR MATERIAL.

ISBN 1-56276-510-8


Acknowledgments

If you had told me five years ago that I would have written not one, but four books, I would not have believed it. Even now I'm not sure I believe it, but fortunately for me, a lot of other people do.

The crew at Ziff-Davis Press has changed from when I started, but the current team is as good as ever, maybe better. Candace Crane did a phenomenal job editing and cleaning up my language without changing the tone. Barbara Dahl, Edith Rex, Bruce Lundquist, and Mina Reimer handled things on the production end. Stephen DeLacy and I struggled through yet another CD-ROM production cycle on an impossible schedule. Lysa Lewallen handled a myriad of details as usual. Les Taylor, as outside tech editor, helped keep me honest. Megan Gandt designed this cool cover. Andrea Burnett and Stephanie Rodriguez on the marketing end helped you find out about this book. And Juliet Langley-what can I say? You are always a pleasure to deal with, and this book would not be here without you.

I'd like to extend a special thanks to Dan Soha, who provided the COMics artwork in Chapter 4

In my Win32 API book I wrote: "This book is dedicated first to the people who work at my company, Desaware. Each and every one of them was involved in one way or another, including taking over some of my responsibilities so I could work on this book."

I could think of no better way to express this-and it is just as true for this book. Franky Wong again acted as inside technical editor, catching errors and pointing out areas that needed additional explanation, not to mention handling the day-to-day operation of the company while I was off writing. My father, Gabriel Appleman, also reviewed each chapter for readability and clarity. You can find out more about the rest of the Desaware team on the help file on the CD-ROM: Stjepan Pejic, Roan Bear, Marian Kicklighter, Karyn Duncan, Josh Peck, Levy Ring, Edo Mor, Michael Dickman, and Matt Solnit-thank you all for your help.

I find words are inadequate to express how I feel. To my family, friends, those of you who have read my previous books, and those who own Desaware's products…your support made this book possible. Thank you.


Introduction

A Different Kind of Book

I've been waiting to write a book on ActiveX technology for years.

True, it didn't used to be called ActiveX. Perhaps it was called OLE, or OLE Controls, or VBX, or Visual Basic Custom Controls. It doesn't matter. All along I've been working with the technology as it evolved and waiting for the right time to do a book on the subject. I was waiting for Visual Basic to mature to the point where it could be used to create these types of controls.

When I saw the beta for Visual Basic 5, I knew my wait was over, which left me with a problem. How do I write a book on this technology that will be both incredibly useful to Visual Basic programmers and also stand out from the myriad of ActiveX VB books that undoubtedly will appear on the bookshelves at the same time? How do I write a book detailed enough for the advanced programmer, but with enough scope to welcome even a beginning Visual Basic programmer to ActiveX development?

So I put on my programmer hat and thought about the things that I like and hate about technology books and quickly realized this: I hate manual rehashes.

Simply paraphrasing the Microsoft documentation is pointless. A certain amount of that is inevitable, I suppose, but at least an author should add a significant amount of new material-and, perhaps, a creative new way of looking at the technology that does not echo the manuals. I also hate having to read through things I know to find a few tidbits of new information. You know the kind of book I'm talking about, where a supposedly advanced book starts out by explaining how to draw controls on forms, click a mouse, and turn on the computer.

I knew I wanted to do a comprehensive book on ActiveX and object programming using Visual Basic. I knew I did not want to waste a lot of time rehashing the manual. In fact, I'll let you in on a little secret: The Visual Basic 5.0 documentation is not bad at all. I suppose that is an odd thing for an after-market book author to say. I realized as I was reading the VB5 documentation that it would be perfect…

As I realized these things, I knew what I wanted to do. I did not want to write The ActiveX Bible for Visual Basic 5.0. Microsoft already wrote it; it's in the documentation. I wanted to write the commentary!

A Talmudic Sage named Maimonides wrote the original Guide to the Perplexed in the Middle Ages. It was a guide that did not try to replace the scripture, but rather elaborate on it, interpret it, and help ordinary people understand it. While I'm certainly not a sage of his caliber, I know he had the right idea.

What's in A Guide to the Perplexed?

"So what's in this book?" you may ask. Here are some of the goals I reached for and philosophies I followed while writing it.

On Concepts

There are certain core concepts a programmer must understand to write ActiveX components (not to mention to program in Visual Basic in the first place). I wanted to take a step back and cover those concepts in-depth. I wanted to cover them in such a way that even a beginning VB programmer could understand them. This was my task in Part 1.

On My Target Audience

I focused on information that is new to Visual Basic 5.0 and in some cases to Visual Basic 4.0. If you are completely new to Visual Basic, this is probably not the book to start with because I assume you already know how to use Visual Basic and are familiar with the general syntax of the language.

However, this book is definitely intended for people who have not yet made extensive use of the new object-oriented constructs introduced with Visual Basic 4.0 (such as classes). It is also for those who have not yet worked with OLE or ActiveX technology.

On Approach

I assume that you have access to the Visual Basic documentation. This book is intended to supplement the Microsoft documentation, not replace it. While there is some overlap by necessity, the emphasis is always to go beyond the documentation in the following ways:

On Style

The entire book is written just like this introduction-in the first person. A commentary is, by definition, more than just an authoritative presentation of accurate technical information. It also includes interpretation and opinion. So, while I'll certainly make every effort to make sure that this book is technically accurate and reasonably comprehensive, there will be portions of the book that are based on personal opinion and biases. In other words, not only will I tell you about a feature, how it works and why. I might also tell you what I think of it, and whether you should use it or perhaps try a different approach.

If you find any technical inaccuracies, I will welcome e-mail on the subject ([email protected]) and make corrections promptly for the next edition as well as post the correction on our Web site at www.desaware.com. If you disagree with my interpretation or commentary, I will also welcome e-mail on the subject, will read it carefully and will either post the opposing viewpoint, or delete the message-depending on the nature of your message and my personal whim.

On Scope

You've probably noticed that Visual Basic 5.0 is enormous. It has evolved dramatically from version 1.0 in both size and scope. In order to make it possible to come out with a good ActiveX book in a timely manner, I had to decide how much information to cover. I have no doubt that I will receive many e-mail messages complaining that I left out the one crucial subject needed to make the book perfect. So let me start by telling you what I included and what I left out and why.

What's In and Why

Core concepts relating to ActiveXBecause I believe an expert is someone who understands the fundamentals of a subject very well. With the advent of classes and ActiveX technology in Visual Basic, a good understanding of the underlying concepts of ActiveX are essential to every Visual Basic programmer.
ClassesWorking with classes is now a key part of the foundation for Visual Basic programming. Many VB programmers don't believe this yet. I intend to change their minds.
Code Components (Alias, OLE servers, EXE servers) With the appearance of event sinking and multithreaded objects with Visual Basic 5.0, this type of component has become even more exciting than before. Plus, it is the foundation for understanding ActiveX controls and documents.
ActiveX ControlsI truly believe this is one of the two most important features in Visual Basic 5.0 (the other being native code compilation). I believe that now that it is possible for VB programmers to create ActiveX controls, they all will.
The Internet (or, ActiveX Documents)Is the Internet the most important new technology of the decade? I don't know. I do know that it is not the most important aspect of ActiveX technology. But it is important, and it's covered here.

What's Not In and Why

The VB IDEUsing the VB environment itself is not particularly hard and is well documented in the Microsoft manuals. I'll cover this subject only enough to show you how to set attributes for ActiveX features.
WizardsThe whole idea of wizards is to simplify certain tasks, so discussing how to use a wizard seems rather pointless. But I will take a look at the code that wizards produce (a very interesting subject) and discuss when you should and should not use them.
_Remote AutomationThis may be a subject for a future edition, and I think you'll find the foundations covered here invaluable for using remote automation. But it's a very large subject in itself and deserving of a book of its own.
Language, controls, and almost everything else This book is about ActiveX technology. There are dozens of other VB books on many different subjects available, For example, to study how to use the Win32 API from Visual Basic you can use my book Visual Basic 5.0 Programmer's Guide to the Win32 API, ZD Press, 1997.

All of the content of the book is based on the Enterprise edition of Visual Basic. However, there are very few subjects that do not apply directly to the professional edition as well. Refer to the online Visual Basic help for limitations relating to the standard edition.

Final Comments

When I told my publisher I wanted to do a technical book that was a commentary on the actual documentation and was written entirely in first person, they said yes. Who would have figured? The result is in your hands. I hope you find it useful and, perhaps, even entertaining.

Finally, I wanted to clarify a few matters to help avoid any possible misunderstandings.

On Microsoft

For the record, I like Microsoft. I believe that most of their success is not due to their lawyers (who are legendary) or marketing (which is everywhere) but ultimately because they write good software. Great software. Like Visual Basic 5.0.

I know that Microsoft bashing is a popular sport. Some people are secretly hoping they'll be taken down or split up or something. I don't feel that way at all. That said, you may notice that in any number of places in this book I take various digs at Microsoft or otherwise attempt to be humorous at their expense. So let me be perfectly clear. I have no hidden anti-Microsoft agenda. I mean them no ill-will. It's just that they are such a big juicy target, I can't resist having some fun with them now and then. And that's all there is to it.

A case in point: How many Microsoft programmers does it take to change a light bulb? Answer: None. They just redefined darkness to be the new standard.

Or take this true story. A programmer asked me what it is like to be a software vendor where Microsoft is the biggest fish in the pond. My response: Microsoft isn't the biggest fish in the pond. They are the pond.

On Free Software

Many books you purchase claim to offer megabytes of "free software" of various types. When you look more closely, however, you find that the software is often shareware that needs to be registered, demo-ware, cripple-ware, or software that is otherwise unsupported. And I don't care what the price is. Unsupported software is always too expensive in the long run for any professional use. So this book contains no "free software."

Now don't worry. You'll find lots of software on the CD-ROM, but it's all intended for educational purposes, to help you learn about ActiveX programming. Maybe you'll look at some of it, decide that it is useful for your own applications, and adapt it to your own purposes. If so, great. Think of it as an extra bonus. But don't buy this book because of free software. If you buy it, let the reason be for the information and educational content.

On Desaware

I am the president of a small software company called Desaware Inc. I founded the company in 1991 when I saw Visual Basic 1.0 and decided it represented the future of Windows programming. I think history has proven me right on that account.

You will occasionally find references to Desaware's tools and products in the text of this book. There are two reasons for this. Desaware specializes in ActiveX controls and tools that extend Visual Basic. That make it possible to take full advantage of Windows from the Visual Basic language. Since I use our tools and code in my own ActiveX development efforts, it should be no surprise that I've borrowed some of that technology to solve some of the problems described in this book. I hope that over the course of reading it, you will find it valuable to take a serious look at the Desaware products and demos on the CD-ROM that comes with this book and consider adding some into your own repertoire. If I may boast a little, I must say our products are very cool.

On E-mail

I get a lot of e-mail, and many of the messages contain questions on API related questions. I suspect after this book comes out, I will begin to see questions on ActiveX-related subjects as well.

I wish I could take the time to read and respond to every question that comes my way, but I'm afraid I can't. There isn't that much time in a day. When I receive a question, I will often answer it if I know the answer off the top of my head. If I don't, I'll try to point the questioner in the right direction. But I rarely have the time to do more than that. If you send me your program and ask me to debug it (which happens more often than you might expect), I must apologize in advance. I won't be able to offer much help.

On Updates and Corrections

I wish I could promise you that this book will be perfectly correct in every way. Not only can I not do that, but I can promise you it won't be. There are bound to be errors, omissions, and typographical errors. They seem to occur no matter how many times I read the proofs and regardless of how many people review the chapters.

If you see a problem, please let me know (politely will be nice, but I'll take corrections even if they come with a flame, which also happens more often than you might expect). Also, watch for the latest corrections and updates on our Web site: www.desaware.com.

That's it for the introduction. Now, on to the good stuff.

Daniel Appleman
March 1997