Register OCX

Does anyone know how to register an OCX in code and not just shelling regserv32.exe with the file.
I would like to know if there is any api to do this.
LVL 13
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you are doing this for an EXE your setup program will automatically register your controls and libraries.
From Microsoft MSDN

HOWTO: Register Your Custom ActiveX DLL from a Client

The information in this article applies to:

Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0


If a compiled client application attempts to reference an object contained in an ActiveX DLL that is not currently or correctly registered on the machine, the following run-time error appears:

ActiveX component can't create object or return reference to this object (Error 429)
This error will not occur when testing in the Visual Basic IDE. In the development environment Visual Basic will one of the following compile errors:
User-defined type not defined
Can't find project or library
This article details a method that can be used to ensure that your client application correctly traps for and resolves error 429 at run-time if the ActiveX DLL is present on the machine but not correctly registered.

Below are steps for creating both an ActiveX DLL server and a client application. The client application is designed to trap for error 429. If the error occurs, the ActiveX server will be registered through code.

In this example, MyServerObject.DLL is the ActiveX server. MyClient.Exe is the client application.

Step-by-Step Instructions for Creating MyServerObject.DLL
In Visual Basic, create a new ActiveX DLL project. Class1 is created by default.

From the Project menu, choose Project1 Properties and change the Project Name property to MyServerObject.

Set the following properties for Class1:
      Property            Value
      (Name)              MyObject
      Instancing          5 - MultiUse

Add the following code to the General Declarations section of MyObject class:
      Public MyProperty As String

Save the Project as MyServerObject.VBP and the MyObject class as MyObject.CLS.

Build the MyServerObject.DLL.

Step-by-Step Instructions for Creating MyClient.Exe
In Visual Basic, start a new standard EXE. Form1 is created by default.

From the Project menu, choose Project1 Properties. Change the Project Name to MyClient.

Select the Project\References menu item to bring up the References dialog box. Go down the Available References list and check MyServerObject.

Add a new module (Module1) to the project.

In Module1, add the following code to the General Declarations section:
      Public Declare Function RegMyServerObject Lib _
      "<Path>\MyServerObject.DLL" _
      Alias "DllRegisterServer" () As Long
where <Path> is the full path to MyServerObject.Dll. The "DllRegisterServer" portion of the above declaration is case-sensitive. NOTE: For more information on this API function see Programmatic Registration at the end of this article.

Add a CommandButton (Command1) to Form1.

Add the following code to the Load event procedure of Form1:
      Private Sub Form_Load()
         On Error GoTo Err_DLL_Not_Registered
         Dim RegMyDLLAttempted As Boolean
         Dim MyObj As New MyServerObject.MyObject

         'The following statement will fail at run-time
         'if MyServerObject is not registered.
         MyObj.MyProperty = "Hello"
         Set MyObj = Nothing
         Exit Sub

         ' Check to see if error 429 occurs
         If Err.Number = 429 Then
            MsgBox "Attempting To Register MyServerObject"

            'RegMyDLLAttempted is used to determine whether an
            'attempt to register the ActiveX DLL has already been
            'attempted. This helps to avoid getting stuck in a loop if
            'the ActiveX DLL cannot be registered for some reason.
            If RegMyDLLAttempted Then
               MsgBox "Unable to Register MyServerObject"
               Resume Next
               RegMyServerObject   'Declared in Module1
               RegMyDLLAttempted = True
               MsgBox "Registration of MyServerObject attempted."
            End If

MsgBox "An Error Occurred"
         End If
      End Sub

Add the following code to the Click event procedure of Command1:
      Private Sub Command1_Click()
         Dim MyObj As New MyObject
         MyObj.MyProperty = "Hello"
         MsgBox MyObj.MyProperty
      End Sub

Save the project and make the MyClient.Exe executable file.

Exit Visual Basic. Test MyClient.Exe by double-clicking on the file in Windows Explorer.

For testing purposes, unregister MyServerObject.Dll using RegSvr32.Exe. From the Start menu, choose Run, and in the Run dialog, type the following command:
RegSvr32.Exe /U <Path>\MyServerObject.Dll
where <Path> is the full path to MyServerObject.Dll.

Run the MyClient.Exe program again. This time you should be notified that registration of MyServerObject.dll is being attempted because it is not already registered.

As demonstrated with the example above, when working with your own client application there are two basic tasks that must be accomplished. First, you need to publicly declare the DllRegisterServer function:
   Public Declare Function RegMyServerObject Lib _
   "<Path>\MyServerObject.DLL" _
   Alias "DllRegisterServer" () As Long
Second, you need to trap for error 429 in the error handling routine of the Form1 Load event and attempt to recover from the error by calling the function declaration for DllRegisterServer.
Programmatic Registration
All ActiveX DLLs created with Visual Basic export the DllRegisterServer and DllUnregisterServer functions. These functions can be declared in a Visual Basic client and called to self-register or unregister an ActiveX DLL. For example, the following declaration could be used to declare a function which would register the custom ActiveX DLL MyServerObject.DLL:
   Public Declare Function RegMyServerObject Lib _
   "MyServerObject.DLL" _
   Alias "DllRegisterServer" () As Long
In code, the "RegMyServerObject" could be called to register the DLL:
   Call RegMyServerObject

Additional query words: register registry unregister unregistered

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

Last Reviewed: August 27, 1999


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Since every ActiveX ocx/dll file has to provide a 'DllRegisterServer' and 'DllUnregisterServer' function, you can (un)register them as shown below (example is for Common Controls OCX, change the 'Lib' part of the declarations to your ocx filename, and give the functions a better name if you like). Keep in mind that this is the same thing as what regsvr32 does, so this takes about the same time as using regsvr32, which can be rather slow.

Private Declare Function RegComCtl32 Lib "ComCtl32.OCX" _
Alias "DllRegisterServer" () As Long
Private Declare Function UnRegComCtl32
Lib "ComCtl32.OCX" _
Alias "DllUnregisterServer" () As Long

Private Sub Form_Load()
    If RegComCtl32 = ERROR_SUCCESS Then
        MsgBox "Registration Successful"
        MsgBox "Registration Unsuccessful"
    End If

    If UnRegComCtl32 = ERROR_SUCCESS Then
        MsgBox "UnRegistration Successful"
        MsgBox "UnRegistration Unsuccessful"
    End If
End Sub

D*mn, beaten by the clock again :-(
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.