How to create dll in C++ for using in C# - c#

I've a little question to ask you.
I have one C++ source and one header files. The C++ file uses windows.h library, makes operations using serial port(basic operations: read(), write() etc.).
What I want to do is, creating a library using these files, and use that library in my C#.Net solution.
What type of library I need to create?
How can I do it?
After creating library, How can I import it to C# solution?
My best regards.
Code Parts I'm using:
// 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));
}

After several comments, here a try:
C++ Code (DLL), eg. math.cpp, compiled to HighSpeedMath.dll:
extern "C"
{
__declspec(dllexport) int __stdcall math_add(int a, int b)
{
return a + b;
}
}
C# Code, eg. 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 don't have to specify it. The same with the calling convention.
As mentioned in the comments, the DLL has to provide a C-interface. That means, extern "C", no exceptions, no references etc.
Edit:
If you have a header and a source file for your DLL, it could 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;
}

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();
}
}

You may use C# DllImport and Dllexport for DLL Interop walkthrough as a starting point. And here is the Platform Invoke Tutorial
Hope this helps.

In addition to Lichian's offer to compile to a regular DLL and use p/invoke which is probably the simplest way
You can also create your C++ as a COM component (probably something you don't want to do) and the 3rd option you have is to add a thin layer of C++/CLI
e.g.
using namespace System;
namespace youcomany{ namespace CPPWrapper
{
Wrapper::Function(String^ parameter)
{
//call the rest of you C++ from here
}
}}

Related

.NET DllImport error EntryPointNotFoundException: Binding AWS-Encryption-SDK from C and C++ to C#

I'm working on a .NET 5 project to bind the AWS-Encryption-SDK from C and C++ to C# using PInvoke, type marshalling, and the DllImportAttribute. When attempting to call this C++ function
aws_cryptosdk_keyring *Build(const Aws::String &generator_key_id, const Aws::Vector<Aws::String> &additional_key_ids = {}) const;
using
[DllImport("aws-encryption-sdk-cpp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern aws_cryptosdk_keyring Build(string generator_key_id, string[] additional_key_ids = null);
I get this error
System.EntryPointNotFoundException : Unable to find an entry point named 'Build' in DLL 'aws-encryption-sdk-cpp.dll'.
The dll signature for this function is
?Build#Builder#KmsKeyring#Cryptosdk#Aws##QEBAPEAUaws_cryptosdk_keyring##AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##AEBV?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##7##Z
So far nothing that I have tried as the EntryPoint has been successful. Does anyone have any ideas?
Expanding on #jwezorek's comment, C# does not know how to call C++ (class) functions, only C functions. There are several reasons why this is so, but I won't get into them here.
You need to create (in C++) a wrapper that converts each C++ call to a 'flattened' C call. For example:
extern "C" void *createClass() {
return (void*) new myClass();
}
extern "C" void destroyClass(void* self) {
delete (myClass*) self;
}
extern "C" int callFunction1(void* self, int x, int y) {
return ((myClass*) self)->function1(x, y);
}
You'll need to write a function for each class function (plus the constructor and destructor). Then compile, linking it to that original C++ DLL's library. Then in C#, write a wrapper to call your flattened class:
public class myClass : IDisposable {
[DllImport("myFlattenedDll", EntryPoint="createClass")]
public static extern IntPtr createClass();
[DllImport("myFlattenedDll", EntryPoint="destroyClass")]
public static extern void destroyClass(IntPtr self);
[DllImport("myFlattenedDll", EntryPoint="callFunction1")]
public static extern int callFunction1(IntPtr self, int x, int y);
private IntPtr self;
public myClass() {
self = createClass();
}
~myClass() {
Dispose();
}
public virtual void Dispose() {
lock (this) {
destroyClass(self);
GC.SuppressFinalize(this);
}
}
public int callFunction1(int x, int y) {
return callFunction1(self, x, y);
}
}
You should also look into a tool called SWIG that can automate a good deal of the wrapper coding for you.

C++ Nested DLL function call from C# gives System.AccessViolationException

My C++ DLL name is MyDLL.dll, which is communicating with TestDLL.dll
Now, I want to call functions from MyDLL.dll in my C# project
Visualization
----------- communicates ------------- communicates ---------------
- C# Code - --------------> - MyDLL.dll - --------------> - TestDLL.dll -
----------- ------------- ---------------
MyDLL.dll is written in C++
TestDLL.dll is written in C
Here is the Code
MyDLL.h
#define MyDLL_API __declspec(dllexport)
extern "C" {
MyDLL_API int sum(int a, int b);
MyDLL_API int do();
}
MyDLL.cpp
#include "MyDLL.h"
#include <Windows.h>
extern "C" {
typedef int (CALLBACK* MYFUNC1)(int, int);
typedef short* (CALLBACK* MYFUNC2)(const char*);
int MyDLL_API sum(int a, int b) // This function works fine
{
return a + b;
}
int MyDLL_API do()
{
int t = -1;
HINSTANCE test_dll;
MYFUNC1 myFunc1;
MYFUNC2 myFunc1;
test_dll = LoadLibraryA("c:\\TestDLL.dll");
if (!test_dll) {
t=0;
}
myFunc1 = (MYFUNC1)GetProcAddress(test_dll, "TestFunc");
if (!myFunc1) {
t=1;
}
myFunc2 = (MYFUNC2)GetProcAddress(test_dll, "AuthFunc");
if (!myFunc2) {
t=2;
}
// If I put 'return t' here, it works fine!
(*myFunc2)("Hello World"); // <-- This line causing the error
return t;
}
}
C# Main Program
class Program
{
public class MyClass
{
[DllImport(#"c:\MyDLL.dll", EntryPoint = "sum")]
public static extern int Sum(int a, int b);
[DllImport(#"c:\MyDLL.dll", EntryPoint = "do")]
public static extern int Do();
}
static void Main(string[] args)
{
int summation = MyClass.Sum(30, 20); // This runs fine.
Console.WriteLine("Sum is: " + summation);
int resultCode = MyClass.Do(); // This causing the ERROR.
Console.WriteLine("Result code is: " + resultCode);
Console.ReadLine();
}
}
Exception Hint
System.AccessViolationException: 'Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.'

Confused over DLL entry points (entry point not found exception)

I'm trying to learn how to use DLL's in C#. I have a very simple DLL just to test the basics.
// MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DLL_Test
{
public partial class Form1 : Form
{
[DllImport("TestDLL.dll",
EntryPoint="?Add##YGHHH#Z",
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int Add(int a, int b);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int num;
try
{
num = Add(2, 3);
richTextBox1.AppendText(num.ToString() + "\n");
}
catch (DllNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}
catch (EntryPointNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
And the DLL code:
// TestDLL.cpp
__declspec(dllexport) int __stdcall Add(int a, int b) {
return(a + b);
}
dumpbin returns the following:
ordinal hint RVA name
1 0 00011005 ?Add##YGHHH#Z = #ILT+0(?Add##YGHHH#Z)
This (and other attempts listed below) have all returned the same exception:
System.EntryPointException: Unable to find entry point named "..."
So I am at a loss for how to solve this. Perhaps I do not understand how DllMain functions as the C# entry point for a DLL. TestDLL.dll works when I test it in a C++ application.
After searching for help, I've attempted the following changes:
// TestDLL.cpp
extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) {
return(a + b);
}
Which results in this from dumpbin
ordinal hint RVA name
1 0 00011005 _Add#8 = #ILT+135(_Add#8)
Thus, I changed my C# code:
// MainForm.cs
...
[DllImport("TestDLL.dll",
EntryPoint="_Add",
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int Add(int a, int b);
...
I've also tried __cdecl:
// TestDLL.cpp
extern "C" __declspec(dllexport) int __cdecl Add(int a, int b) {
return(a + b);
}
.
// MainForm.cs
...
[DllImport("TestDLL.dll",
EntryPoint="_Add",
ExactSpelling = true,
CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b);
...
Perhaps I'm misunderstanding the calling conventions. Any help would be very appreciated. Thank you.
use
extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) { ... }
and
[DllImport("TestDLL.dll", CallingConvention = CallingConvention.Stdcall)]
public static extern int Add(int a, int b);
extern "C" will prevent name mangling with params and return type such as ?Add##YGHHH#Z.
__stdcall will prepend an _ and add #8 : _Add#8 (where 8 is the total size of arguments). Note that it also affects the way parameters are pushed on the stack.
In your DLLImport statement, since you specify CallingConvention.StdCall, you don't need to specify the name mangling. Just give the regular name (Add) and .NET will take care of name mangling (_Add#8).
Note that you must specify the CallingConvention or .NET wouldn't emit the correct code to push arguments on the stack
The following should work.
Unmanged:
extern "C" __declspec(dllexport) int Add(int a, int b)
{
return(a + b);
}
Managed:
class Program
{
[DllImport("TestDLL.dll")]
public static extern int Add(int a, int b);
static void Main()
{
Console.WriteLine(Add(1, 2));
}
}
For future reference: I had a similar problem, solved creating an EMPTY C++ dll project. Probably the standard Visual Studio template causes some trouble.
Refer to this link: http://www.codeproject.com/Articles/9826/How-to-create-a-DLL-library-in-C-and-then-use-it-w

Callbacks from C++ back to C#

say I have a C++ library function for computing PI:
// pi.h:
#ifdef BUILDING_DLL
#define DLL_MACRO __declspec(dllexport)
#else
#define DLL_MACRO __declspec(dllimport)
#endif
namespace Cpp {
class PI {
public:
static double DLL_MACRO compute();
};
};
// pi.cpp:
#include "pi.h"
#include <cmath>
double
Cpp::PI::compute() {
// Leibnitz summation formulae:
double sum = 0.0;
for(long n = 0; n < 100*1000*1000; n++)
sum += 4.0*pow(-1.0, n)/(2*n + 1.0);
return sum;
}
I need to call this function from C#, and I want to use C++/CLI as a "bridge". However this C++ function is somewhat slow. Therefore the C# code calling this function need to get callbacks telling it how far the function has come in %.
The C# code might need some state, e.g. a progress bar, to deal with this info.
So the callbacks from C++ must enter into a member function on the C# side.
So I introduce:
// piCLI.h: The C++/CLI "bridge" between C# and C++
#include "pi.h"
#pragma once
namespace CLI {
public ref class PI abstract {
public:
double compute() {
return Cpp::PI::compute();
}
virtual void progress(int percentCompleted) = 0;
};
};
and
namespace CSharp
{
public class PI : CLI.PI
{
public override void progress(int percentCompleted)
{
System.Console.WriteLine(percentCompleted + "% completed.");
}
}
}
Now invoking CSharp.PI.compute() works fine :-). It forwards the call to Cpp::PI::compute() as intended.
But how do I get the C++ library to forward progress updates to CSharp.PI.progress() whilst Cpp::PI::compute() is running?
Thanks in advance for any answers!
I would take a function pointer/delegate approach as well:
// pi.h:
#pragma once
#ifndef DLL_MACRO
#ifdef BUILDING_DLL
#define DLL_MACRO __declspec(dllexport)
#else
#define DLL_MACRO __declspec(dllimport)
#endif
#endif
namespace Cpp {
typedef void (__stdcall *ComputeProgressCallback)(int);
class PI {
public:
static double DLL_MACRO compute(ComputeProgressCallback callback);
};
}
// pi.cpp:
#include "pi.h"
#include <cmath>
double Cpp::PI::compute(Cpp::ComputeProgressCallback callback) {
double sum = 0.;
for (long n = 0L; n != 100000000L; ++n) {
sum += 4. * std::pow(-1., n) / (2L * n + 1.);
callback(/*impl*/);
}
return sum;
}
// piCLI.h: The C++/CLI "bridge" between C# and C++
#pragma once
#include "pi.h"
namespace CLI {
public delegate void ComputeProgressDelegate(int percentCompleted);
public ref class PI abstract sealed {
public:
static double compute(ComputeProgressDelegate^ callback) {
using System::IntPtr;
using System::Runtime::InteropServices::Marshal;
IntPtr cbPtr = Marshal::GetFunctionPointerForDelegate(callback);
return Cpp::PI::compute(
static_cast<Cpp::ComputeProgressCallback>(cbPtr.ToPointer())
);
}
};
}
namespace CSharp {
public static class PI {
public static double compute() {
CLI.PI.compute(
percentCompleted => System.Console.WriteLine(
percentCompleted.ToString() + "% completed."
)
);
}
}
}
Or, to override an abstract progress method rather than creating a delegate on the C# side:
// piCLI.h: The C++/CLI "bridge" between C# and C++
#pragma once
#include "pi.h"
namespace CLI {
public ref class PI abstract {
delegate void ComputeProgressDelegate(int percentCompleted);
public:
double compute() {
using System::IntPtr;
using System::Runtime::InteropServices::Marshal;
ComputeProgressDelegate^ callback = gcnew ComputeProgressDelegate(
this,
&PI::progress
);
IntPtr cbPtr = Marshal::GetFunctionPointerForDelegate(callback);
return Cpp::PI::compute(
static_cast<Cpp::ComputeProgressCallback>(cbPtr.ToPointer())
);
}
protected:
virtual void progress(int percentCompleted) abstract;
};
}
namespace CSharp {
public sealed class PI : CLI.PI {
protected override void progress(int percentCompleted) {
System.Console.WriteLine(
percentCompleted.ToString() + "% completed."
);
}
}
}
Pass a C++/cli defined Native function to your native C++ as a callback and upon callback use a gcroot to call your managed C# function.

Enry point not found while call C++ dll from C# application

I have simple C++ dll function:
__declspec(dllexport) int tst1(int a);
int tst1(int a)
{
return a + 1;
}
I have C# application that calls it:
[DllImport("Project1.dll")]
public static extern int tst1(int i);
static void Main(string[] args)
{
Console.WriteLine( tst1(1) );
Console.ReadLine();
}
}
Got EntryPointNotFoundException error:
What I do wrong?
The name is getting mangled by C++ decoration. Add extern "C" to prevent the mangling of the name:
extern "C" __declspec(dllexport) int tst1(int a);

Categories

Resources