Partager via


Visual Basic Concepts

Creating an ActiveX DLL

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 in-process component, or ActiveX DLL, runs in another application’s process. The client may be the application itself, or another in-process component that the application is using.

Figure 2.1   In-process components are used by applications or other in-process components

The series of step-by-step procedures in this chapter builds an in-process component called ThingDemo, with class modules that demonstrate object lifetime, global objects, and objects that control modal and modeless dialog boxes.

You’ll also see how to debug an ActiveX DLL in process, by running the DLL and a test project together in the Microsoft Visual Basic development environment.

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

Topics

Creating the ThingDemo DLL Project

Shows how to set the project options for an in-process component.

Creating Properties and Methods for the Thing Class

Adds two properties and a method.

Creating the TestThing Test Project

Creates a project group, consisting of ThingDemo and a test project, so you can debug the DLL project in process.

Creating and Testing Thing Objects

Adds code to the TestThing project to create instances of the Thing object and invoke its properties and methods.

Running the TestThing Test Application

Demonstrates the life cycle of object supplied by an in-process component.

Circular References and Object Lifetime

Explores object life cycles further, illustrating the effects of circular references on component shutdown.

Adding a Form to the ThingDemo Project

Adds code to show a form as either a modal or modeless dialog, using a global class to control the form.

Using the Global Object in TestThing

Tests the global object and dialogs, and explains more about debugging DLLs in-process.

Compiling and Testing the ThingDemo DLL

Compiles the project. Shows how to test the .dll file with the test project, and how to include it in another project.

Circular References and Component Shutdown

Shows how Visual Basic unloads an in-process component after the client has released all references to its objects. Also shows how circular references can prevent this unloading.

ActiveX DLL Creation Summary

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

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 checked, then click OK. This will make it easy to save changes to the project as you go along.