从.NET程序集创建对象时,VB6中的后期绑定运行时错误 [英] Late binding run-time error in VB6 when creating an object from a .NET assembly

查看:199
本文介绍了从.NET程序集创建对象时,VB6中的后期绑定运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vb6项目,该项目引用了vb.net com库。

i have a vb6 project that has a reference to a vb.net com library.

当我使用诸如以下这样的早期绑定时,该项目运行良好:

the project runs well when i use early binding such as:

Dim b as object
Set b = new myComLib.testObject

当我使用后期绑定时,例如:

when i use late binding such as:

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

我得到以下错误:


运行时错误'429':ActiveX组件无法创建对象

Run-time error '429': ActiveX component can't create object

有什么想法吗?

谢谢

推荐答案

.NET COM Interop类的注册表项在这种情况下是:-

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

HKEY_CLASSES_ROOT\myComLib.testObject 

包含CLSID值和CLSID条目本身

containing a CLSID value and the CLSID entry itself

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

它们也被复制到

HKEY_LOCAL_MACHINE\SOFTWARE\Classes

CreateObject使用HKEY_CLASSES_ROOT条目来检索传入的类名称的详细信息,如果缺少它们,您将收到

CreateObject uses the HKEY_CLASSES_ROOT entries to retrieve the details of the class name passed in so if they're missing you will receive


运行时错误'429':ActiveX组件无法创建对象

Run-time error '429': ActiveX component can't create object

在VB6 IDE中,添加了对dll的引用(对于.NET程序集,通过(是tlb文件)绕过此注册表搜索,从而无需COM注册表项即可进行早期绑定。

Within the VB6 IDE, adding a reference to the dll (in the case of a .NET assembly, via it's tlb file) bypasses this registry search thereby allowing the early binding to work without the COM registry entries.

必须正确注册该类,CreateObject才能正常工作。这应该作为Visual Studio生成过程的一部分进行,否则需要使用Regasm手动注册。

The class has to be correctly registered for CreateObject to work. This should occur as part of the Visual Studio build process, otherwise it needs to be registered manually using Regasm.

您可以通过执行以下操作来测试此行为:-

You can test this behaviour by doing the following:-

1)创建一个新的VB.NET项目myComLib,在项目Compile属性中注册COM Interop,并添加一个类testObject

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

Public Class testObject

    Public Property TestProperty As String

    Public Function TestFunction() As String
        Return "return"
    End Function

End Class

2)构建myComLib

2) Build myComLib

3)创建一个新的VB6项目,向Form1和以下代码添加两个命令按钮

3) Create a new VB6 project, add two command buttons 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)运行VB6项目(如果没有完全编译,将会失败)

4) Run the VB6 project (without full compile as that will fail)

Command2将弹出一个消息框,Command1将失败,并显示

Command2 will popup a message box, Command1 will fail with


编译错误:用户定义

Compile Error: User-defined type not defined.

5)停止项目并通过其tlb文件添加对myComLib的引用

5) Stop the project and add a reference to myComLib via it's tlb file

6)运行VB6项目,两个按钮现在都应该起作用

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

7)进入注册表并删除HKEY_CLASSES_ROOT\myComLib.testObject条目(这可以通过重建.NET组件来重新创建,您需要关闭VB6才能进行重建)

7) Go into the registry and delete the HKEY_CLASSES_ROOT\myComLib.testObject entry (this can be re-created by Rebuilding the .NET component, you'll need to close VB6 to carry out the rebuild)

Command2现在将因

Command2 will now fail with


运行时错误'429':ActiveX组件无法创建对象

Run-time error '429': ActiveX component can't create object

直到重新添加注册表项。

until the registry entry is re-added.

这篇关于从.NET程序集创建对象时,VB6中的后期绑定运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