HOWTO: Programmatically Register and UnRegister .OCX Files

ID: Q173091


The information in this article applies to:


SUMMARY

This article describes how to programmatically register and unregister ActiveX controls (.OCXs).

Microsoft Visual Basic ships with several ActiveX controls (.OCX files). These files are automatically registered by the Visual Basic setup program during installation. Developers often desire to register or unregister these and/or their own controls at will. This can be accomplished by using a tool such as RegSvr32.EXE or it can be done programmatically through Visual Basic code.

The ActiveX control framework supports the DllRegisterServer and DllUnregisterServer entry points. These entry points can be declared as functions in your Visual Basic code and then called to register and unregister the ActiveX control.


MORE INFORMATION

The following example demonstrates how to write a code module that can be used to register and unregister an ActiveX control, specifically the Microsoft Windows Common Controls found in ComCtl32.OCX.

Step-by-Step Example

  1. Create a new Standard Exe project in Visual Basic.


  2. Add a Module (Module1) to the project.


  3. Add the following code to the General Declarations section of Module1:
    
          Public Declare Function RegComCtl32 Lib "ComCtl32.OCX" _
             Alias "DllRegisterServer" () As Long
    
          Public Declare Function UnRegComCtl32 Lib "ComCtl32.OCX" _
             Alias "DllUnregisterServer" () As Long
    
          Public Const S_OK = &H0
    
          Sub RegisterComCtl32()
             On Error GoTo Err_Registration_Failed
             If RegComCtl32 = S_OK Then
                MsgBox "Registered Successfully"
             Else
                MsgBox "Not Registered"
             End If
          Exit Sub
          Err_Registration_Failed:
             MsgBox "Error: " & Err.Number & " " & Err.Description
          End Sub
    
          Sub UnRegisterComCtl32()
             On Error GoTo Err_Unregistration_Failed
             If UnRegComCtl32 = S_OK Then
                MsgBox "Unregistered Successfully"
             Else
                MsgBox "Not Unregistered"
             End If
          Exit Sub
          Err_Unregistration_Failed:
             MsgBox "Error: " & Err.Number & " " & Err.Description
          End Sub
     


  4. View the Immediate Window (CTRL+G) and type:
    
          UnRegisterComCtl32
     
    Press the ENTER key to unregister ComCtl32.OCX, and then type:
    
          RegisterComCtl32
     
    and press the ENTER key to register the control.


These functions can now be called to register and unregister the ActiveX control, programmatically providing functionality similar to that of RegSvr32.EXE. You can test these functions by right-clicking on the ToolBox and selecting Components from the pop-up menu. An entry for the Microsoft Windows Common Controls 5.0 will be displayed on the Controls tab of the Components dialog if ComCtl32.OCX is registered and no entry will be displayed if ComCtl32.OCX is not registered.

Please note the following:


Keywords          : kbnokeyword kbVBp500 kbVBp600 
Version           : 
Platform          : 
Issue type        : kbhowto 

Last Reviewed: May 11, 1999