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

来源:互联网 发布:java 发送微信消息 编辑:程序博客网 时间:2024/06/06 11:55

一、生成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 __cplusplusextern "C"{#endif//--------------第一套api接口---Begin-----------------------------////客户端初始化 获取handleint 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(){intret = 0;void*handle = NULL;unsigned char buf[1024];unsigned char out[1024];intbuflen = 0;intoutlen = 0;//strcpy((unsigned char *)buf,"abcdefghijklmnop");memcpy(buf,"abcdefghijklmnop",17);buflen = 17;//printf("buf:%s\n",buf);//客户端初始化 获取handleret = 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;}


0 0
原创粉丝点击