C# 调vc带回调的动态库

 

 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);

 

posted @ 2023-04-11 16:55  cnchengv  阅读(33)  评论(0)    收藏  举报