10.动态库

第一套API函数

动态库代码:DJSocketClient.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma warning(disable:4996)

//客户端初始化 获取handle上下
__declspec(dllexport) // 把这个函数以动态库的形式导出来

typedef struct __SCK_HANDLE
{
    char               version[16];
    char               serverip[16];
    int                serverport;
    unsigned char      *buf;
    int                buflen;
}SCK_HANDLE;



// handle 运行的一块上下文环境
__declspec(dllexport)
int cltSocketInit(void **handle /*out*/)
{
    SCK_HANDLE *sk = NULL;
    sk = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));
    int ret = -1;
    if (sk == NULL)
    {
        printf("cltSocketInit err, malloc error
");
        return ret;
    }
    memset(sk, 0, sizeof(SCK_HANDLE));
    strcpy(sk->version,"1.0.0");
    strcpy(sk->serverip,"192.168.42.1");
    sk->serverport = 8081;
    *handle = sk;
    return 0;
}



//客户端发报文
__declspec(dllexport)
int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/, int buflen /*in*/)
{
    int ret = -1;
    SCK_HANDLE *sh = NULL;
    if (handle == NULL || buf == NULL)
    {
        printf("cltSocketSend err, argus err
");
        return -2;
    }

    sh = (SCK_HANDLE *)handle;

    sh->buf = (char *)malloc(buflen); // 这里要注意结构体的浅拷贝问题,不要再创建一个临时变量

    if (sh->buf == NULL)
    {
        printf("cltSocketSend err, sh->buf malloc err");
        return -3;
    }

    memcpy(sh->buf, buf, buflen);
    sh->buflen = buflen;

    return 0;

}


//客户端收报文
__declspec(dllexport)
int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/)
{
    SCK_HANDLE *sh = NULL;
    int ret = -1;

    if (handle == NULL || buf == NULL || buflen == NULL)
    {
        printf("cltSocketRev err, arugs err
");
        return -1;
    }

    sh = (SCK_HANDLE *)handle;
    memcpy(buf,sh->buf,sh->buflen);
    *buflen = sh->buflen;

    return 0;
}



//客户端释放资源
__declspec(dllexport)
int cltSocketDestory(void *handle/*in*/)
{
    SCK_HANDLE *sh = NULL;
    int ret = -1;

    if (handle == NULL)
    {
        printf("cltSocketDestory err, arugs err
");
        return -1;
    }

    sh = (SCK_HANDLE*)handle;

    if (sh->buf != NULL)
    {
        free(sh->buf);
        sh->buf = NULL;
        sh->buflen = 0;
    }

    if (sh != NULL)
    {
        free(sh);
    }
    return 0;
}

使用该动态库

#include <stdio.h>
#include <stdlib.h>
#include "socketclientdll.h"



int main()
{
    int ret = 0;
    void *handle = NULL;
    
    unsigned char sendbuf[1024] = "123456789eejioofajiofjaiofjalfjla";
    int send_buf_len = 12;

    unsigned char receivebuf[1024] = {0};
    int receive_buf_len = 1024;

    //客户端初始化 获取handle上下
    ret = cltSocketInit(&handle /*out*/);
    if (ret != 0)
    {
        printf("%s
","Socket初始化失败");
        return;
    }
    

    // 客户端发报文
    ret = cltSocketSend(handle /*in*/, sendbuf /*in*/, send_buf_len /*in*/);
    if (ret != 0)
    {
        printf("%s
", "数据发送失败");
        return;
    }

    //客户端收报文
    ret = cltSocketRev(handle /*in*/, receivebuf /*in*/, &receive_buf_len /*in out*/);
    if (ret != 0)
    {
        printf("%s
","数据接收失败");
        return;
    }
    else
    {
        printf("数据接收成功,接收到的数据为:%s
",receivebuf);
    }

    //客户端释放资源
    ret = cltSocketDestory(handle/*in*/);
    if (ret != 0)
    {
        printf("Socket释放失败");
        return;
    }

    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/yongdaimi/p/6951174.html