动态库的lib文件里面保存了动态库dll中函数的起始地址,如果不用lib文件,而是我们自己手动通过函数指针来调用可以这样
//客户端初始化 获取handle上下
typedef int (*CltSocketInit)(void **handle );
//客户端发报文
typedef int (*CltSocketSend)(void *handle , unsigned char *buf , int buflen );
//客户端收报文
typedef int (*CltSocketRev)(void *handle , unsigned char *buf , int *buflen );
//客户端释放资源
typedef int (*CltSocketDestory)(void *handle);
//在这个函数里面完成动态库的加载
//利用winapi
void CMFC应用程序动态加载dll项目Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HINSTANCE hInstance = NULL;
hInstance=::LoadLibrary("c:/socketclient.dll");
CltSocketInit cltSocketInit = (CltSocketInit)::GetProcAddress(hInstance, "cltSocketInit");
CltSocketSend cltSocketSend = (CltSocketSend)::GetProcAddress(hInstance, "cltSocketSend");
CltSocketRev cltSocketRev = (CltSocketRev)::GetProcAddress(hInstance, "cltSocketRev");
CltSocketDestory cltSocketDestory = (CltSocketDestory)::GetProcAddress(hInstance, "cltSocketDestory");
void *handle = NULL;
unsigned char buf[100];
int buflen = 10;
memcpy(buf, "ddddddddddssssssssss", 10);
unsigned char out[100] = {0};
int outlen = 0;
int ret = cltSocketInit(&handle);
ret = cltSocketSend(handle, buf, buflen);
ret = cltSocketRev(handle, out, &outlen);
ret = cltSocketDestory(handle);
printf("out:%s", out);
//SQRTPROC* pFunction;
//VERIFY(hInstance=::LoadLibrary("c:\\winnt\\system32\\mydll.dll"));
//VERIFY(pFunction=(SQRTPROC*)::GetProcAddress(hInstance,"SquareRoot"));
//double d=(*pFunction)(81.0);//调用该DLL函数
AfxMessageBox("dddd");
}
这是函数指针的正向调用用法,函数指针主要的用法是逆向调用,比如前台调用后台dll中的接口,可以给后台传送一个函数指针,在后台里面实现回调前台的接口,从而实现模块解耦合