How to call VB.NET DLL from C ++ (also call functions - not just a DLL file)

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

I tried calling the VB.NET DLL file from C ++ many times and it works fine, but the problem is that I cannot call the VB.NET DLL file function (I can only load the VB.NET DLL file)

in VB.NET DLL I have the following code:

Public Function example_function1(ByVal i As Integer) As Integer Return 3 End Function Public Function example_function2(ByVal i As Integer) As Integer Return 3 End Function 

==============================

My C ++ code:

  typedef int (__stdcall *ptf_test_func_1_type)(int); typedef int (__stdcall *ptf_test_func_2_type)(int*); int i =1; HINSTANCE dll_instance = LoadLibrary("DLLs7.dll"); int main() { if(dll_instance !=NULL) { printf("The DLLs file has been Loaded \n"); cout << GetLastError() << endl; ptf_test_func_1_type p_func1=(ptf_test_func_1_type)GetProcAddress(dll_instance,"Class1::example_function1"); ptf_test_func_2_type p_func2=(ptf_test_func_2_type)GetProcAddress(dll_instance,"Class1::example_function2"); // Function No 1 // if (p_func1 != NULL) { cout << "\nThe function number 1 is " << p_func1(i) << endl; } else { cout << "\nFailed" << endl; cout << GetLastError() << endl; } // Function No 2 // if (p_func2 != NULL) { cout << "\nThe function number 2 is" << p_func2(&i) << endl; } else { cout << "\nFailed" << endl; cout << GetLastError() << endl; } } else { printf("\nDLLs file Load Error"); cout << GetLastError() << endl; } cout << GetLastError() << endl; return(0); } 

My next steps:

1) I created the VB.NET DLL.

2) I created a new visual C ++ application and selected "win32 console application"

3) I wrote code to call DLLs and functions (as you can see above)

I missed anything in the step or code because I can call the VB.NET DLL file, but I cannot name the VB.NET DLL function

as you can see, I wrote GETLASTERRIR () to find ERROR

cout <GetLastError () <cps;

but I found this error 127 in the function on failure and 203 in the call dll

can someone help me

Thank you very much

Hello

+2
source share
4 answers

Since your vb assembly requires a completely different runtime than the "native" executable, you will need to use some layer in between. This layer can be COM.

You can open your assembly in the COM subsystem using the "ComVisible" property. Then you must register the assembly in order to open it to COM subscribers.

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

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

EDIT-- Just tried ... and everything seems to be fine:

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)] 

(In order to be able to generate tlb, signing is required)

C ++ Code:

 #import <adder.tlb> 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(); 
+5
source
  • GetProcAddress does not understand how to change the name of the C ++ language, not to mention any other manipulation, therefore there is no DLL for which "Class1::example_function1" can be a valid identifier. Usually it is used only with extern "C" (or implemented in C without ++) functions that are not distorted.
  • If it is implemented in VB.NET, it is not a dll at all . This is a .net assembly, and you need to work in the CLR (.net) environment to use it. You can run C ++ code in the CLR, but it must be "managed C ++", which is extended with special types for referencing .net objects and working with the garbage collector.
+2
source

You cannot directly access the .NET code from C ++, for this you need C ++ / CLI.

If your program should be native C ++, there is the possibility of writing a mixed-mode DLL shell that provides its own C ++ interface for the main program and uses the C ++ / CLI in the implementation to redirect calls to .NET. Dll.

+1
source

To do this, you need to write a wrapper in C ++ / CLI. You may find the following link helpful. http://www.codeproject.com/KB/mcpp/cppcliintro01.aspx

+1
source

Source: https://habr.com/ru/post/1266456/


All Articles