在VB6中引用.NET程序集不起作用

dre*_*f17 9 c# vb6 com

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

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

public class MyClass

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

}
Run Code Online (Sandbox Code Playgroud)

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

然后我运行批处理文件来注册组装工具以注册COM的对象.

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
regasm "c:\Program Files\TestApp\TestInterop.dll" /tlb:TestInterop.tlb
Run Code Online (Sandbox Code Playgroud)

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

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

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

Dim strTest as string

strTest = obj.GetRespose()
Run Code Online (Sandbox Code Playgroud)

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

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

此外,intellesense不适用于obj.我不得不输入GetResponse方法.这是正常的吗?

有没有人知道什么可能是错的或我错过了什么步骤.谢谢!

Mik*_*ton 11

您需要将.NET程序集放在GAC中,或者使用/ codebase命令行开关运行RegAsm(它会抱怨,但这至少会起作用).不幸的是,没有intellisense是正常的.