C++ 静态调用C的DLL库(调用lib文件)

简介: C++ 静态调用C的DLL库(调用lib文件)

编写DLL代码

新建工程

新建空项目工程

tt.png

点击确定。然后右键项目选择新建项。

tt.png

再次选择新建项,选中C++文件,将其改为MySocketClient.c,然后选择添加。

tt.png

到这里,新建工程就完成了。

tt.png

然后右键项目,选择属性 将配置类型改为动态库(.dll)

tt.png

编写头文件MySocketClient.h

#ifndef _INC_MYSOCKETCLIENT_H_


#define _INC_MYSOCKETCLIENT_H_


#define Import_SSS


#ifdef Import_SSS


#define API _declspec(dllexport)


#else


#define API _declspec(dllimport)


#endif



#ifdef _cplusplus//extern"C" 来告诉编译器:这是一个用C写成的库文件,请用C的方式来链接它们。


extern "C" {

#endif // _cplusplus


   API//导出函数,让外界调用。


   int socketClient_Init(void **handle);


   API


   int socketClient_Send(void *handle, unsigned char *buf, int buflen);


   API


   int socketClient_Recv(void *handle, unsigned char *buf, int *buflen);


   API


   int socketClient_Destory(void *handle);


#ifdef _cplusplus


}


#endif // _cplusplus


#endif //_INC_MYSOCKETCLIENT_H_


编写MySocketClient.c

#define _CRT_SECURE_NO_WARNINGS


#include <stdlib.h>


#include <string.h>


#include <stdio.h>


#include "MySocketClient.h"


typedef struct _Sck_Handle


{

   char version[16];


   char ip[16];


   int port;


   unsigned char *p;


   int len;


}Sck_Handle;//定义Handle的结构体。


int socketClient_Init(void **handle)


{

   int ret = 0;


   Sck_Handle *tmpHandle = NULL;


   if (handle == NULL)


   {

        ret = -1;


        printf("[socketClient_Init] err %d handle=NULL \n", ret);


        return ret;


   }


   tmpHandle = (Sck_Handle *)malloc(sizeof(Sck_Handle));


   if (tmpHandle == NULL)


   {

        ret = -2;


        printf("[socketClient_Init] err:%d malloc err \n", ret);


   }


   memset(tmpHandle, 0, sizeof(Sck_Handle));//初始化结构体。


   strcpy(tmpHandle->version, "1.0.0.1");


   strcpy(tmpHandle->ip, "192.168.12.121");


   tmpHandle->port = 11111;


   *handle = tmpHandle;


   return ret;


}


socket报文发送


//__declspec(dllexport)


int socketClient_Send(void *handle, unsigned char *buf, int buflen)


{

   int          ret = 0;


   Sck_Handle   *tmpHandle = NULL;


   if (handle == NULL || buf == NULL || buflen <= 0)


   {

        ret = -2;


        printf("func socketclient_send() err :%d  (handle == NULL ||  buf==NULL || buflen <=0 ) \n", ret);


        return ret;


   }


   tmpHandle = (Sck_Handle *)handle;


   tmpHandle->len = buflen;


   tmpHandle->p = (unsigned char *)malloc(buflen);


   if (tmpHandle->p == NULL)


   {

        ret = -2;


        printf("func socketclient_send() err :%d  malloc len:%d \n", ret, buflen);


        return ret;


   }


   memcpy(tmpHandle->p, buf, buflen); //数据的缓存到内存


   printf("接收到发送数据是%s \n", tmpHandle->p);


   return ret;


}


socket报文接受


//__declspec(dllexport)


int socketClient_Recv(void *handle, unsigned char *buf, int *buflen)


{

   int          ret = 0;


   Sck_Handle   *tmpHandle = NULL;


   if (handle == NULL || buf == NULL || buflen == NULL)


   {

        ret = -2;


        printf("func socketclient_recv() err :%d  (handle == NULL ||  buf==NULL || buflen==NULL ) \n", ret);


        return ret;


   }


   tmpHandle = (Sck_Handle *)handle;


   memcpy(buf, tmpHandle->p, tmpHandle->len);


   *buflen = tmpHandle->len; //间接赋值  告诉调用者 收到的数据的长度


   printf("数据长度是%d \n", tmpHandle->len);


   return ret;


}


