• 动态库封装参考模板


      1 #include "stdio.h"
      2 #include "stdlib.h"
      3 #include "string.h"
      4 //VS2012 动态库 第一套模板
      5 typedef struct _SCK_HANDLE {
      6     char    version[16];
      7     char    serverip[16];
      8     int        serverport;
      9     char    *pBuf;
     10     int        buflen;
     11 }SCK_HANDLE;
     12 
     13 //------------------第一套api接口---Begin--------------------------------//
     14     //客户端初始化 获取handle上下文
     15 __declspec(dllexport) //加这个才能导出lib
     16 int cltSocketInit(void **handle /*out*/)
     17 {
     18     int ret = 0;
     19     SCK_HANDLE *sh = NULL;
     20     sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
     21     if(sh == NULL)
     22     {
     23         ret = -1;
     24         //打印日志
     25         printf("func cltSocketInit() err:%d", ret);
     26         return ret;
     27     }
     28     strcpy(sh->version, "1.0.0");
     29     strcpy(sh->serverip, "192.168.0.100");
     30     sh->serverport = 8080;
     31     *handle = sh;
     32     return 0;
     33 }
     34 
     35 //客户端发报文
     36 __declspec(dllexport)
     37 int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/,  int buflen /*in*/)
     38 {
     39     int ret = 0;
     40     SCK_HANDLE *sh = NULL;
     41     if(handle == NULL || buf == NULL || buflen > 33333333333)
     42     {
     43         ret = -1;
     44         printf("func cltSocketSend() err:%d", ret);
     45         return ret;
     46     }
     47     sh = (SCK_HANDLE *)handle;
     48     sh->pBuf = (char *)malloc(buflen * sizeof(char));
     49     if(sh->pBuf == NULL)
     50     {
     51         ret = -1;
     52         printf("func cltSocketSend() malloc err, buflen:%d", buflen);
     53         return ret;
     54     }
     55     memcpy(sh->pBuf, buf, buflen);
     56     sh->buflen = buflen;
     57 
     58     return ret;
     59 }
     60 
     61 //客户端收报文
     62 __declspec(dllexport)
     63 int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
     64 {
     65     int ret = 0;
     66     SCK_HANDLE *sh = NULL;
     67     if(handle == NULL || buf == NULL || buflen == NULL)
     68     {
     69         ret = -1;
     70         printf("func cltSocketRev() err:%d", ret);
     71         return ret;
     72     }
     73     sh = (SCK_HANDLE *)handle;
     74     
     75     memcpy(buf, sh->pBuf, sh->buflen);
     76     *buflen = sh->buflen;
     77     return ret;
     78 }
     79 
     80 //客户端释放资源
     81 __declspec(dllexport)
     82 int cltSocketDestory(void *handle/*in*/)
     83 {
     84     int ret = 0;
     85     SCK_HANDLE *sh = NULL;
     86     if(handle == NULL)
     87     {
     88         ret = -1;
     89         printf("func cltSocketRev() err:%d", ret);
     90         return ret;
     91     }
     92     sh = (SCK_HANDLE *)handle;
     93 
     94     if(sh->pBuf != NULL)
     95     {
     96         free(sh->pBuf);
     97     }
     98     free(sh);
     99 
    100     return ret;
    101 }
  • 相关阅读:
    Java基础之单元测试与包装类
    Java基础之多态性
    Java基础之继承性与super
    Java 基础之方法的重写
    LeetCode 【困难】数据库-第579:查询员工的累计薪水
    LeetCode 【困难】数据库-第618:学生地理信息报告(分组行列转换)
    python 合并指定的excel表中的sheet中的行。 去重,留唯一
    LeetCode 【困难】数据库-第569:员工薪水中位数
    LeetCode 【困难】数据库-第571:给定数字的频率查询中位数
    LeetCode 【困难】数据库-第185:部门工资前三高的所有员工
  • 原文地址:https://www.cnblogs.com/yongssu/p/4274715.html
Copyright © 2020-2023  润新知