Navision C/AL 使用 C# COM dll 创建错误

Navision C/AL Create error with C# COM dll

我正在为 Navision 2009 SP1(经典客户端)开发报告,其中出现了对 NAV 中不可用的额外功能的需求。

我在 C# 中创建了一个 COM dll,安装了它(使用 InstallShield "free" vs version)并在 NAV 中创建了一个自动化变量。 class 和界面可见,我看到了 Print 方法,但是当我尝试创建变量时,出现以下错误:

"Could not create an instance of the OLE control or Automation server identified by..... Check that the OLE control or Automation server is correctly installed and registered."

这是代码(只是想建立连接):

[ComVisible(true), Guid("080a97fb-321c-4a2f-b948-dd52ce263415"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IPrinterTest
{
    [DispId(1)]
    bool Print(string test, string bytesInStringRepresentation);
}

[ClassInterface(ClassInterfaceType.None), ComVisible(true), Guid("8d7b85a9-1a20-4ea0-a7d4-decf26632eee"), ProgId("Printer.PrinterTest")]
public class PrinterTest : IPrinterTest
{
    public PrinterTest()
    {

    }

    public bool Print(string test, string bytesInStringRepresentation)
    {
        return true;
    }
}

生产环境只有 .NET Framework Client Profile(3.5 和 4.0),这意味着 Regasm 可用(在 4.0 中)而 gacutil 不可用。

我假设我的代码有问题而不是 InstallShield,因为 dll 可以在 Navision 中选择,并且当我尝试创建导航自动化变量时出现问题。

此致 马库斯

好的,所以上面的代码没有任何问题,显然 Visual Studio 也有一个安装程序工具集。

我创建了一个新的解决方案,重新创建了库项目并添加了 VS 安装程序项目而不是 InstallShield 项目。我确保库程序集是强签名的,并将我生成的 TLB 文件添加到安装程序项目中的 "FileSystem"。安装程序项目会自动将 TLB 文件注册为依赖项,因此之后可以从 "FileSystem" 中删除 TLB 文件。

通过该设置,我能够为 COM 互操作正确注册 DLL。

所以要记住的要点是:

  • 如果您的设置与我相同(界面和class),请确保它们都是ComVisible 和public。此外,他们需要具有匹配的名称,例如"IPrinterTest" & "PrinterTest".
  • 确保程序集是强签名的。
  • 您必须为 DLL 生成一个 TLB 文件并将其添加到安装程序 "FileSystem"。

稍后我会用博客post编辑这个答案,详细说明整个过程。