The error message you have contains good advice:
Verify that the calling agreement and PInvoke signature settings match the target unmanaged signature.
You should have the same calling convention indicated on both sides (C ++ dll and C # build). In C ++, you can specify it by adding a function declaration with one of __cdecl, __stdcall, etc.
extern "C" { __declspec(dllexport) int __stdcall Try(int v) { return 10 + v; } }
extern "C" { __declspec(dllexport) int __stdcall Try(int v) { return 10 + v; } }
On the C # side, you specify it with the DllImport attribute, the default is CallingConvention.StdCall, which corresponds to __stdcall in C ++, so it looks like you have __cdecl on the C ++ side. To fix the problem, use __stdcall in your DLL, as shown above, or use CDecl in C #, for example:
class Program { [DllImport("TestLib.dll", CallingConvention=CallingConvention.Cdecl )] public static extern int Try(int v); static void Main(string[] args) { Console.WriteLine("Wynik: " + Try(20)); Console.ReadLine(); } }
class Program { [DllImport("TestLib.dll", CallingConvention=CallingConvention.Cdecl )] public static extern int Try(int v); static void Main(string[] args) { Console.WriteLine("Wynik: " + Try(20)); Console.ReadLine(); } }
Konstantin Oznobihin
source share