locked
Calling VB.NET dll from C++ RRS feed

  • Question

  • I have a C++ application exe that uses a C++ dll. For some reasons, I want to replace the C++ dll with a VB.NET dll.
    The dll is not and ActiveX and is therefore not registered.
    I cannot use a class but a module since the C++ application does not expect any class in the dll. In all my attempts, I could not have the inner functions of the VB.NET to be called by the C++ exe.
    How should I write the VB.NET dll?
    Thanks for the help.
    Menashe
    Tuesday, August 3, 2010 10:13 AM

Answers

All replies

  • Create your class library in Vb.net, which is already a COM visible by default but your method or function are not, so you have to expose each method to COM  or you can expose the whole class as ClassInterfaceType.AutoDual.

    Below is example of using ClassInterfaceType.AutoDual.

    <ClassInterface(ClassInterfaceType.AutoDual)> public class TestClass

       Function Add (Byval val1 as Integer, Byval val2 as Integer) As Integer
          Return val1 + val2
      End Sub
    End Class

    or you can do it by generating your own guid like it describe in this links

    http://www.codeproject.com/KB/cs/ManagedCOM.aspx

    http://support.microsoft.com/kb/828736/en-us

    http://stackoverflow.com/questions/106033/how-do-i-call-a-net-assembly-from-c-c

    Those link are written for C# but is the similar process for vb.net

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, August 3, 2010 11:39 AM
  • Thanks.

    You suggest two possibilities: eitehr expose the entire class or each method. If I don't want a class, how do you exose each method in your example?

     


    Menashe
    Tuesday, August 3, 2010 12:29 PM
  • Thanks.

    You suggest two possibilities: eitehr expose the entire class or each method. If I don't want a class, how do you exose each method in your example?

     


    Menashe


        There is no way you can create a DLL in .net without a class like in C++/C. VB.NET does not support header file or linker.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Tuesday, August 3, 2010 12:53 PM
  • The below article contains an example of how to create a COM-callable assembly in VB.NET (you can ignore the VB 6.0 part). I'm not aware of any other method that will allow you to export the functions from a VB.NET DLL to C++.

    http://support.microsoft.com/kb/817248

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, August 3, 2010 1:26 PM
  • I thank you but I omitted to say that the C++ Application (for which I don't have source code) load the dll using LoadLibrary and call the inner functions using a GetprocAddress. I therefore cannot use a class for this since the calling applicatoin is not aware of any class but rather expect to directly call the inner functions of my dll. Hence the need to directly expose these functions.

    In the meantime, I have been able to mix VB in my VC++ project so my C++ dll is in fact built of a mixture of both languages and I can progressively convert teh entire C to VB, which is great, but this is not the most comfortable solution.

     


    Menashe
    Monday, August 9, 2010 4:55 AM
  • Hi Menashe,

    In fact, there is three ways for calling .net dll in c++ code:

    1. Use a C++/CLI wrapper

    2. Host CLR

    3. Convert .NET assembly to a COM server, and call it from C++ through .NET-COM interop

    For more information and sample code:
    http://1code.codeplex.com/wikipage?title=Invoke%20.NET%20Assembly%20from%20Native%20C%2b%2b&referringTitle=Documentation

    If you still have question about it, please let me know.

     

    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Proposed as answer by Papy Normand Tuesday, June 12, 2018 6:09 PM
    Thursday, August 12, 2010 8:47 AM