【问题标题】:Resolve .net tlb reference解析 .net tlb 引用
【发布时间】:2017-07-26 13:47:22
【问题描述】:


现在我正在编写一个.net dll,它应该可以在 VBA 代码(Excel、Access 等)中使用。以下设置工作正常:

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
[Guid("8079e4a4-1e4b-4788-92ba-9d5b017fa9be")]  //Allocate your own GUID
public interface ICommunication
{
    string TestProp { get; set; }
    string Login();
    string Disconnect();
}

[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("19fd86e2-f1b9-478c-ba7a-bd76bdf19b85")]  //Allocate your own GUID
[ProgId("TestDll.Communication")]
public class Communication : ICommunication
{
    public string TestProp { get; set; }

    public string Login()
    {
        // use of BouncyCastle here
        return "logged in";
    }

    public string Disconnect()
    {
        // ...
        return "disconnected";
    }
}

通过引用生成的 tlb 文件,我可以正确使用 Property 以及 Disconnect 函数,但是调用 Login 函数会导致问题(Excel 中的“找不到文件”消息框),我猜这与引用的 BouncyCastle 的使用有关。

Sub Button1_Click()
    Dim o: Set o = CreateObject("TestDll.Communication")
    Dim value As String
    value = o.Login()
    MsgBox value
End Sub

在 com 可见库中处理对其他 .net 程序集的引用的最佳方法是什么?到目前为止,我尝试向 GAC 注册 bouncycastle,但没有成功。

谢谢:)

【问题讨论】:

  • 我的猜测是 Login 方法中的某处发生了异常,或者在 JIT 时间发生了异常。为了诊断这个问题,我建议: 1) 将Login 方法的全部内容移动到辅助方法(private string LoginHelper() {...})。 2) 重写Login 方法以在try/catch 块内调用您的助手。以某种方式记录异常。
  • 如果没有看到 Login() 方法中的代码,作为起点,您可以将其包装在 try/catch 中并显示带有任何异常详细信息的消息框。将using System.Windows.Forms; 添加到您的班级,并在您的方法中添加try { your code; } catch (Exception e) { MessageBox.Show(e.ToString()); }。除此之外,创建一个 WinForms 或 WPF 应用程序作为测试外壳,其中包含您可以运行的方法副本以进行更好的测试。
  • 谢谢。尝试捕获并返回异常。ToString() 是个好主意!

标签: c# excel com tlbexp vba


【解决方案1】:

确实就像上面建议的一个无法读取的文件: 在每次构建时,我都会在项目的 bin 目录中放置一个 .txt,该目录是运行时读取的。 在我的解决方案中使用 dll 可以找到该文件,因为相对路径是我的 bin 目录,但是使用 tlb 相对根路径是已登录 Windows 用户的文档文件夹。

有趣的是,我一直认为该错误与我将 dll 设置为 com 可见的方式有关:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2012-04-23
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2015-03-20
    相关资源
    最近更新 更多