C # shell C ++ dll; "Runtime check error # 0 - ESP value was not properly stored during function call." error

Here is the code in the C ++ dll:

extern "C" _declspec(dllexport) int testDelegate(int (*addFunction)(int, int), int a, int b)
{
    int res = addFunction(a, b);
    return res;
}

and here is the code in C #:

public delegate int AddIntegersDelegate(int number1, int number2);

public static int AddIntegers(int a, int b)
{
  return a + b;
}

[DllImport("tester.dll", SetLastError = true)]
public static extern int testDelegate(AddIntegersDelegate callBackProc, int a, int b);

public static void Main(string[] args)
{
  int result = testDelegate(AddIntegers, 4, 5);
  Console.WriteLine("Code returned:" + result.ToString());
}

When I run this small application, I get a message from the header of this message. Can someone help please?

Thanks in advance,

D

+3
source share
3 answers

, 32- Windows. __cdecl, - CallingConvention.StdCall. , , C/++.

C/++:

typedef int (__stdcall * Callback)(int, int);

extern "C" _declspec(dllexport) int testDelegate(Callback addFunction, int a, int b)
{
    int res = addFunction(a, b);
    return res;
}

#:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int AddIntegersDelegate(int number1, int number2);
+11

testDelegate __stdcall, ( .)

+3

" ": , , dll.

0

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


All Articles