Microsoft KB Archive/173407

= How To Register Your Custom ActiveX DLL from a Client =

Article ID: 173407

Article Last Modified on 7/15/2004

-

APPLIES TO


 * 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 Q173407



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.</li></ol>

Step-by-Step Instructions for Creating MyClient.Exe
<ol> In Visual Basic, start a new standard EXE. Form1 is created by default.</li> From the Project menu, choose Project1 Properties. Change the Project Name to MyClient.</li> Select the Project\References menu item to bring up the References dialog box. Go down the Available References list and check MyServerObject.</li> Add a new module (Module1) to the project.</li>  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. </li> Add a CommandButton (Command1) to Form1.</li>  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 </li>  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 </li> Save the project and make the MyClient.Exe executable file.</li> Exit Visual Basic. Test MyClient.Exe by double-clicking on the file in Windows Explorer.</li> 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.</li> 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.</li></ol>

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 kbhowto KB173407

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.