020-动态库开发环境搭建
本帖最后由 moc 于 2018-8-22 18:29 编辑1、动态库项目的创建
图文教程详见:传送门
1. 文件——新建——项目;创建项目,并给项目命名如:myDll;
2.确定;下一步;应用程序类型选择Dll(D),附加选项选择空项目;
3、添加源文件、头文件编写程序;
4、运行生成解决方案,会在Debug下生成.myDll.dll和myDll.lib文件。
第一套api接口的dll封装:
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
typedef struct _SCK_HANDLE
{
char version;
char serverip;
int serverport;
char *pbuf;
int buflen;
}SCK_HANDLE;
//------------------第一套api接口---Begin--------------------------------//
//客户端初始化 获取handle上下
__declspec(dllexport) //该语句的含义是下面的函数是向外抛出的函数
int cltSocketInit(void **handle /*out*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
if (sh == NULL)
{
ret = -1;
printf("func cltSocketInit() error:%d", ret);
return ret;
}
strcpy(sh->version, "1.0.0");
strcpy(sh->serverip, "192.168.0.0");
sh->serverport = 8080;
*handle = sh;
return ret;
}
//客户端发报文
__declspec(dllexport)
int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/, int buflen /*in*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
if (handle == NULL || buf == NULL || buflen > 1000000000)
{
ret = -1;
printf("func cltSocketSend error:%d", ret);
return ret;
}
sh = (SCK_HANDLE *)handle;
sh->pbuf = (char *)malloc(buflen * sizeof(char));
if (sh->pbuf == NULL)
{
ret = -2;
printf("func cltSocketSend malloc error, buflen:%d", buflen);
return ret;
}
memcpy(sh->pbuf, buf, buflen);
sh->buflen = buflen;
return ret;
}
//客户端收报文
__declspec(dllexport)
int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
if (handle == NULL || buf == NULL || buflen == NULL)
{
ret = -1;
printf("func cltSocketRev error:%d", ret);
return ret;
}
sh = (SCK_HANDLE *)handle;
memcpy(buf, sh->pbuf, sh->buflen);
*buflen = sh->buflen;
return ret;
}
//客户端释放资源
__declspec(dllexport)
int cltSocketDestory(void *handle/*in*/)
{
int ret = 0;
SCK_HANDLE *sh = NULL;
if (handle == NULL)
{
ret = -1;
printf("func cltSocketDestory error:%d", ret);
return ret;
}
sh = (SCK_HANDLE *)handle;
if (sh->pbuf != NULL)
{
free(sh->pbuf);
}
free(sh);
return ret;
}
//------------------第一套api接口---End-----------------------------------//
2、动态库文件的使用
1. 将Dll项目中生成的dll文件和lib文件(Debug文件夹里),拷贝到使用的项目文件夹(项目名.vcxproj所在的文件夹)下,(如果Dll有.h文件也一并拷入)。
2.(如果Dll有项目中有.h文件)头文件 添加现有项 添加.h文件到项目中;
3. 配置lib, 项目名 --- 属性 --- 链接器 --- 输入 --- 附加依赖项 --- 编辑 --- 输入myDll.lib --- 确定 --- 应用 ---- 确定。
4. 如果调试时想进入动态库源码,就需要多拷贝一个myDll.pdb文件。
编写程序使用动态库中的函数:
#include "stdlib.h"
#include "stdio.h"
#include "socketclientdll.h"
/*
.lib 资源描述文件,描述了.dll中,函数的引用方式
.dll 编译生成的动态库文件(存放在API的入口地址)
*/
void main()
{
int ret = 0;
void *handle = NULL;
unsigned char buf;
int buflen = 11;/*in*/
unsigned char outbuf;
int outbufLen;/*in*/
strcpy(buf, "adddasdadddddddddasdada");
ret = cltSocketInit(&handle /*out*/);
if (ret != 0)
{
printf("cltSocketInit error: %d", ret);
goto End;
}
// 客户端发报文
ret = cltSocketSend(handle/*in*/, buf/*in*/, buflen/*in*/);
if (ret != 0)
{
printf("cltSocketSend error: %d", ret);
goto End;
}
// 客户端收报文
// 底层库提供一种机制,而不是一种策略
ret = cltSocketRev(handle/*in*/, outbuf, &outbufLen);
if (ret != 0)
{
printf("cltSocketRev error: %d", ret);
goto End;
}
printf("outbuf:%s \n", outbuf);
End:
//客户端释放资源
if (handle != NULL)
{
cltSocketDestory(handle/*in*/);
}
system("pause");
}
页:
[1]