.net下通过委托调用c++ 的dll文件中的回调函数

时间:2022-09-01 17:58:09
   定义DLL相关的方法:
typedef void(__stdcall * ptest)(char *,char *);  /*注意此处必须是__stdcall,该方法在.net中实现的,涉及到夸语言调用,此处必须是__stdcall,否则会出现Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.*/

static ptest cdlltest = NULL;

extern "C" __declspec(dllexport) void setptest(ptest notiGISSta)
{
cdlltest = notiGISSta;

}


extern "C" __declspec(dllexport) void getdatefromdll(char * pc)
{
char str[2048] = { 0 };
char str1[2048] = { 0 };
sprintf_s(str, 2048,
"{\"sender\":\"%s\","
"\"latitude\":\"%s\","
"\"longitude\":\"%s\"}"
, pc, pc, pc);

sprintf_s(str1, 2048,
"{\"a\":\"%s\","
"\"b\":\"%s\","
"\"c\":\"%s\"}"
, pc, pc, pc);

cdlltest(str, str1);
}
   .net调用:
         void ShowDllString(string cString, string cStringb)
{
string vc = cString + cStringb;
MessageBox.Show(vc);
//return cString;
}

private void button1_Click(object sender, EventArgs e)
{
DllDelegate stringDelegate = new DllDelegate(ShowDllString);

setcallback(stringDelegate);

paddsd("2017-07-10");
}