0


Right now I am writing a .net dll which should be useable within VBA code (Excel, Access etc.). The following setup is working fine:

[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";
    }
}

By referencing the generated tlb file I can properly use the Property aswell Disconnect function however calling the Login function leads to problems ("File not found" messagebox in Excel) which I guess are related to the usage of referenced BouncyCastle.

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

What is the best way to deal with references to other .net assemblies inside com visible libraries? So far I tried registering bouncycastle to the GAC with no success.

Thanks :)

Community
  • 1
  • 1
MaxWell
  • 43
  • 1
  • 5
  • My guess would be that there is an exception occurring somewhere within the `Login` method, or an exception is occurring at JIT time. To diagnose this issue, I recommend: 1) Move the entire content of the `Login` method to a helper method (`private string LoginHelper() {...}`). 2) Rewrite the `Login` method to call your helper within a `try`/`catch` block. Log the exception somehow. – Michael Gunter Mar 06 '17 at 22:54
  • Without seeing the code in your `Login()` method, as a starting point you could wrap it in a try/catch and show a messagebox with any exception details. Add `using System.Windows.Forms;` to your class, and in your method `try { your code; } catch (Exception e) { MessageBox.Show(e.ToString()); }`. Beyond that, create a WinForms or WPF application as a test shell with a copy of your method you can run for better testing. – zaphodalive Mar 07 '17 at 00:24
  • Thanks. Try catch and returning the exception.ToString() was a good idea! – MaxWell Jul 06 '18 at 08:15

1 Answers1

0

It was indeed like suggested above a file which could not be read: On each build I place a .txt in the bin directory of the project which is read runtime. Using the dll within my solution the file could be found since the relative path was my bin directory however using the tlb the relative root path was the document folder of the signed in windows user.

Funny how I thought the whole time that the error was related to the way I setup my dll as com visible :).

MaxWell
  • 43
  • 1
  • 5