The C code is as follows:
# Include "stdio. h"
_ Declspec (dllexport) int Call (int (* qq) (int num), char * str)
{
Printf (str );
Return qqqq (123 );
}
Multiple verifications show that the function pointer passed in C # To C can be run successfully if the delegate does not contain parameters, but the delegate parameter can be either int, string, or other parameters, "Try to read or write the protected memory. This usually indicates that other memory is corrupted. After finding a solution for one day, you can add [UnmanagedFunctionPointer (CallingConvention. Cdecl)] to the delegate Statement of C #. The correct call is as follows:
[UnmanagedFunctionPointer (CallingConvention. Cdecl)]
Public delegate int MyDeg (int num );
Class Program
{
Static void Main (string [] args)
{
Try
{
MyDeg myd = new MyDeg (FF );
Console. WriteLine (Call (myd, "helloworld "));
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
}
Static int FF (int num)
{
Console. WriteLine (num );
Return num + 1;
}
[DllImport ("my. dll", EntryPoint = "Call")]
Public extern static int Call (MyDeg mm, string str );
}