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
Simon source share