传智扫地僧课程学习笔记。
上节课是回调函数,即反向调用,
老实说,我对这个反向,一直形成不了感性的认识,
借着正向调用,来对比记忆吧,
正向调用,就是一个应用程序,调用很多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;
CltSocketInit cltSocketInit; //用函数指针类型 定义 函数指针变量
CltSocketSend cltSocketSend;
CltSocketRev cltSocketRev;
CltSocketDestory cltSocketDestory;
//找函数的入口地址
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];
int buflen = 9;
unsigned char out[2048];
int outlen;
int ret = 0;
strcpy((char *)buf, "aaaabbbdddddddddddddsssssssssss");
ret = cltSocketInit(&handle);
ret = cltSocketSend(handle, buf, buflen);
ret = cltSocketRev(handle, out, &outlen);
ret = cltSocketDestory(handle);
}