locked
How to reference a function in a vb.net module in a DLL? RRS feed

  • Question

  • I have a VB.NET DLL and am using vba to instantiate classes and read events. That is working fine. However, the DLL includes a Module with a function i would like to use in the vba code as well. How do i refer to the function? It seems like vba doesn't see it at all.
    Monday, November 2, 2020 9:52 PM

All replies

  • Maybe create a helper .NET class that is visible in VBA and can access the module. Or add intermediate functions to existing classes.

    Tuesday, November 3, 2020 6:38 AM
  • I'm not sure in VB but in C# you need to make Class and method COM Visible.

        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class TestDll
        {
            private int numberOne = 0;
            private int numberTwo = 0;
    
            [ComVisible(true)]
            public int n1 { get; set; }
    
            [ComVisible(true)]
            public int n2 { get; set; }
    
            [ComVisible(true)]
            public void SetNumberOne(int number)
            {
                numberOne = number;
            }
    

    Tuesday, November 3, 2020 1:26 PM
  • It does not seem to need [ComVisible(true)]. Instead the project has an Interface.

    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>
    Public Interface IDEventInterface
    End Interface

    Which it implements:

    <ComSourceInterfaces(GetType(IDEventInterface))>
    Public Class Main
    End Class

    The calls generally go through that. Although, other classes are visible in vba even without any special demarcation. So, i am confused.

    • Proposed as answer by Maxi456 Tuesday, November 3, 2020 4:37 PM
    Tuesday, November 3, 2020 1:40 PM
  • Maybe create a helper .NET class that is visible in VBA and can access the module. Or add intermediate functions to existing classes.

    Creating an intermediate helps. So, i can always do that. I still want to know (curiosity) if i can make the function itself visible directly.
    Tuesday, November 3, 2020 1:42 PM
  • Not sure if this helps.  This is an example I got from the book COM and .NET Interoperability  by ANDREW TROELSEN pg 609.  Can you view it in the Object browser.

    Imports System.Runtime.InteropServices
    ' This is the name of the event interface to be generated.
    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    Guid("68E53BB4-48F9-45cc-96C6-72033295E26A")>
    
    Public Interface _DEventInterface
        <DispId(1)> Sub TheEvent(ByVal msg As String)
    End Interface
    
    <ClassInterface(ClassInterfaceType.AutoDual),
    Guid("7D6A1F10-0224-4fbf-8C17-B7A4B707372A"),
    ComSourceInterfaces(GetType(_DEventInterface))>
    
    Public Class VbDotNetEventSender
        Public Event TheEvent(ByVal msg As String)
        Public Sub FireTheEvent()
            RaiseEvent TheEvent("Hello from the DotNetEventSender")
        End Sub
    End Class


    • Edited by mogulman52 Tuesday, November 3, 2020 3:14 PM
    Tuesday, November 3, 2020 3:02 PM

  • Creating an intermediate helps. So, i can always do that. I still want to know (curiosity) if i can make the function itself visible directly.

    You can try defining Shared subs of functions in your class, which act like module’s members in usual circumstances, but since COM is based on interfaces, it does not seem possible to make such members visible directly. (“A COM object exposes its features through an interface, which is a collection of member functions” [https://docs.microsoft.com/en-us/windows/win32/com/com-technical-overview#objects-and-interfaces]).

    Instead of intermediate subs and functions, you can also convert your module to a COM class. Module’s members will become class’s members.





    • Edited by Viorel_MVP Tuesday, November 3, 2020 4:08 PM
    Tuesday, November 3, 2020 4:04 PM
  • All good information, thank you.

    >you can also convert your module to a COM class.

    How many classes can a dll have? My understanding is the Interface must be named IDEventInterface and that it can only have one implementation. (I'd also rather not turn the module into a class were that avoidable.)

    Fwiw, i was trying to use a json to .net conversion function, but i have found a couple vba json parsers which are pretty easy to use. So, my problem has passed. I'm still a little curious though.

    Tuesday, November 3, 2020 4:20 PM