How to create ActiveX controls using the VB 5 CCE

Visual Basic 5 Controls Creation Edition (VB 5 CCE) may be downloaded for free from Microsoft's website (Note: Microsoft has removed VB 5 CCE in 2004, but here is a link that still works download.microsoft.com/msdownload/sbn/vbcce/vb5ccein.exe). Microsoft's intension was to encourage ActiveX controls development with this tool. VB 5 CCE is basically a stripped-down version of the full Visual Basic 5.0 application; i.e., it does not include the functionality to create DLL and EXE projects and it only has limited documentation. But, if you know what you are doing, you can use VB 5 CCE to develope more than just the simple visual controls that it was intended for ... it gives you the power to enhance WSH with custom objects, including acting as your gateway to call API functions from your WSH scripts.

Why I used VB 5 CCE ...

A few year ago this tool was only know by Visual Basic programmers and nearly nobody knows how VB 5 CCE could be used to extend system functions. At that time I acted as a technical editor for a Microsoft Press book about Access Basic. I found VB 5 CCE on the companion CD, and I recognized that I could be used to write ActiveX controls. Later on, during writing an Office 97 VBA programmers title for MS Press Germany, I decided to take a quick look to VB 5 CCE. However, I did not get deeply into the details because I really didn't need the tool since I could access API functions with VBA. (Later on I recognized that VB 5 CCE is a nice tools to protect VB code offered to VBA programmers. But that's another story.) In the summer 1998, I begun investigating WSH 1. Because I missed many things in WSH 1 I was looking for a "light" tool to write a few ActiveX controls that would extend WSH with objects/methods that could call API functions from within a script and more. After looking at ATL, C++ and VB 6.0, I came to the conclusion that these tools are a overkill for my purpose. ATL and Visual C ++ provide a huge environment to develope great controls, but I won't waste my system with several hundred megabytes of code just to create a simple ActiveX control (nor I had the time to spend a few additional weeks to learn all the tricks to use these tools). VB 5.0/6.0 also seemed a little bit too "big" for the task. So, I uninstalled the whole Visual Studio 6.0 package from my test machine. Then I remembered me VB 5 CCE, installed the package and used it to create my first simple ActiveX control (at that time I needed an InputBox function for JScript). Within an hour, I was able to create my InputBox method as an ActiveX control (I needed only 3 lines of code within the UserControl ... most of the time was wasted trying out how to name the VB 5 project to set the ProgID for the CreateObject method). After succeeding with that task, I continued with another example. At that time, since I didn't know much about the run-time error handling capabilities of VBScript and JScript, I was interested in creating a method to check whether a Registry key or value exists -- otherwise using the WSH RegRead method resulted in many run-time errors. I already knew the Windows API functions to access the registry, because I wrote some Visual Basic routines a year ago for a Registry book....

... after a few hours, I was able to implement a KeyExists method and it worked well. Even though after writing a book about WSH programming I eventually found some tricky ways to do similar tests from within WSH, I still use the methods I have developed as ActiveX controls. Since then, I used VB 5 CCE to implement many other helpful ActiveX controls that expose objects, methods and properties to WSH scripts. Within writing my WSH title for Microsoft Press Germany in autumn 1998, I was again looking for a tool which would be handy for the VBScript programmer. Because VB 5 CCE is based on Visual Basic, a superset of VBScript, I assumed that it might be easy to use for a VBScript programmer. Also: VB 5 CCE doesn't need more than 20 MB of disk space and the tool is offered for free! So I added several chapters and samples demonstrating how to use VB 5 CCE to extend WSH with ActiveX controls.

... and why I wrote this page

My idea to release my WSH book in English as soon as possible failed. On the other hand, I got feedback from visitors of the WSH Bazaar which expressed their surprise that it would be possible to use VB 5 CCE for ActiveX control development (and for API calls). To allow others to share the knowledge (without hanging nights in front of the screen, like me) I decided to create this page. It explaings the basic techniques to create an ActiveX control using VB 5 CCE. In addition, this page comes with a ready-to-use sample that you could use as a foundation to develope your own controls.

The steps to create an ActiveX control

