ComVisible C#DLL在VB6.0中导致错误438

时间:2014-03-03 13:58:26

标签: c# .net dll vb6 com-interop

我在C#(.NET v4)中编写了一个DLL,我想在VB6.0 Projekt中使用它。我基本上遵循http://msdn.microsoft.com/de-de/library/ms227568(v=vs.80).aspx的教程,最后得到了这个课程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CRUFL_CS_ExchangeRate
{
    [Serializable]
    [ComVisible(true), ClassInterface(ClassInterfaceType.None), Guid("F5DCE88F-AD38-4a9a-9A69-0F8DC0EDB4E3")]
    public class ExchangeUfl : IExchangeUfl
    {
        public double ConvertUSDollarsToCDN(double usd)
        {
            return (usd * 1.45);
        }

        public void bla()
        {
        }

        public string t2()
        {
            return "t2";
        }

        public String test()
        {
            return "test";
        }
    }
}

和这个界面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CRUFL_CS_ExchangeRate
{
    [ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("F6B3D6DB-E4C8-48A9-B9B5-324012E2E93F")]
    interface IExchangeUfl
    {
        [DispId(1)]
        double ConvertUSDollarsToCDN(double usd);

        [DispId(2)]
        String test();

        [DispId(3)]
        string t2();

        [DispId(4)]
        void bla();
    }
}

我的AssemblyInfo.cs看起来像这样

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("CRUFL_CS_ExchangeRate")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("CRUFL_CS_ExchangeRate")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: Guid("912fe53d-dfc9-4eec-bbca-7f2ed29d95dc")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

我已打开项目属性中的强名称签名,并且COM-Interopt注册已关闭。

我创建了项目并通过“regasm.ex C:\ path \ to \ the \ lib.dll / codebase”注册了DLL,它出现在GAC中(我使用的是www.nirsoft.net的RegDllView) )

在VB项目中我可以创建对象,所以我假设注册正常。 但当我调用其中一个方法时,我得到一个运行时错误'438':对象不支持此属性或方法

我的VB代码如下所示:

Dim testi As Object
Dim fab As Object
Set fab = CreateObject("CRUFL_CS_ExchangeRate.ExchangeUfl")
Set testi = fab.t2()

我调用哪种方法无关紧要,我总是得到同样的错误:( 我错过了什么吗?我做错了什么吗?有什么想法吗?

谢谢:)

1 个答案:

答案 0 :(得分:2)

  1. 跳过GAC,并使用/ codebase选项。

  2. 使用RegAsm注册.NET汇编时,也可以使用/tlb选项导出类型库。

  3. 作为复核,请在注册表中查看您刚刚在HKCR注册的对象。

  4. 在VB6项目中,请勿使用CreateObject语法。而是添加对先前导出的类型库的引用。

  5. 提示:避免COM公共签名/类名中的下划线。 VB6在某些边缘情况下使用特殊含义处理下划线。最好把它扼杀在萌芽状态。