• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

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.
Thanks.
0
crazyman
Asked:
crazyman
  • 2
1 Solution
 
tkuppinenCommented:
If you are doing this for an EXE your setup program will automatically register your controls and libraries.
0
 
caraf_gCommented:
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

--------------------------------------------------------------------------------


SUMMARY
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.



MORE INFORMATION
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

         Err_DLL_Not_Registered:
         ' 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
            Else
               RegMyServerObject   'Declared in Module1
               RegMyDLLAttempted = True
               MsgBox "Registration of MyServerObject attempted."
               Resume
            End If
         Else

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
 

0
 
Jeremy_DCommented:
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
Const ERROR_SUCCESS = &H0

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

    'Unregister
    If UnRegComCtl32 = ERROR_SUCCESS Then
        MsgBox "UnRegistration Successful"
    Else
        MsgBox "UnRegistration Unsuccessful"
    End If
End Sub
'-----------------------------------

Cheers,
Jeremy
0
 
Jeremy_DCommented:
D*mn, beaten by the clock again :-(
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now