我用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
方法。这正常吗?
有人知道可能出了什么问题或者我错过了哪些步骤吗?谢谢!