How to call VB.NET DLL from C++ (Call the functions also - not DLL file only)

前端 未结 4 394
长情又很酷
长情又很酷 2021-01-07 05:38

I want to ask question about how to call VB.NET DLL from C++ program

I have tried many times to call VB.NET DLL file from C++ and it is working fine but the problem

4条回答
  •  鱼传尺愫
    2021-01-07 06:23

    Since your vb assembly needs a totally different runtime than the 'native' executable, you will need to use some layer in between. This layer may be COM.

    You can expose your assembly to the COM subsystem by it's 'ComVisible' property. Then, you should register the assembly to expose it to COM 'subscribers'.

    Only then you can #import the assembly namespace from your c++ code.

    Note: this is a very brief version of an msdn article "How to call a managed DLL from native Visual C++ code"

    EDIT-- Just tried it out... and it seems to work allright:

    C# code

    namespace Adder
    {
        public interface IAdder
        {
            double add(double a1, double a2);
        }
        public class Adder : IAdder
        {
            public Adder() { }
            public double add(double a1, double a2) { return a1 + a2; }
        }
    }
    

    Project settings

    [assembly: ComVisible(true)]
    [assembly: AssemblyDelaySign(false)]
    

    (Signing was needed in order to be able to generate the tlb)

    C++ code:

    #import  raw_interfaces_only
    
      CoInitialize(NULL);
      Adder::IAdderPtr a;
      a.CreateInstance( __uuidof( Adder::Adder ) );
      double d = 0;
      a->add(1.,1., &d); 
      // note: the method will return a HRESULT; 
      // the output is stored in a reference variable.
      CoUninitialize();
    

提交回复
热议问题