如何封装自己的动态库应用案例

一、生成dll文件

1.创建一个dll应用程序生成dll



2.添加mysocketclient.c文件

3.生成mysocketclient.dll

4.会在相应目录下生成mysocketclient.dll和mysocketclient.lib文件

二、创建测试程序加载dll文件

1.创建win32空项目并添加这两个文件

2.将生成的dll文件和lib文件拷到测试案例的相应目录下

3.添加lib文件名字



附录:

mysocketclient.c

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

typedef struct SCK_HANDLE
{
	char version[64];
	char ip[128];
	int  port;
	unsigned char *p;
	int plen;

}SCK_HANDLE;

//客户端初始化 获取handle
__declspec(dllexport)
	int cltSocketInit(void **handle/*out*/)
{
	int ret = 0;
	SCK_HANDLE *hdl = NULL;

	printf("cltSocketInit begin\n");

	hdl = (SCK_HANDLE*)malloc(sizeof(SCK_HANDLE));

	if (hdl == NULL)
	{
		ret = -1;
		printf("func cltSocketInit() err:%d\n",ret);
		return ret;
	}
	memset(hdl,0,sizeof(SCK_HANDLE));

	//strcpy(hdl->version,"1.1");
	strcpy(hdl->ip,"192.168.1.12");

	hdl->port = 8088;
	hdl->plen = 0;

	*handle = hdl;

	printf("cltSocketInit end\n");

	return ret;
}

//客户端发报文
__declspec(dllexport)
	int cltSocketSend(void *handle /*in*/,unsigned char *buf /*in*/, int buflen/*in*/ )
{
	int ret = 0;
	SCK_HANDLE *hdl =NULL;

	hdl = (SCK_HANDLE*)handle;

	hdl->p = (unsigned char*)malloc(sizeof(unsigned char)*buflen);
	if (hdl->p == NULL)
	{
		ret = -1;
		printf("func cltSocketSend() err :%d\n",ret);
		return ret;
	}

	memset(hdl->p,0,sizeof(char)*buflen);

	memcpy(hdl->p,buf,buflen);
	hdl->plen = buflen;

	return ret;
}

//客户端接收报文
__declspec(dllexport)
	int cltSocketRecv(void *handle/*in*/,unsigned char *buf/*in*/, int *buflen/*out*/)
{
	int ret = 0;
	SCK_HANDLE *hdl =NULL;

	if (handle == NULL || buf == NULL)
	{
		ret = -1;
		printf("func cltSocketRecv() err :%d\n (handle == NULL || buf == NULL) ",ret);
		return ret;
	}

	hdl = (SCK_HANDLE*)handle;

	memcpy(buf,hdl->p,hdl->plen);

	*buflen = hdl->plen;

	return ret;
}

//客户端释放资
__declspec(dllexport)
	int cltSocketDestory(void*handle/*in*/)
{
	int ret = 0;
	SCK_HANDLE *hdl =NULL;

	hdl = (SCK_HANDLE*)handle;

	if (handle == NULL)
	{
		ret = -1;
		printf("func cltSocketDestory() err :%d\n (handle == NULL) ",ret);
		return ret;
	}
	if (hdl->p!=NULL)
	{
		free(hdl->p);
	}
	return ret;
}

mysocketclientdll.h

#ifndef _INC_Demo01_H
#define _INC_Demo01_H

#ifdef __cplusplus
extern "C"{

#endif
	//--------------第一套api接口---Begin-----------------------------//

	//客户端初始化 获取handle
	int cltSocketInit(void **handle/*out*/);

	//客户端发报文
	int cltSocketSend(void *handle /*in*/,unsigned char *buf /*in*/, int buflen/*in*/ );

	//客户端接收报文

	int cltSocketRecv(void *handle/*in*/,unsigned char *buf/*in*/, int *buflen/*out*/);

	//客户端释放资
	int cltSocketDestory(void*handle/*in*/);

	//--------------第一套api接口---End-----------------------------//
#ifdef __cplusplus   
}   
#endif

#endif

测试框架.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include<stdio.h>
#include <string.h>
#include "socketclientdll.h"


int main()
{
	int		ret = 0;

	void	*handle = NULL;
	unsigned char buf[1024];
	unsigned char out[1024];

	int		buflen = 0;
	int		outlen = 0;

	//strcpy((unsigned char *)buf,"abcdefghijklmnop");

	memcpy(buf,"abcdefghijklmnop",17);

	buflen = 17;

	//printf("buf:%s\n",buf);

	//客户端初始化 获取handle
	ret = cltSocketInit(&handle/*out*/);

	if (ret!=0)
	{
		printf("func cltSocketInit() err:%d\n",ret);
		return ret;
	}

	//客户端发报文
	ret = cltSocketSend(handle /*in*/,buf /*in*/,buflen/*in*/ );

	if (ret!=0)
	{
		printf("func cltSocketSend() err:%d\n",ret);
		return ret;
	}

	//客户端接收报文

	ret = cltSocketRecv(handle/*in*/,out/*in*/, &outlen/*out*/);

	if (ret!=0)
	{
		printf("func cltSocketRecv() err:%d\n",ret);
		return ret;
	}

	//客户端释放资
	ret = cltSocketDestory(handle/*in*/);

	if (ret!=0)
	{
		printf("func cltSocketDestory() err:%d\n",ret);
		return ret;
	}

	printf("out:%s\n",out);

	system("pause");
	return ret;
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值