C接口的封装和设计

导出和导入DLL

环境:win10, Visual Studio 2017

首先打一个简单的测试环境,这里我们采用的隐式链接

//.h文件
#pragma once
#ifndef _INC_TEST_H
#define _INC_TEST_H

#ifdef  __cplusplus
extern "C"
{
#endif
    //初始化
	int init(void **handle/*out*/);
    //发送数据
	int sendData(void *handle/*in*/, unsigned char *buf/*in*/, int bufLen/*in out*/);
    //接收数据
	int revData(void *handle/*in*/, unsigned char *buf/*in*/, int *bufLen/*in out*/);
    //释放内存
	int destory(void *handle/*in*/);
#ifdef  __cplusplus
}
#endif

#endif  /* _INC_TEST_H */
//.c主函数文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
	//动态库测试
	void *handle = NULL;
	unsigned char *buf = "I am massage!";
	int len, ret = 0;

	char revBuf[128] = { 0 };

	ret = init(&handle);
	if (ret != 0)
	{
		printf("Error: fun init!");
	}

	ret = sendData(handle, buf, strlen(buf));
	if (ret != 0)
	{
		printf("Error: fun sendData!");
	}
	printf("send = %s\n", buf);

	ret = revData(handle, revBuf, &len);
	if (ret != 0)
	{
		printf("Error: fun revData!");
	}
	printf("rev = %s, len = %d\n", revBuf, len);

	ret = destory(handle);
	if (ret != 0)
	{
		printf("Error: fun destory!");
	}
    system("pause");
	return ret;
}

接下来我们搭建DLL的环境,鼠标右键项目-->项目属性-->配置属性(常规)修改配置类型为 动态库(.dll)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct SCK_HANDLE
{
	unsigned char	*p;
	int		plen;
}THandle;

__declspec(dllexport)
int init(void **handle/*out*/)
{
	int ret = 0;
	THandle *h = (THandle *)malloc(sizeof(THandle));
	if (h == NULL)
	{
		ret = -1;
		printf("Error: DLL fun init! h == NULL");
	}
	memset(h, 0, sizeof(THandle));
	h->p = NULL;
	h->plen = 0;

	*handle = h;

	return ret;
}

__declspec(dllexport)
int sendData(void *handle/*in*/, unsigned char *buf/*in*/, int bufLen/*in out*/)
{
	int ret = 0;
	THandle *h = NULL;
	if (handle == NULL || buf == NULL)
	{
		ret = -1;
		printf("Error: DLL fun sendData! handle == NULL || buf == NULL");
	}
	h = (THandle *)handle;
	if (h == NULL)
	{
		ret = -2;
		printf("Error: DLL fun sendData! h == NULL");
	}
	h->p = (unsigned char *)malloc(sizeof(unsigned char)*bufLen);
	memcpy(h->p, buf, bufLen);
	h->plen = bufLen;
	return ret;
}

__declspec(dllexport)
int revData(void *handle/*in*/, unsigned char *buf/*in*/, int *bufLen/*in out*/)
{
	int ret = 0;
	THandle *h = NULL;
	if (handle == NULL || buf == NULL || bufLen == NULL)
	{
		ret = -1;
		printf("Error: DLL fun revData! handle == NULL || buf == NULL || bufLen == NULL");
	}
	h = (THandle *)handle;
	if (h == NULL)
	{
		ret = -2;
		printf("Error: DLL fun revData! h == NULL");
	}
	memcpy(buf, h->p, h->plen);
	*bufLen = h->plen;
	return ret;
}

__declspec(dllexport)
int destory(void *handle/*in*/)
{
	int ret = 0;
	THandle *h = NULL;
	if (handle == NULL)
	{
		ret = -1;
		printf("Error: DLL fun destory! handle == NULL");
	}

	h = (THandle *)handle;
	if (h->p != NULL)
	{
		free(h->p);
		h->p = NULL;
	}
	free(h);

	return ret;
}

业务逻辑完成之后生成DLL,点击菜单栏上面的生成-->生成选定内容

以上表示DLL生成成功。生成完成后会生成到项目源文件夹下的Debug文件夹下,有两个重要文件TestDell.dll和TestDell.lib,需要拷到之前我们搭建的环境下。

具体位置在:打开第一个项目鼠标右击项目-->在文件资源管理器中打开文件夹,把上面的TestDell.dll和TestDell.lib拷进去。接下来需要链接动态库,鼠标右击项目-->属性-->链接器-->附加依赖项--><编辑>

这样就可以动态链接库(DLL)了。具体的可以加断点,调试查看。

动态链接过程在加载的DLL模块时动态建立一个函数调用与函数地址的对应表。如果重新编译和重建DLL文件,并不需要修改应用程序,除非你改变了导出函数的符号名和参数序列。

在DLL代码中,必须像下面这样明确声明导出函数:__declspec(dllexport)

与DLL的隐式链接和显示链接

  • 隐式链接是指在应用程序中不需指明DLL文件的实际存储路径,程序员不需关心DLL文件的实际装载。而显式链接与此相反。

隐式链接

隐式链接时,在建立一个DLL文件时,链接程序会自动生成一个与之对应的LIB导入文件。该文件包含了每一个DLL导出函数的符号名和可选的标识号,但是并不含有实际的代码。LIB文件作为DLL的替代文件被编译到应用程序项目中。当程序员通过静态链接方式编译生成应用程序时,应用程序中的调用函数与LIB文件中导出符号相匹配,这些符号或标识号进入到生成的EXE文件中。LIB文件中也包含了对应的DLL文件名(但不是完全的路径名),链接程序将其存储在EXE文件内部。当应用程序运行过程中需要加载DLL文件时,Windows根据这些信息发现并加载DLL,然后通过符号名或标识号实现对DLL函数的动态链接。

显式链接

使用显示链接后,就不必再使用导入文件,而是直接调用Win32的LoadLibary函数,并指定DLL的路径作为参数。LoadLibary返回HINSTANCE参数,应用程序在调用 GetProcAddress函数时使用这一参数。GetProcAddress函数将符号名或标识号转换为DLL内部的地址。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

__declspec(dllexport) 
int test(int a, int b)
{
	return a + b;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>

int main(int argc, char** argv)
{
	int(*p_test)(int, int) = NULL;

	int add_result;

	HMODULE module = LoadLibraryA("Project1.dll");

	if (module == NULL)
		printf("加载DLL失败");

	p_test = (int(*)(int, int))GetProcAddress(module, "test");
	if (p_test != NULL) 
	{
		add_result = p_test(5, 5);
		printf("Add result is %d\n", add_result);
	}
	else 
	{
		system("function p_test can not excute");
	}
	FreeLibrary(module);

	system("pause");
	return 0;
}

DLL分配的内存该如何释放

  • 要保证内存分配和释放的统一性,数据的创建和清除应该放在同一个层次上面,像文章开头的例子中在DLL中分配了内存,那么这个DLL同时应该提供一个函数释放这些内存。

 

 
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值