【问题标题】:Referencing .NET Assembly in VB6 won't work在 VB6 中引用 .NET 程序集不起作用
【发布时间】:2011-03-01 07:41:20
【问题描述】:

我使用 c# 编写了一个 .net 程序集来执行托管和非托管代码都将使用的功能。我有一个 VB6 项目,现在需要通过 COM 使用程序集。

我创建了我的 .net 程序集,确保 ComVisible 设置为 true 并通过项目属性为 COM 互操作注册。

public class MyClass

    [ComVisible(true)]
    public string GetResponse()
    {
        return "Testing Response"
    }

}

我构建了程序集并将文件复制到一个文件夹中。 TestInterop.dll

然后我运行一个批处理文件来注册组装工具来为 COM 注册对象。

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
regasm "c:\Program Files\TestApp\TestInterop.dll" /tlb:TestInterop.tlb

我打开一个新的 VB6 应用程序并引用 TestInterop.dll

在 VB6 中,我编写了以下代码并编译。

Dim obj as TestInterop.MyClass
Set obj = new TestInterop.MyClass

Dim strTest as string

strTest = obj.GetRespose()

当我运行程序时,它在 obj.GetResponse() 行上出错。

Run-time error' -2147024894 (80070002'):
Automation error
The system cannot find the file specified

此外,智能感知不适用于 obj。我必须输入GetResponse 方法。这正常吗?

有没有人知道可能出了什么问题或我错过了哪些步骤。谢谢!

【问题讨论】:

    标签: c# com vb6


    【解决方案1】:

    您需要将 .NET 程序集放在 GAC 中,或者使用 /codebase 命令行开关运行 RegAsm(它会报错,但这至少可以工作)。不幸的是,没有智能感知是正常的。

    【讨论】:

    • 工作就像一个魅力。非常感谢!我使用 /codebase 命令运行了 regasm 工具。 /codebase 还会为您创建 tlb 文件。感谢大家的回复。
    • 这是正确的方法。有时这个过程会手动运行。所以你可以使用高级安装的试用版。创建一个虚拟项目并添加你的 dll 和 tlb。你可以在属性中看到注册选项卡检查注册。并构建项目。执行 exe 。关闭 vb6 或其他 ide 中的解决方案。然后添加引用它将在我的情况下工作 vb6 工作
    【解决方案2】:

    上次我看到我忘记硬编码 GUID。所以每次我重新编译VB都会找不到我的代码。这是来自 VB.NET 的模板。 (不要使用这些 GUID,自己制作。)

    <ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
    Public Class ComClass1
    
    #Region "COM GUIDs"
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
        Public Const ClassId As String = "eaf83044-f0a7-417b-b333-e45aec398ca5"
        Public Const InterfaceId As String = "84e0fb8f-266d-40e6-9e8c-3d4eb37d3bf0"
        Public Const EventsId As String = "22ea2214-032f-4eb6-b2d4-c5dd213bab87"
    #End Region
    
        ' A creatable COM class must have a Public Sub New() 
        ' with no parameters, otherwise, the class will not be 
        ' registered in the COM registry and cannot be created 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
    
    End Class
    

    【讨论】:

      【解决方案3】:

      我认为,tlb 文件是在框架目录下生成的,而不是这个目录 (c:\Program Files\TestApp)。

      这可能是这里的问题吗?

      【讨论】:

        【解决方案4】:

        我注意到您不需要手动运行 RegAsm,实际上只需将 AssemblyInfo 属性 ComVisible 设置为 true:

        [程序集:ComVisible(true)]

        您也可以通过转到项目属性 -> 应用程序 -> 程序集信息 -> 使程序集 COM 可见,然后设置复选框来执行此操作。

        无需将您正在创建的程序集注册到 GAC 即可在 VB6 中使用它。

        【讨论】:

          【解决方案5】:

          我遇到了 -2147024894 错误或其他错误,无论我尝试了什么,直到我直接从 exe 运行 vb6 消费者代码。 VB6 调试器阻止我在运行时使用 dll。我什至无法实例化该对象。我可以在设计时引用 tlb,并且也有完美的智能支持。一旦我在 Visual Studio 之外启动应用程序,一切都运行良好。希望这对某人有所帮助。

          【讨论】:

            猜你喜欢
            • 2014-07-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-28
            • 1970-01-01
            • 2013-01-14
            • 2012-09-10
            • 1970-01-01
            相关资源
            最近更新 更多