How to create a dll in C ++ for use in C #

I have a little question to ask you.

I have one C ++ source and one header file. The C ++ file uses the windows.h library, performs operations using the serial port (basic operations: read (), write () , etc.).

I want to do this by creating a library using these files, and use this library in my C # .Net solution .

Which library do I need to create? How can I do it? After creating the library, how can I import it into a C # solution?

Best wishes.

The code elements that I use are:

// MathFuncsDll.h namespace MathFuncs { class MyMathFuncs { public: // Returns a + b static __declspec(dllexport) double Add(double a, double b); // Returns a - b static __declspec(dllexport) double Subtract(double a, double b); // Returns a * b static __declspec(dllexport) double Multiply(double a, double b); // Returns a / b // Throws DivideByZeroException if b is 0 static __declspec(dllexport) double Divide(double a, double b); }; } // MathFuncsDll.cpp // compile with: /EHsc /LD #include "MathFuncsDll.h" #include <stdexcept> using namespace std; namespace MathFuncs { double MyMathFuncs::Add(double a, double b) { return a + b; } double MyMathFuncs::Subtract(double a, double b) { return a - b; } double MyMathFuncs::Multiply(double a, double b) { return a * b; } double MyMathFuncs::Divide(double a, double b) { if (b == 0) { throw new invalid_argument("b cannot be zero!"); } return a / b; } } 

C # import part:

 [DllImport("SimpleDll.dll", CallingConvention=CallingConvention.Cdecl)] public static extern double Add(double a, double b); static void Main(string[] args) { string a = Add(1.0, 3.0)); } 
+6
source share
4 answers

After a few comments here try:

C ++ code (DLL), for example. math.cpp compiled into HighSpeedMath.dll file:

 extern "C" { __declspec(dllexport) int __stdcall math_add(int a, int b) { return a + b; } } 

C # code, for example. Program.cs:

 namespace HighSpeedMathTest { using System.Runtime.InteropServices; class Program { [DllImport("HighSpeedMath.dll", EntryPoint="math_add", CallingConvention=CallingConvention.StdCall)] static extern int Add(int a, int b); static void Main(string[] args) { int result = Add(27, 28); } } } 

Of course, if the entry point matches already, you do not need to specify it. Same thing with the calling convention.

As pointed out in the comments, the DLL should provide a C interface. This means extern "C", no exceptions, references, etc.

Edit:

If you have a header and source file for your DLL, it might look like this:

math.hpp

 #ifndef MATH_HPP #define MATH_HPP extern "C" { __declspec(dllexport) int __stdcall math_add(int a, int b); } #endif 

math.cpp

 #include "math.hpp" int __stdcall math_add(int a, int b) { return a + b; } 
+6
source

You need to compile your C ++ code into a dynamic link library and do the following in C #:

  class MyClass { [DllImport("MyDLL.dll")] public static extern void MyFunctionFromDll(); static void Main() { MyFunctionFromDll(); } } 
+5
source

In addition to Lichian's suggestion for compiling into a regular DLL and using p / invoke, which is probably the easiest way, you can also create your C ++ as a COM component (maybe something you don't want to do) and the third option is to add a thin C ++ / CLI layer for example.

 using namespace System; namespace youcomany{ namespace CPPWrapper { Wrapper::Function(String^ parameter) { //call the rest of you C++ from here } }} 
+1
source

You can use C # DllImport and Dllexport for DLL Interop as a starting point. And here is the Invoke Tutorial Platform

Hope this helps.

+1
source

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


All Articles