Visual Basic Concepts

Creating an ActiveX EXE Component

Components provide reusable code in the form of objects. An application that uses a component’s code, by creating objects and calling their properties and methods, is referred to as a client.

Components can run either in-process or out-of-process with respect to the clients that use their objects. An out-of-process component, or ActiveX EXE, runs in its own address space. The client is usually an application running in another process.

Figure 3.1   Client and out-of-process component

The fact that an out-of-process component runs in its own process means that a client can tell it to do something, and then go about its business while the component does the work.  When such a system is properly set up, the component can tell the client when the task is done using an asynchronous notification, as explained in "Asynchronous Call-Backs and Events" in "Building Code Components."

Note   In-Process and Out-of-Process Code Components," in "General Principles of Component Design," discusses differences between in-process and out-of-process code components.

The step-by-step procedures in this chapter explore the creation and use of out-of-process components by building a simple component called CoffeeWatch. Coffeewatch demonstrates the behavior of modal and modeless dialog boxes, and shows one way to implement asynchronous notifications using events.

You’ll also see how to debug an ActiveX EXE out of process, by running the component and a test project in two separate instances of the Visual Basic development environment.

The procedures for creating the Coffee component build on each other. The sequence in which you perform the procedures is therefore important:

Topics

Creating the Coffee Project

Opens a new ActiveX EXE project, sets the necessary project information, and adds modules required for demonstrating modal and modeless forms.

Showing Forms from the CoffeeMonitor Class

Adds code to show a form either modally or modelessly.

Creating the CoffeeWatch Test Project

Shows how to create a test project in a second instance of the development environment. Adds code to the test project to call the methods of CoffeeMonitor.

How Modal and Modeless Forms Behave Out of Process

Shows that modal and modeless forms display differently when they’re not in the same process as the client’s main form.

Providing an Asynchronous Notification Event

Shows how to set up an object to provide asynchronous notifications to one or more clients.

Receiving an Asynchronous Notification Event

Adds code to the client to request asynchronous notifications, and to take some action when the notification events occur.

Sharing the CoffeeMonitor

Creates a Connector object that clients can use to obtain a reference to a single shared instance of CoffeeMonitor.

Using the Shared CoffeeMonitor

Adds code to the client to request a Connector object, and then call its CoffeeMonitor property to obtain a reference to a shared CoffeeMonitor object.

Creating and Testing the Coffee Executable

Compiles both the Coffee component and the CoffeeWatch test program. Runs both the CoffeeWatch project and several instances of CoffeeWatch.exe using one instance of Coffee.exe.

ActiveX EXE Component Creation Summary

Outlines the typical process of designing and creating an out-of-process component.

Before You Begin

These procedures will be easier to follow if you set up your development environment as described below.

Before You Begin

  1. On the View menu, click Toolbox to open the Toolbox.

  2. On the View menu, click Project Explorer to open the Project Explorer window. The Project Explorer window will be used extensively to switch between project files.

  3. If the Project Explorer window is in Folder view, as shown below, click the Toggle Folders button on the Project Explorer window toolbar to turn the folders off.

  4. On the View menu, click Properties Window to open the Properties window.

  5. On the Tools menu, click Options to open the Options dialog box.

    Select the Editor tab, and make sure Require Variable Declaration is checked. This makes it much easier to catch typing errors.

    Select the Environment tab. Make sure Prompt To Save Changes is selected, then click OK. This will make it easy to save changes to the project as you go along.

Sample applications

Coffee2.vbp, CoffWat2.vbp, MTCoffee.vbp

Coffee2.vbp and CoffWat2.vbp contain expanded source code for this chapter. In addition to asynchronous notifications using events, they include an alternate version using call-back methods on a secondary interface, and an example of all-code timers you can use instead of the Timer control. MTCoffee illustrates some features of multithreading. The sample applications are listed in the directory.