Last Runtime Error in VB6 When Creating an Object from a .NET Assembly

I have a vb6 project that has a link to the vb.net com library.

The project works well when I use early binding, for example:

Dim b as object Set b = new myComLib.testObject 

when I use the last binding, for example:

 Dim b as object Set b = CreateObject("myComLib.testObject") 

I get the following error:

Runtime Error '429': ActiveX component cannot create object

Any ideas?

thanks

+6
source share
2 answers

The registry entries for the .NET COM Interop class in this case are: -

 HKEY_CLASSES_ROOT\myComLib.testObject 

containing the CLSID value and the CLSID element itself

 HKEY_CLASSES_ROOT\CLSID\<<myComLib.testObject\CLSID value>> 

They are also replicated to

 HKEY_LOCAL_MACHINE\SOFTWARE\Classes 

CreateObject uses the HKEY_CLASSES_ROOT records to get information about the passed class name, so if they are missing, you will get

Runtime Error '429': ActiveX component cannot create object

Inside the VB6 IDE, adding a reference to a DLL (in the case of building .NET through a tlb file) bypasses this registry search, thereby allowing early binding to work without entries in the COM registry.

The class must be correctly registered for CreateObject to work. This should happen as part of the Visual Studio build process, otherwise you must manually register it with Regasm.

You can verify this behavior by doing the following: -

1) Create a new myComLib project for VB.NET for COM interoperability in the project. Compile properties and add testObject class

 Public Class testObject Public Property TestProperty As String Public Function TestFunction() As String Return "return" End Function End Class 

2) Create myComLib

3) Create a new VB6 project, add two commands to Form1 and the following code

 Private Sub Command1_Click() Dim b As Object Set b = New myComLib.testObject b.TestProperty = "Hello" MsgBox b.TestProperty, vbOKOnly, b.TestFunction() End Sub Private Sub Command2_Click() Dim b As Object Set b = CreateObject("myComLib.testObject") b.TestProperty = "Hello" MsgBox b.TestProperty, vbOKOnly, b.TestFunction() End Sub 

4) Run the VB6 project (without full compilation, as this will not work)

Command2 will display a message box, command 1 will end with

Compilation error: user type not defined.

5) Stop the project and add a link to myComLib via the tlb file

6) Run the VB6 project, and both buttons should now work

7) Go to the registry and delete the entry HKEY_CLASSES_ROOT \ myComLib.testObject (this can be recreated using the rebuild of the .NET component, you will need to close VB6 to perform the rebuild)

Command2 now fails with

Runtime Error '429': ActiveX component cannot create object

until a registry entry is added.

+5
source

If you use the ClassInterfaceType.None parameter, you need to add the ProgId attribute to your class to allow late binding.

For instance:

 [Guid("B1E17DF6-9578-4D24-B578-9C70979E2F05")] public interface _Class1 { [DispId(1)] string TestingAMethod(); } [Guid("197A7A57-E59F-41C9-82C8-A2F051ABA53C")] [ProgId("Rubberduck.SourceControl.Class1")] [ClassInterface(ClassInterfaceType.None)] public class Class1 : _Class1 { public string TestingAMethod() { return "Hello World"; } } 
0
source

Source: https://habr.com/ru/post/907546/


All Articles