Share via


Visual Basic Concepts

Creating the ThingDemo DLL Project

How easy is it to create an ActiveX DLL with Visual Basic? If you can declare variables and write procedures, you can create an in-process component.

This section provides step-by-step instructions on how to define a simple class, and demonstrates the life cycle of objects provided by components. You can use objects created from this class with any application that can use Automation to control objects.

Note   This topic is part of a series that walks you through creating a sample ActiveX DLL. It begins with the topic Creating an ActiveX DLL.

To create the ThingDemo project

  1. On the File menu, click New Project.

  2. In the New Project dialog box, double-click the ActiveX DLL icon. Visual Basic automatically adds a class module, Class1, to the new project.

  3. Press F4 to open the Properties window. Double-click the Name property and change it to Thing. This is the name you’ll use to create objects from the class.

    The default value for the Instancing property is MultiUse. This allows clients to create multiple instances of the Thing class. For a full discussion of the Instancing property, see "Instancing for Classes Provided by ActiveX Components," in "General Principles of Component Design."

  4. On the Project menu, click Project1 Properties to open the Project Properties dialog box. Select the General tab, fill out the information shown below, then click OK.

    The project name, ThingDemo, is also used as the name of the component’s type library. It can be combined with the name of each class the component provides, to produce unique class names.

    If two components each provide a Thing class, the fully qualified class name lets you specify which component’s Thing class you want to use, for example, ThingDemo.Thing.

  5. On the Project menu, click Add Module to open the Add Module dialog box. Double click the Module icon to add a module to the project.

    Note   If you’ve used the Options dialog box (accessed from the Tools menu) to disable the Add Module dialog box, you’ll just get the module. This is okay.

  6. In the Code window for the module, add the following code:

    Option Explicit
    Public gdatServerStarted As Date
    
    Sub Main()
       ' Code to be executed when the component starts,
       '   in response to the first object request.
       gdatServerStarted = Now
       Debug.Print "Executing Sub Main"
    End Sub
    
    ' Function to provide unique identifiers for objects.
    Public Function GetDebugID() As Long
       Static lngDebugID As Long
       lngDebugID = lngDebugID + 1
       GetDebugID = lngDebugID
    End Function
    
  7. On the File menu, click Save Project to save the project files, using the following names. Visual Basic will provide the extensions automatically.

File File name Extension
Module ThingDemo_Module1 .bas
Class module ThingDemo_Thing .cls
Project ThingDemo .vbp

For More Information   See "Choosing a Project Type and Setting Project Properties" in "General Principles of Component Design."

Step by Step

This topic is part of a series that walks you through creating a sample ActiveX DLL.

To See
Go to the next step Creating Properties and Methods for the Thing Class
Start from the beginning Creating an ActiveX DLL.