Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Key Techniques

This chapter's main purpose is to explain how to create and install a basic add-in. While PropertySetter itself is a simple program, it demonstrates useful techniques for accessing the controls selected in the development environment. It also shows how an add-in can read and modify the properties of those controls. The key techniques described in this chapter are listed briefly below. They are described in detail in the following sections.

Understanding OLE Add-ins are OLE servers used by the Visual Basic programming environment.This section explains how add-ins can provide OLE services to client applications.

Creating Add-Ins

Creating an add-in is a complicated process. This section describes the steps needed to create and install an add-in using Visual Basic 4 and Visual Basic 5. This section ends with a summary of the steps required.

Testing Add-Ins

Because add-ins are invoked by the Visual Basic development environment, testing them is different from testing other applications. This section explains how the add-in designer can use two instances of the Visual Basic development environment to test an add-in.

Understanding OLE

Object Linking and Embedding, or OLE, is a standard that defines how applications should use objects to interact with each other. Starting with the release of Visual Basic 5, Microsoft has changed the name OLE to ActiveX. For most purposes, the two names are interchangeable. In order to understand OLE, one must first understand classes and objects.

Version 4.0 of Visual Basic introduced classes to the language. In many ways, a class is similar to a form that has no user interface. Like a form, a class can have variables, subroutines, functions, and property procedures. Variables, subroutines, and functions can be private to the class module, or public so other parts of an application can interact with them. After a class is defined, an application can create instances of the class, much as it would create new instances of a form. If the class MyClass is defined, the program can create new instances using either of the following two methods;


Dim instance_1 As New MyClass ' Declare and initialize instance_1.
Dim instance_2 As MyClass ' Declare instance_2.
Set instance_2 = New MyClass ' Initialize instance_2.

Once a program has created an instance of a class, it can access the public variables, subroutines, functions, and property procedures of the instance much as it would access similar objects for a form. If the object my_obj belongs to a class that has defined a public subroutine FactorAll, the program could invoke the object's FactorAll subroutine, as in my_obj.FactorAll. This is all so similar to the way forms work that, classes would only deserve a brief mention in this chapter except for one new twist: A program can make class objects available to other applications. One application can create and manipulate objects that are defined in another. It takes a little thought to understand the power of this seemingly trivial ability.

Classes are more than just abstract data types like those defined using the Type statement. They can include subroutines and functions that perform complex actions. In fact, the subroutines and functions could perform all of the actions of a complete application. Suppose a class performed all the tasks needed by a full-featured word processor. Then any other application could create objects of that class and incorporate all of the word processor's features. For example, it would be easy to add spell checking, dictionary, and thesaurus functions to an electronic mail application. This allows developers to easily reuse the code in the word processing application, without rewriting the word processing code.

OLE defines how applications use this sort of class interaction. When an application defines a class for use by other programs, that application is called an OLE server. Other programs that use the server to create objects are called OLE clients. An application can use classes in OLE servers to reuse the code implemented by the servers. Some commercial applications expose objects that other applications can use. Some commercial applications can even use server classes defined by a Visual Basic application. The Visual Basic programming environment is one such application. It can use specially constructed server classes as add-ins to make common programming chores easier.

Creating Add-Ins

There are four steps to creating an application that can act as an add-in OLE server. The steps are listed below and described in the following sections;

  • Setting Project Options
  • Creating the Add-In Class
  • Creating Sub Main
  • Creating the Server DLL

Creating and installing an add-in is a complicated process. After the following four sections describe the process, a summary presents a more detailed list of the steps required.

Setting Project Options

To make a project behave as a proper add-in, several of its properties must be set. These properties are slightly different in Visual Basic 4 and Visual Basic 5.

Project Properties in Visual Basic 4

In Visual Basic 4 these properties are set by selecting the Options... command on the Tools menu. The Project tab of the Options dialog is shown in Figure 4.3.

Figure 4.3: The project Options dialog.

Three options must be set on the project Options dialog to create an add-in: Startup Form, Project Name, and StartMode. OLE servers, including add-ins, do not need to display startup forms. Instead, Visual Basic 4 begins execution with a subroutine named Main. To indicate that the project will start by running this subroutine, the Sub Main option should be selected from the Startup Form list.

The Project Name option determines the name by which the server will be known to the system registry. Other applications will use this name to access the server. For example, if a project's name is set to MyProject in the options dialog, and the project defines a server class named MyClass, then another application could refer to the server class as MyProject.MyClass. Since other applications will need to use the project name, it should be relatively short and easy to remember.

Normally an OLE server runs only when it is serving a client application. If an OLE server program is started from within the Visual Basic programming environment, no client will be ready for the server so the program will immediately end. For testing purposes, however, it is convenient to keep a server running even if it has no clients. Then a client application can invoke the server and connect to the running application.

Checking the OLE Server option button in the project Options dialog tells the Visual Basic development environment to keep the program running even if it has no clients. This option affects the application only when it is running in the Visual Basic development environment. This option does not change the program's behavior when it is running as a normal OLE server.

Project Properties in Visual Basic 5

In Visual Basic 5, the Properties... command in the Project menu presents the project Options dialog. The General tab of the Project Properties dialog is shown in Figure 4.4.

Figure 4.4: The Project Properties dialog.

In Visual Basic 5, only two options must be set on the Project Properties dialog: Project Type and Project Name. Setting the project type to ActiveX DLL or ActiveX EXE indicates that the application is an ActiveX or OLE server. This tells Visual Basic to keep the application running in the development environment even if it does not have a client, much as selecting the OLE Server option does in Visual Basic 4. It also makes the Startup Object irrelevant. In Visual Basic 5, this object can be set to either Sub Main or (None). In either case, when Visual Basic 5 runs an OLE server in the development environment, it does not execute Sub Main. This has consequences that are described in the later section, titled Creating Sub Main. The Project Name should be set as it is for Visual Basic 4.


[Home Page] [Tutorial Page] [Main Add-In Page] [Next Lesson] [Prev Lesson]





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.