函数指针的正向调用案例

来源:互联网 发布:php固定资产管理系统 编辑:程序博客网 时间:2024/06/05 21:18

传智扫地僧课程学习笔记。



上节课是回调函数,即反向调用,

老实说,我对这个反向,一直形成不了感性的认识,

借着正向调用,来对比记忆吧,


正向调用,就是一个应用程序,调用很多dll,

我们在windows下可以通过winsdk,把dll加载到程序中,

然后查找函数地址,进行调用,


下面是老师课上示例代码,

先建立MFC项目,属性设置包括,1基于对话框 ,2不使用unicode,

然后添加一个button,双击后,就可以实现代码级别的修改了,如下了,

//函数指针类型  //客户端初始化 获取handle上下typedef int (*CltSocketInit)(void **handle /*out*/); //客户端发报文typedef int (*CltSocketSend)(void *handle /*in*/, unsigned char *buf /*in*/,  int buflen /*in*/);//客户端收报文typedef int (*CltSocketRev)(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/);//客户端释放资源typedef int (*CltSocketDestory)(void *handle/*in*/);//------------------第一套api接口---End-----------------------------------//void C函数指针正向调用Dlg::OnBnClickedButton1(){// TODO: 在此添加控件通知处理程序代码AfxMessageBox("ddddd");HINSTANCE hInstance;CltSocketInitcltSocketInit;  //用函数指针类型 定义 函数指针变量 CltSocketSendcltSocketSend;CltSocketRevcltSocketRev;CltSocketDestorycltSocketDestory;//找函数的入口地址  hInstance=::LoadLibrary("c:/socketclient.dll");cltSocketInit =(CltSocketInit)::GetProcAddress(hInstance, "cltSocketInit");cltSocketSend =(CltSocketSend)::GetProcAddress(hInstance, "cltSocketSend");cltSocketRev =(CltSocketRev)::GetProcAddress(hInstance, "cltSocketRev");cltSocketDestory =(CltSocketDestory)::GetProcAddress(hInstance, "cltSocketDestory");void*handle = NULL;unsigned char buf[2048];intbuflen = 9;unsigned charout[2048];intoutlen;intret = 0;strcpy((char *)buf, "aaaabbbdddddddddddddsssssssssss");ret = cltSocketInit(&handle);ret =  cltSocketSend(handle, buf, buflen);ret = cltSocketRev(handle, out, &outlen);ret = cltSocketDestory(handle);}


0 0