socket环境释放


//__declspec(dllexport)


int socketClient_Destory(void *handle)


{

   int          ret = 0;


   Sck_Handle   *tmpHandle = NULL;


   if (handle == NULL)


   {

        return -1;


   }


   tmpHandle = (Sck_Handle *)handle;


   if (tmpHandle->p != NULL)


   {

        free(tmpHandle->p); //释放结构体 成员域的 指针所指向的内存空间


   }


   free(tmpHandle); //释放结构体内存


   return 0;


}


然后右键编译工程。在Debug文件夹下面就可以看到生成的dll



静态调用

静态调用,使用lib文件调用


#define  _CRT_SECURE_NO_WARNINGS


#include <stdio.h>


#include <windows.h>


#include <iostream>


using namespace std;


#pragma comment(lib,"MySocketClient.lib")


extern "C"


{


   int socketClient_Init(void **handle);


int socketClient_Send(void *handle, unsigned char *buf, int buflen);


int socketClient_Recv(void *handle, unsigned char *buf, int *buflen);


int socketClient_Destory(void *handle);


}


int main()


{



   unsigned char buf[1024];


   int buflen;



   unsigned char out[1024];


   int outlen;


   void *handle = NULL;


   int ret = 0;


   strcpy((char *)buf, "aaaaAAAAAFFffffffdddddddd");


   buflen = 9;


   //客户端初始化 获取handle上下


   ret = socketClient_Init(&handle /*out*/);


   if (ret != 0)


   {


        printf("func socketclient_init() err:%d \n ", ret);


        goto End;


   }


   //客户端发报文


   ret = socketClient_Send(handle /*in*/, buf /*in*/, buflen /*in*/);


   if (ret != 0)


   {


        printf("func socketclient_send() err:%d \n ", ret);


        goto End;


   }


   //客户端收报文


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


   if (ret != 0)


   {


        printf("func socketclient_recv() err:%d \n ", ret);


        goto End;


   }


   printf("接收到的数据长度是%d \n", outlen);



End:


   //客户端释放资源


   ret = socketClient_Destory(handle/*in*/);



   printf("hello...\n");


   system("pause");


   return 0;


}


运行结果:

tt.png



 


tt.png

目录
相关文章
|
18天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
19 2
|
29天前
|
存储 C++
基于C++的简易文件压缩与解压缩工具设计与实现
基于C++的简易文件压缩与解压缩工具设计与实现
16 3
|
30天前
|
算法 编译器 C语言
【C++ 异常】C++ 标准库异常类及其应用
【C++ 异常】C++ 标准库异常类及其应用
28 0
|
30天前
|
缓存 算法 C语言
【C++ 标准查找算法 】C++标准库查找算法深入解析(In-depth Analysis of C++ Standard Library Search Algorithms)
【C++ 标准查找算法 】C++标准库查找算法深入解析(In-depth Analysis of C++ Standard Library Search Algorithms)
46 0
|
9天前
|
编译器 C++
C++编程之美:探索初始化之源、静态之恒、友情之桥与匿名之韵
C++编程之美:探索初始化之源、静态之恒、友情之桥与匿名之韵
21 0
|
16天前
|
C++
glog --- C++日志库
glog --- C++日志库
|
24天前
|
C++ Python
【C++/Python】C++调用python文件
【C++/Python】C++调用python文件
|
24天前
|
XML JSON JavaScript
推荐一个比较好用的c++版本http协议库-cpp-httplib
推荐一个比较好用的c++版本http协议库-cpp-httplib
38 1
|
30天前
|
安全 网络性能优化 Android开发
深入解析:选择最佳C++ MQTT库的综合指南
深入解析:选择最佳C++ MQTT库的综合指南
87 0
|
30天前
|
XML 运维 监控
【深入探究 C++ 日志库清理策略】glog、log4cplus 和 spdlog 的日志文件管理策略
【深入探究 C++ 日志库清理策略】glog、log4cplus 和 spdlog 的日志文件管理策略
67 0