Good day! There are some simple functions on dll with ++ such as add, substract, divide, in the c # project I call them like this:

[DllImport("MathFuncs.dll",EntryPoint="Add",CallingConvention = CallingConvention.Cdecl)] public static extern double Add(double a, double b); [DllImport("MathFuncs.dll",EntryPoint="_Substract", CallingConvention = CallingConvention.Cdecl)] public static extern double Substract(double a, double b); 

and on the loaf respectively call the function:

  c = Add(c, tmp); 

The question is: when you call Add, everything works fine, but if you call other functions, you get the error: I can't find the entry point ... What is the problem? however, if you comment out all the functions and leave any one (except for add), it is still such an error.

Code on the pros

 #include "MathFuncsDll.h" _declspec(dllexport) double Sub(double a, double b) { return a - b; } _declspec(dllexport) double Add(double a, double b) { return a + b; } _declspec(dllexport) double Multiply(double a, double b) { return a*b; } _declspec(dllexport) double Divide(double a, double b) { if (b == 0) throw invalid_argument("b cannot be zero!"); return a / b; } 

And in the hider

 #include <stdexcept> using namespace std; extern "C" { __declspec(dllexport) double Add(double a, double b); } extern "C" { __declspec(dllexport) double Sub(double a, double b); } extern "C" { __declspec(dllexport) double Multiply(double a, double b); } extern "C" { __declspec(dllexport) double Divide(double a, double b); } 
  • Such an error is characteristic of an error in the signature of the function or in its name. - Yury Bakharev
  • Without the function export code in the plus code, it is difficult to answer your question - Andrey Buran
  • Look at the names of the functions exported from the dll using some DependencyWalker. - αλεχολυτ
  • Updated the answer. And another question for you - what will happen if you leave only [DllImport(@"MathFuncs.dll")] ? - isnullxbh

1 answer 1

Look, in the library code, whether extern "C" is used there - if not, then the names are flimsy, and your Add is not Add at all.

UPD: found out, not because of this. Then try to help number 2 : add _stdcall

 extern "C" { __declspec(dllexport) double __stdcall Add(double a, double b); } 

Code:

 // mathlib.h #pragma once extern "C" { __declspec(dllexport) double __stdcall Add( double, double ); __declspec(dllexport) double __stdcall Sub( double, double ); __declspec(dllexport) double __stdcall Mul( double, double ); __declspec(dllexport) double __stdcall Div( double, double ); } // mathlib.cpp #include "mathlib.h" __declspec(dllexport) double __stdcall Add( double lo, double ro) { return ( lo + ro ); } __declspec(dllexport) double __stdcall Sub( double lo, double ro ) { return ( lo - ro ); } __declspec(dllexport) double __stdcall Mul( double lo, double ro ) { return ( lo * ro ); } __declspec(dllexport) double __stdcall Div( double lo, double ro ) { return ( lo / ( ro == 0.0 ) ? 1.0 : ro ); } // C# app using System; using System.Runtime.InteropServices; namespace libconsm { class Program { [DllImport(@"mathlib.dll", EntryPoint ="Add", CallingConvention =CallingConvention.StdCall)] public static extern double Add(double a, double b); [DllImport(@"mathlib.dll", EntryPoint = "Sub", CallingConvention = CallingConvention.StdCall)] public static extern double Sub(double a, double b); static void Main(string[] args) { Console.Write(Add(12, 12)); Console.Write(Sub(13, 12)); } } } 

Here, actually. Just checked - everything works. By the way, when do you collect - take into account the debug / release version of the builds?

The results of dumpbin /exports command:

Release: Release

Debug: enter image description here

As you can see, they are different. So there in the settings when building .dll you need to shaman, if that.

  • error C2373: 'Add': redefinition; different type modifiers - Sergey
  • @ user230911, try writing the f-ii body directly in the header. extern "C" { __declspec(dllexport) double __stdcall Add(double a, double b){...} } - isnullxbh
  • @ user230911, kst., in the .cpp file also corrected - added __stdcall ? - isnullxbh
  • changed in .cpp "can not find the entry point" - Sergey
  • wrote the function body in the header - the error is the same - Sergey