鱼C论坛

 找回密码
 立即注册
查看: 1933|回复: 0

[学习笔记] 020-动态库开发环境搭建

[复制链接]
发表于 2018-8-22 15:27:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 moc 于 2018-8-22 18:29 编辑

1、动态库项目的创建
图文教程详见:传送门
1. 文件——新建——项目;创建项目,并给项目命名如:myDll;
2.确定;下一步;应用程序类型选择Dll(D),附加选项选择空项目;
3、添加源文件、头文件编写程序;
4、运行生成解决方案,会在Debug下生成.myDll.dll和myDll.lib文件。

第一套api接口的dll封装:
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "string.h"

  4. typedef struct _SCK_HANDLE
  5. {
  6.         char version[16];
  7.         char serverip[16];
  8.         int serverport;
  9.         char *pbuf;
  10.         int buflen;
  11. }SCK_HANDLE;


  12. //------------------第一套api接口---Begin--------------------------------//
  13. //客户端初始化 获取handle上下
  14. __declspec(dllexport)     //该语句的含义是下面的函数是向外抛出的函数
  15. int cltSocketInit(void **handle /*out*/)
  16. {
  17.         int ret = 0;
  18.         SCK_HANDLE *sh = NULL;
  19.         sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
  20.         if (sh == NULL)
  21.         {
  22.                 ret = -1;
  23.                 printf("func cltSocketInit() error:%d", ret);
  24.                 return ret;
  25.         }
  26.         strcpy(sh->version, "1.0.0");
  27.         strcpy(sh->serverip, "192.168.0.0");
  28.         sh->serverport = 8080;

  29.         *handle = sh;
  30.         return ret;
  31. }

  32. //客户端发报文
  33. __declspec(dllexport)
  34. int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/, int buflen /*in*/)
  35. {
  36.         int ret = 0;
  37.         SCK_HANDLE *sh = NULL;
  38.         if (handle == NULL || buf == NULL || buflen > 1000000000)
  39.         {
  40.                 ret = -1;
  41.                 printf("func cltSocketSend error:%d", ret);
  42.                 return ret;
  43.         }
  44.         sh = (SCK_HANDLE *)handle;

  45.         sh->pbuf = (char *)malloc(buflen * sizeof(char));
  46.         if (sh->pbuf == NULL)
  47.         {
  48.                 ret = -2;
  49.                 printf("func cltSocketSend malloc error, buflen:%d", buflen);
  50.                 return ret;
  51.         }
  52.         memcpy(sh->pbuf, buf, buflen);
  53.         sh->buflen = buflen;

  54.         return ret;
  55. }

  56. //客户端收报文
  57. __declspec(dllexport)
  58. int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
  59. {
  60.         int ret = 0;
  61.         SCK_HANDLE *sh = NULL;
  62.         if (handle == NULL || buf == NULL || buflen == NULL)
  63.         {
  64.                 ret = -1;
  65.                 printf("func cltSocketRev error:%d", ret);
  66.                 return ret;
  67.         }
  68.         sh = (SCK_HANDLE *)handle;

  69.         memcpy(buf, sh->pbuf, sh->buflen);
  70.         *buflen = sh->buflen;

  71.         return ret;
  72. }

  73. //客户端释放资源
  74. __declspec(dllexport)
  75. int cltSocketDestory(void *handle/*in*/)
  76. {
  77.         int ret = 0;
  78.         SCK_HANDLE *sh = NULL;
  79.         if (handle == NULL)
  80.         {
  81.                 ret = -1;
  82.                 printf("func cltSocketDestory error:%d", ret);
  83.                 return ret;
  84.         }
  85.         sh = (SCK_HANDLE *)handle;

  86.         if (sh->pbuf != NULL)
  87.         {
  88.                 free(sh->pbuf);
  89.         }
  90.         free(sh);

  91.         return ret;
  92. }
  93. //------------------第一套api接口---End-----------------------------------//
复制代码

2、动态库文件的使用
1. 将Dll项目中生成的dll文件和lib文件(Debug文件夹里),拷贝到使用的项目文件夹(项目名.vcxproj所在的文件夹)下,(如果Dll有.h文件也一并拷入)。
2.(如果Dll有项目中有.h文件)头文件   添加现有项 添加.h文件到项目中;
3. 配置lib, 项目名 --- 属性 --- 链接器 --- 输入 --- 附加依赖项 --- 编辑 --- 输入myDll.lib --- 确定 --- 应用 ---- 确定。
4. 如果调试时想进入动态库源码,就需要多拷贝一个myDll.pdb文件。

编写程序使用动态库中的函数:
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "socketclientdll.h"

  4. /*
  5. .lib   资源描述文件,描述了.dll中,函数的引用方式
  6. .dll     编译生成的动态库文件(存放在API的入口地址)
  7. */

  8. void main()
  9. {
  10.         int ret = 0;
  11.         void *handle = NULL;
  12.         unsigned char buf[128];
  13.         int buflen = 11;  /*in*/

  14.         unsigned char outbuf[128];
  15.         int outbufLen;  /*in*/

  16.         strcpy(buf, "adddasdadddddddddasdada");

  17.         ret = cltSocketInit(&handle /*out*/);
  18.         if (ret != 0)
  19.         {
  20.                 printf("cltSocketInit error: %d", ret);
  21.                 goto End;
  22.         }

  23.         // 客户端发报文
  24.         ret = cltSocketSend(handle/*in*/, buf/*in*/, buflen/*in*/);
  25.         if (ret != 0)
  26.         {
  27.                 printf("cltSocketSend error: %d", ret);
  28.                 goto End;
  29.         }

  30.         // 客户端收报文
  31.         // 底层库提供一种机制,而不是一种策略
  32.         ret = cltSocketRev(handle/*in*/, outbuf, &outbufLen);
  33.         if (ret != 0)
  34.         {
  35.                 printf("cltSocketRev error: %d", ret);
  36.                 goto End;
  37.         }
  38.         printf("outbuf:%s \n", outbuf);

  39. End:
  40.         //客户端释放资源
  41.         if (handle != NULL)
  42.         {
  43.                 cltSocketDestory(handle/*in*/);
  44.         }
  45.         system("pause");
  46. }
复制代码



本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 04:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表