c++ CALLBACK 也就是 不能少,不然c#对不上 #define CALLBACK __stdcall
typedef void (CALLBACK * FUNCTION)(int iRet,char* strOut); int wsClientMain(int iType, FUNCTION callFun); extern "C" { __declspec(dllexport) int test1() { return 256; } __declspec(dllexport) int wsClienGetMsg(int iType, FUNCTION callFun) { return wsClientMain(iType,callFun); } }
C#
namespace testdlg { class PlatUpgrade { public static WSCallbackFUNCTION CallFun ; public const string dllFile = "platformUpgradeLib.dll"; [DllImport(dllFile, EntryPoint = "test1", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] public static extern int test1(); [DllImport(dllFile, EntryPoint = "wsClienGetMsg", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)] public static extern int wsClienGetMsg(int iType, WSCallbackFUNCTION callbackFunction); [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate void WSCallbackFUNCTION(int iRet, string strOut); public static void myCallback_Function(int iRet, string strOut) { string str = "####--->" + iRet + ", " + strOut; //MessageBox.Show(str); Console.WriteLine(str); Program.f1.richTextBox1.Text = str; } } } 调用时 PlatUpgrade.CallFun = new PlatUpgrade.WSCallbackFUNCTION(PlatUpgrade.myCallback_Function); PlatUpgrade.wsClienGetMsg(3,PlatUpgrade.CallFun);
标签:vc,wsClienGetMsg,C#,带回,int,PlatUpgrade,CallingConvention,static,public From: https://www.cnblogs.com/cnchengv/p/17306855.html