如何封裝自己的動態庫應用案例
阿新 • • 發佈:2019-01-03
一、生成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; }