Saturday, June 11, 2011

Register/Unregister ActiveX Controls Using Code


Register/Unregister ActiveX Controls Using Code

This example will show you how to register and unregister ActiveX
controls from Visual Basic using code, instead of using the Regsvr32.exe

Preparations
Add 2 Command Buttons to your form.
Press the first to unregister the control, and the second to register it.
Module Code
'replace the two "d:\MyDir\MyFile.OCX" below with the name of the
'OCX file you want to register or unregister
Public Declare Function RegControl Lib "d:\MyDir\MyFile.OCX" _
Alias "DllRegisterServer" () As Long

Public Declare Function UnRegControl Lib "d:\MyDir\MyFile.OCX" _
Alias "DllUnregisterServer" () As Long

Public Const S_OK = &H0

Sub RegisterControlSub()
On Error GoTo Err_Registration_Failed
If RegControl = 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 UnRegisterControlSub()
On Error GoTo Err_Unregistration_Failed
If UnRegControl = 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
Form Code
Private Sub Command1_Click()
UnRegisterControlSub
End Sub
Private Sub Command2_Click()
RegisterControlSub
End Sub

3 comments:

  1. Dude. Tell them its NOT you thats Alien X. Its me. This is just a fan blog....

    ReplyDelete
  2. ITS NT A FAN BLOG!!!! ITS THE OFFICIAL ADVERTISER"S BLOG!!!!

    ReplyDelete
  3. Besides they knw im nt alienx cuz all ma posts say "Posted by Ijaz" and nt Alienx... so no offence =)

    ReplyDelete