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

查看:24
本文介绍了从 .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_ROOTmyComLib.testObject 

包含 CLSID 值和 CLSID 条目本身

containing a CLSID value and the CLSID entry itself

HKEY_CLASSES_ROOTCLSID<<myComLib.testObjectCLSID value>>

它们也被复制到

HKEY_LOCAL_MACHINESOFTWAREClasses

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在项目中注册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_ROOTmyComLib.testObject 条目(这可以通过重建 .NET 组件重新创建,您需要关闭 VB6 才能执行重建)

7) Go into the registry and delete the HKEY_CLASSES_ROOTmyComLib.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天全站免登陆