您的位置:首页 > 编程语言 > C语言/C++

c语言学习笔记 之调用dll动态库

2015-05-23 23:49 302 查看
项目基于vs2013一、新建dll库项目新建项目-->win32-->win32项目-->输入项目名-->选择Dll-->选择空项目-->完成头文件如下/*下面定义了一套socket客户端发送报文接受报文的api接口请写出这套接口api的调用方法*/#ifndef _INC_Demo01_H#define _INC_Demo01_H#ifdef  __cplusplusextern "C" {#endif//客户端初始化 获取handle上下int cltSocketInit(void **handle /*out*/);//客户端发报文int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/,  int buflen /*in*/);//客户端收报文int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/);//客户端释放资源int cltSocketDestory(void *handle/*in*/);#ifdef  __cplusplus}#endif#endif  /* _INC_Demo01_H */c文件:#include <stdio.h>#include <stdlib.h>#include <string.h>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*/){int ret = 0;SCK_HANDLE *sh = NULL;sh = (SCK_HANDLE *)malloc(sizeof(SCK_HANDLE));if (sh == NULL){ret = -1;printf("func cltSocketInit() err: %d, malloc err....", ret);return ret;}memset(sh, 0, sizeof(SCK_HANDLE));strcpy(sh->serverip, "192.168.0.128");sh->serverport= 88;*handle = sh;return ret;}//客户端发报文__declspec(dllexport)int cltSocketSend(void *handle /*in*/, unsigned char *buf /*in*/,  int buflen /*in*/){int ret = 0;SCK_HANDLE *sh = NULL;if (handle==NULL || buf==NULL){ret = -1;printf("func cltSocketSend() err: %d, (handle==NULL || buf==NULL)", ret);return ret;}sh = (SCK_HANDLE *)handle ;sh->buf = (char *)malloc(buflen);if (sh->buf == NULL){ret = -2;printf("func cltSocketSend() err: %d, (buflen:%d)", ret, buflen);return ret;}memcpy(sh->buf, buf, buflen);sh->buflen = buflen;return ret;}//客户端收报文__declspec(dllexport)int cltSocketRev(void *handle /*in*/, unsigned char *buf /*in*/, int *buflen /*in out*/){int  ret = 0;SCK_HANDLE *sh = NULL;if (handle==NULL || buf==NULL || buflen==NULL){ret = -1;printf("func cltSocketSend() err: %d, ((handle==NULL || buf==NULL || buflen==NULL))", ret);return ret;}sh = (SCK_HANDLE *)handle;memcpy(buf, sh->buf, sh->buflen);*buflen  = sh->buflen;if (sh->buf != NULL){free(sh->buf);sh->buf = NULL; //把状态回到原始sh->buflen = 0;}return ret;}//客户端释放资源__declspec(dllexport)int cltSocketDestory(void *handle/*in*/){int  ret = 0;SCK_HANDLE *sh = NULL;if (handle==NULL ){ret = -1;printf("func cltSocketSend() err: %d, ((handle==NULL )", ret);return ret;}sh = (SCK_HANDLE *)handle;if (sh->buf != NULL){free(sh->buf);sh->buf = NULL;sh->buflen = 0;}free(sh);return ret;}客户端:#include <stdio.h>#include <stdlib.h>#include <string.h>#include "socketclientdll.h"void main(){int ret = 0;void *handle = NULL;unsigned char buf[1024]; /*in*/int buflen = 10;unsigned char out[1024] = {0}; /*in*/int outlen = 0;strcpy(buf, "aadd3456789012233ddddddddddddddaaaaaaaaaaa");//客户端初始化 获取handle上下//运行的上下文环境ret = cltSocketInit(&handle/*out*/);if (ret !=0){printf("func cltSocketInit() err :%d \n", ret);return ;}// //客户端发报文ret = cltSocketSend(handle /*in*/, buf /*in*/,buflen /*in*/);if (ret !=0){printf("func cltSocketSend() err :%d \n", ret);return ;}//// //客户端收报文ret = cltSocketRev(handle /*in*/, out /*in*/, &outlen /*in out*/);if (ret != 0){printf("func cltSocketRev() err :%d \n", ret);return ;}printf("out: %s \n", out);//// //客户端释放资源ret = cltSocketDestory(handle/*in*/);if (ret != 0){printf("func cltSocketDestory() err :%d \n", ret);return ;}system("pause");}
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: