0

我已经为此奋斗了几个小时,但我找不到我做错了什么。

我创建了一个新的 C# dll 项目,这是它包含的唯一类的内容:

using System;
using System.Runtime.InteropServices;

namespace PolygonSl {

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Config {

        [ComVisible(true)]
        public string GetCompany() {
            return "POL";
        }
    }
}

我基本上删除了它试图让它工作的所有东西,唯一的参考是System.

我检查了Make assembly COM-Visible标志Assembly Information并且我的项目已签名(代码库需要接缝)。

它编译得很好,在那之后,我打电话RegAsm.exe给它,给它我的dll,我添加了/codebase/tlb命令成功了。

当我进入我的 VBA 项目时,我可以将我的新 tlb 文件添加到引用中,工作正常。之后,我可以在我的代码中使用它,自动完成功能正在工作,我可以毫无错误地编译。

然后,当我执行时,我得到了这个:

Run-time error '430':
Class does not support Automation or does not support expected interface

这是我在 VBA 中的代码示例:

Private Sub Button1_Click()
    'With CreateObject("PolygonSl.Config")
    With New PolygonSl.Config
        MessBox .GetCompany, MB_OK, "Test"
    End With
End Sub

我尝试了后期绑定,并且我的代码运行良好,但我希望能够使用自动完成功能。

有人对我可以尝试使其工作的建议有什么建议吗?

编辑(在我的环境中添加一些细节)

  • 我为与 Dynamics SL(Microsoft ERP 之一)相关的项目使用 VS2008
  • 我在 Windows Server 2008 R8 Standard 上,从 VMWare 运行
  • 在 Framework 3.5、Release、x86 上编译,Dynamics SL 客户端为 32 位
  • 我在 Dynamics 和 Excel 上尝试了我的 dll,以确保问题不是 Dynamics ;)
4

1 回答 1

1

我认为您需要定义一个界面才能看到 getcompany。

using System;
using System.Runtime.InteropServices;

namespace PolygonSl
{
    [Guid("6DC1808F-81BA-4DE0-9F7C-42EA11621B7E")]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IConfig
    {
        string GetCompany();
    }

    [Guid("434C844C-9FA2-4EC6-AB75-45D3013D75BE")]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.ClassInterface(ClassInterfaceType.None)]
    public class Config : IConfig
    {
        public string GetCompany()
        {
            return "POL";
        }
    }
}

您可以通过将光标放在类定义中并使用 Edit.Refactor.ExtractInterface 来自动生成接口。

我不得不承认我在这里的能力处于绝对优势,以上内容是根据我在其他地方看到的示例汇总的。

编辑

以下测试代码在我的电脑上运行良好

Option Explicit
Sub polygontest()

    Dim my_polygon As SOPolygon.Config
    Set my_polygon = New SOPolygon.Config
    Debug.Print my_polygon.GetCompany

End Sub

其中 SOPolygon 是项目名称。

于 2019-04-10T15:07:34.223 回答