In addition to using objects provided by applications such as Microsoft Word and Microsoft Excel and objects that are exposed by WSH itself or by Windows, you can use many objects provided by ActiveX controls. In some cases, these controls come with a setup program that allows you to install and uninstall the components.
If you develop an ActiveX control using a development environment such as Visual Basic or Visual Basic 5 Control Creation Edition (CCE), the component is registered automatically on the development machine and the control is stored in an OCX file.
Before you can use an ActiveX control (OCX file) in a script, you must register the control. If the developer of the OCX file didn't provide an installation program that registers the object, you must use the program RegSvr32.exe, which ships with several Microsoft applications and operating systems (including Windows 98 and Windows 2000). To register an OCX file as an ActiveX control, you invoke the RegSvr32.exe program by using a command such as the following:
The program locates the file, analyzes its content, and adds all requested information to the Registry. You can then use the ActiveX control in a WSH script.
In Windows 98 and Windows 2000, you'll find RegSvr32.exe in the System or System32 folder. When you invoke this program without specifying an object's file, a dialog box with all the program's options appears, as shown in Figure 2-12.
Figure 2-12 Options for RegSvr32.exe
You can double-click on the sample file \WSHDevGuide\Chapter02\OCXReg.reg to add two commands, Register OCX and Unregister OCX, to the shortcut menu of your OCX files. These commands register and unregister, respectively, the ActiveX control in the OCX file.
After installing ActiveX controls using RegSvr32.exe or after using a development environment such as Visual Basic or Visual Basic 5 CCE to create your own controls, your system (and your Registry) can become cluttered with installed ActiveX components. So how do you remove unneeded ActiveX controls? It's not sufficient just to delete the OCX file. You must uninstall the entire component, which means removing the Registry entries. To remove all the component's Registry entries, you should invoke RegSvr32.exe as follows:
RegSvr32.exe /u C:TestControl.ocx
The program locates the OCX file, and the /u switch forces the program to remove all entries for this ActiveX component from the Registry. You can then safely delete the OCX file without the risk that unused entries will remain in your system.
If you imported the OCXReg.reg file, as described in the preceding Tip, you can right-click on the OCX file and use the shortcut command Unregister OCX to unregister the OCX file.
You can find more detailed information about installing and uninstalling ActiveX controls in my book Advanced Development with Microsoft Windows Script Host 2.0.