Microsoft KB Archive/173091: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
Line 69: Line 69:
<li>Add a Module (Module1) to the project.</li>
<li>Add a Module (Module1) to the project.</li>
<li><p>Add the following code to the General Declarations section of Module1:</p>
<li><p>Add the following code to the General Declarations section of Module1:</p>
<pre class="codesample">      Public Declare Function RegComCtl32 Lib &quot;ComCtl32.OCX&quot; _
<pre class="codesample">      Public Declare Function RegComCtl32 Lib "ComCtl32.OCX" _
         Alias &quot;DllRegisterServer&quot; () As Long
         Alias "DllRegisterServer" () As Long


       Public Declare Function UnRegComCtl32 Lib &quot;ComCtl32.OCX&quot; _
       Public Declare Function UnRegComCtl32 Lib "ComCtl32.OCX" _
         Alias &quot;DllUnregisterServer&quot; () As Long
         Alias "DllUnregisterServer" () As Long


       Public Const S_OK = &amp;H0
       Public Const S_OK = &amp;H0
Line 80: Line 80:
         On Error GoTo Err_Registration_Failed
         On Error GoTo Err_Registration_Failed
         If RegComCtl32 = S_OK Then
         If RegComCtl32 = S_OK Then
             MsgBox &quot;Registered Successfully&quot;
             MsgBox "Registered Successfully"
         Else
         Else
             MsgBox &quot;Not Registered&quot;
             MsgBox "Not Registered"
         End If
         End If
       Exit Sub
       Exit Sub
       Err_Registration_Failed:
       Err_Registration_Failed:
         MsgBox &quot;Error: &quot; &amp; Err.Number &amp; &quot; &quot; &amp; Err.Description
         MsgBox "Error: " &amp; Err.Number &amp; " " &amp; Err.Description
       End Sub
       End Sub


Line 92: Line 92:
         On Error GoTo Err_Unregistration_Failed
         On Error GoTo Err_Unregistration_Failed
         If UnRegComCtl32 = S_OK Then
         If UnRegComCtl32 = S_OK Then
             MsgBox &quot;Unregistered Successfully&quot;
             MsgBox "Unregistered Successfully"
         Else
         Else
             MsgBox &quot;Not Unregistered&quot;
             MsgBox "Not Unregistered"
         End If
         End If
       Exit Sub
       Exit Sub
       Err_Unregistration_Failed:
       Err_Unregistration_Failed:
         MsgBox &quot;Error: &quot; &amp; Err.Number &amp; &quot; &quot; &amp; Err.Description
         MsgBox "Error: " &amp; Err.Number &amp; " " &amp; Err.Description
       End Sub
       End Sub


Line 122: Line 122:
Please note the following:<br />
Please note the following:<br />


* &quot;RegComCtl32&quot; and &quot;UnRegComCtl32&quot; are user-defined names and may be changed to suit your needs.
* "RegComCtl32" and "UnRegComCtl32" are user-defined names and may be changed to suit your needs.
* The Step-by-Step Example above assumes that ComCtl32.OCX is located in the Windows\System folder of the local hard drive. If it is not located there, you must specify the complete path to the .OCX file in the Declare statement.
* The Step-by-Step Example above assumes that ComCtl32.OCX is located in the Windows\System folder of the local hard drive. If it is not located there, you must specify the complete path to the .OCX file in the Declare statement.
* &quot;DllRegisterServer&quot; and &quot;DllUnregisterServer&quot; are case-sensitive. It is important that the correct case be used when declaring these functions.
* "DllRegisterServer" and "DllUnregisterServer" are case-sensitive. It is important that the correct case be used when declaring these functions.





Revision as of 10:06, 21 July 2020

Knowledge Base


Article ID: 173091

Article Last Modified on 7/1/2004



APPLIES TO

  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition



This article was previously published under Q173091

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:

  • "RegComCtl32" and "UnRegComCtl32" are user-defined names and may be changed to suit your needs.
  • The Step-by-Step Example above assumes that ComCtl32.OCX is located in the Windows\System folder of the local hard drive. If it is not located there, you must specify the complete path to the .OCX file in the Declare statement.
  • "DllRegisterServer" and "DllUnregisterServer" are case-sensitive. It is important that the correct case be used when declaring these functions.


Keywords: kbhowto KB173091