You need the VB 5 CCE (which may be downloaded from Microsoft's website) and install it in Windows. Then fire up your VB 5 CCE.

  1. After launching the program, a "New project" dialog asks you what to do. Select the ActiveX Control icon on the New property page and close the dialog window using the "OK" button. A new project will be created. This project contains one UserControl.
  2. Click in the project explorer window on the project name ("Project1(project1)"). Use the (Name) entry of the property page to re-name your project (from "project1" to "WSHTest" for instance).
  3. Click on the Entry "UserControl(UserControl)". Use the (Name) entry in the property page to re-name your control from "usercontrol1" to "Test1" for instance.
  4. Save the project into a file (like WSHTest.vbp) using the Save button or the File menu.
  5. Double click on the UserControl icon to open the control window. Use the Create "View Code" button in the project explorer window to show the code page.
  6. Add the code for procedures and functions which will be used within the project. These entries are exposed later as the methods of your ActiveX control.
  7. Select the command "Make .....ocx" in the "Files" menu. If you used the file Test.vbp to save your project the name Test.ocx is used. I have used the name WSHTest.ocx in the ZIP-Archive.

In some environments, the last step opens some dialogs allowing you to set a few options for your ActiveX control. You may close these dialogs to set the default values. Then, an OCX-file with the name of your project file (or the selected name) gets created. The OCX control gets registered automatically on your machine. Within your WSH script you do not need to worry about this file name, because the Windows Registry keeps all the information where to find the control.

Note: The window of the project explorer may be found in the upper right corner of the picture shown above. Currently my WSHTest project contains only one UserControl module. The window in the lower right is the property window showing the properties of the selected module. Currently I have selected the UserControl Test1. The names shown in brackets within the project explorer window are derived from the file names. If you save the project the first time, a project file name is associated. I have used here Test.vbp, so all other files uses the same name but different extensions (like Test.ctl for the source code file of the control). I have named the OCX file as WSHTest.ocx (which is different than the project file name) to demonstrate that file names are not used within your WSH scripts.

Important: Thanks to Steven Bondi, who has tested my sample, I would like to add an additional note. I can't publish here hundred pages of my WSH book used to explain the basics of ActiveX programming. But, my first release of this page was a bit to short. You may use my sample ZIP file to study the code. But take care that the path settings depend on my test machine (I run several Windows 98 builds on my test machine - therefore the project file contains path names that may not apply to all user machines). If you intend to create your own ActiveX control it is important to use a new project! This assures that all paths and libraries are set properly within the project. Also VB 5 CCE creates a unique GUID for your control. This GUID is requested to register your control as a COM object. If you would like to sample code from another project, use the export and import function of VB 5 CCE (the program can run only one project at a time - this is another limitation of VB).

Use the Control in your script

After completing these steps, your ActiveX control is ready to be used within a WSH script. Do you remember when I recommended (in the steps given above) to re-name the project name and the UserControl in the VB 5 CCE? Both names are required in your CreateObject ("TypLib.class") statement. The following VBScript line creates a reference to your new ActiveX control:

Set objAdr = WScript.CreateObject("WSHTest.Test1")

The name WSHTest is the name of the type library and it corresponds to the project name. The subname Test1 is the class name of the object and it is the same as the UserControl name. After you create a reference to the object using the CreateObject method you may access the methods exposed from this object. The following statement uses the WSHDate method:

myDate = objAdr.WSHDate()

We use the object variable and the name of the method within an assignment statement. The result is assigned to the myDate variable. Pretty simple, isn't it?

The source code for the sample ActiveX control

For your convenience I have provided a few simple methods within the UserControl Test1. The source code is shown below:

'************************************************
'* Project:  Test1 (file Test.vbp)
'* Author:   (c) Günter Born
'*
'* This control demonstrates how to use the VB 5 CCE to
'* create ActiveX-controls for the Windows Scripting Host.
'* Compile it using the "Make Test.ocx" command of the
'* "File" menu within the VB 5 CCE. The OCX component is
'* getting registered automatically.
'*
'* Use it at your own risk. In no event will the author be
'* responsible/liable for any consequences of the use of this software.
'* The control may be used for free. Distribution is allowed,
'* if the whole package is distributed with the copyright note
'* and a note pointing to my web-site. This demo was derived
'* from a real sample published in my book "Inside Windows
'* Scripting Host", Microsoft Press Germany. Further versions
'* of this control will be published on my WSH Bazaar at:
'* http://www.borncity.de
'*************************************************

Public Function WSHGetTime() As String
' Just to test, return current time
 WSHGetTime = Time
End Function

Public Function WSHGetDate() As String
' Just to test, return current date
 WSHGetDate = Date
End Function

Public Function WSHInputBox(Prompt As String, title As String, Default As Variant) As Variant
' For JScript: provide a InputBox-Function
 WSHInputBox = InputBox(Prompt, title, Default)
End Function

Public Sub WSHAbout()
 txt = "ActiveX-Control to extend the Windows Scripting Host" + vbCrLf + _
        "with a new Test object and and new methods. Get more from my" + vbCrLf + _
        "Windows Scripting Host Bazaar at:" + vbCrLf + vbCrLf + _
        "http://www.borncitye.de"
 MsgBox txt, vbOKOnly + vbInformation, "(c) Günter Borns WSH-Test - Version 1.1"
End Sub

'*** End

The ActiveX control exposes the methods WSHGetDate, WSHGetTime, WSHInputBox and WSHAbout. You may call these methods from either VBScript or JScript. WSHGetDate and WSHGetTime returns the current date and the current time. The WSHAbout method invokes a simple dialog box with some information about the control. The WSHInputBox method invokes an Inputbox dialog. This will be rather helpful in JScript, because this language doesn't support the InputBox function from VBScript. Having the right ActiveX control enables you to use WSHInputBox in JScript (without the trickier way I have shown in my sample pages).

The source code for a VBScript test program

To show you more about the way how to use the methods provided by the WSHTest.ocx, I have created a short VBScript program. (By the way, the interface of these methods are identically to the WSHExtend ActiveX control offered for download from this site). Further details about using the ActiveX control may be obtained from the following listing.

'************************************************
' File:    TestOCX.vbs (WSH sample in VBScript) 
' Author:  (c) Günter Born
'
' Demonstrates how to access the Test.ocx component
' using a WSH script.
' Use at your own risk. No support, no liability.
'
' New samples are available at the the WSH Bazaar:
' http://www.borncity.de
'************************************************
Option Explicit

Dim objAdr 

MsgBox "Test whether the ActiveX control Test.ocx is registered." + _
        vbCRLF + "If a run-time error occurs, the OCX isn't registered." + _
        vbCRLF + "Then use the program RegisterOcx.bat."

' Create the Test object, requires Test.ocx, otherwise the
' following line creates a run-time error (can't create object).
Set objAdr = CreateObject("WSHTest.Test1")

