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

目录
相关文章
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
2567 2
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
1932 3
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
1070 3
C++STL底层原理:探秘标准模板库的内部机制
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
535 11
|
IDE 编译器 开发工具
msvcp100.dll,msvcp120.dll,msvcp140.dll,Microsoft Visual C++ 2015 Redistributable,Visual C++ 运行库安装
MSVC是Windows下C/C++开发核心工具,集成编译器、链接器与调试器,配合Visual Studio使用。其运行时库(如msvcp140.dll)为程序提供基础函数支持,常因缺失导致软件无法运行。通过安装对应版本的Microsoft Visual C++ Redistributable可解决此类问题,广泛应用于桌面软件、游戏及系统级开发。
1686 2
|
并行计算 C++ Windows
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
768 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)