' our first attempt to use some methods
WScript.Echo "Time:", objAdr.WSHGetTime()
WScript.Echo "Date:", objAdr.WSHGetDate()

WScript.Echo "Your Input was: ", objAdr.WSHInputBox ( _
        "Enter something", "WSHTest by G. Born", "Hello")

objAdr.WSHAbout
' *** End

After invoking the script (double click on the VBS file), you will be guided through a few dialogs showing the date and time, asking for input and showing the "About" dialog. As you can see, accessing the methods of the ActiveX control is very simple.

And here comes another simple sample which may be used in the real world: it shows how convenient the ActiveX control WSHTest.ocx may be for the JScript programmers. The following few lines of code implements an InputBox in JScript.

//************************************************
// File:    Input.js  (WSH sample in JScript) 
// Author:   (c) G. Born 
//
// This script demonstrates how to get a user input in
// JScript. I use the ActiveX control Test.ocx.
// Use AS IS without any warranty. The source:
// the WSH Bazaar at
// http://www.borncity.de
//************************************************
// 

var title = "InputBox function for JScript";
var prompt = "Please enter your name:";

// Create the new object
var WSHTest = WScript.CreateObject("WSHTest.Test1");

// Get the user input and display it with the Echo method

WScript.Echo (WSHTest.WSHInputBox (prompt, title, "Hello World"));

//*** End

After invoking the script (double click on the JS file), you will see an input box asking for your name. If you click on OK, the user input will be shown in a 2nd dialog box. Clicking Cancel returns an empty string which will also be shown in a message box.

The whole project may be downloaded as a ZIP file: ActiveXDemo.zip (8 KB). Unzip the content of your file into a separate folder. The folder will then contain the source code modules for VB 5 CCE, the ready to use OCX, the VBS/JS test files and two BAT files. If you have already installed VB 5 CCE you can register the OCX and use the VBS file to test the control. To register/unregister the control use the two BAT files RegisterOcx.bat and UnregisterOcx.bat. If you load the VBP file into VB 5 CCE, you can inspect the source code and you may compile the project into a new OCX file.

Note: German readers can take the whole beef with several hundred pages of explanations from my WSH book Inside Windows Scripting Host, published by Microsoft Press. The English manuscript dealing with this stuff will be published as Advanced Development with Microsoft Windows Script Host 2.0 by Microsoft Press (in the second half of 2000)..

The project: I walked the "stoney way" to learn about WSH and ActiveX programming. Therefore I intend to open a section that contains helpful ActiveX controls extending WSH - and let scripters exchange their solutions. Have you developed a component (preferably using VB 5 CCE) and want to let others benefit from it? Would you like to see the control here in the WSH Bazaar? Please feel free to upload it. Because I don't have a high speed internet connection, and because I don't wan't to guess what a "black box" module does, here are a few suggestions: All VB 5 CCE project files must be packed into a ZIP archive. Please send only the source file, not the run-time libraries and the final result, so the ZIP archive should be less than 100 KB. Your module should come with at least a short HTML file explaining the features and how to use it from WSH.

Back

(c) Günter Born