c++ builder 动态调用 VS 2010 的DLL

VS 编译出的 DLL 要被 Builder 6 或者 Builder 2010 动态调用的时候,如果 dump出来 函数是
有修饰符了,这样需要用到def 文件 去规定函数 接口,防止 有修饰符。

1. vs 2010 的dumpbin.exe 可以用 vs 工具 里面的的命令提示

在这里插入图片描述

2. 命令是dumpbin.exe -exports XX.dll

没有修改之前是 有修饰符的,如果直接掉用是 invoke 不到的
在这里插入图片描述

3 VS 里面添加 def 文件,添加正常定义的文件

操作步骤:
右键 选择添加-》新建项-》选择 如下的 模块定义文件 def
名字最好和 dll 库的名字 一样。
在这里插入图片描述def文件内容:

LIBRARY     LibKPHA

EXPORTS
    KPHA_Check_TEE_State          = _KPHA_Check_TEE_State@4
    KPHA_Init2                    = _KPHA_Init2@8
	KPHA_Is_TEE_Supported         = _KPHA_Is_TEE_Supported@4
    KPHA_SetLogPath               = _KPHA_SetLogPath@4
    KPHA_ShutdownDevice           = _KPHA_ShutdownDevice@4
	KPHA_Check_RPMB               = _KPHA_Check_RPMB@12
	Get_KPHA_Version              = _Get_KPHA_Version@0

LIBRARY 后面要跟 要编译的DLL的名字

一定记得做下面的操作否则 def文件不生效

在工程的属性中添加 创建的def文件
在这里插入图片描述

4 重新编译后用dumpbin.exe 查看:

在这里插入图片描述
可以看到 已经加上了 def 文件的内容

5 使用代码测试例子如下:

- Vs DLL 开放的 接口函数:

#ifndef LIBKPHA_H_
#define LIBKPHA_H_

#define LIBKPHA_API __declspec(dllexport)
#define WINDOWS

#ifdef __cplusplus
extern "C" {
#endif

LIBKPHA_API int  __stdcall KPHA_SetLogPath(const char *log_path);

LIBKPHA_API int  __stdcall KPHA_ShutdownDevice(int handle);

LIBKPHA_API int  __stdcall KPHA_Init(int handle,
    char *log_path, char *infile_dir, char *outfile_dir);

LIBKPHA_API int __stdcall KPHA_Init2(int handle, const char *base_path);

LIBKPHA_API int __stdcall KPHA_Is_TEE_Supported(int handle);

LIBKPHA_API int __stdcall KPHA_Check_TEE_State(int handle);

LIBKPHA_API unsigned long  __stdcall Get_KPHA_Version(void);

LIBKPHA_API int  __stdcall KPHA_Check_RPMB(int handle, char *buf, int len);

#ifdef __cplusplus
}
#endif
#endif

封出的接口要用来定义:
int __declspec(dllexport) __stdcall

- C++builer 中动态调用例子

typedef HANDLE  __stdcall MY_KPHA_Init2 (int handle, const char *base_path);
 MY_KPHA_Init2   *KPHA_Init2;
// 用 HANDLE  MY_KPHA_Init2
 typedef HANDLE  __stdcall MY_KPHA_Check_TEE_State (int handle);
 MY_KPHA_Check_TEE_State   *KPHA_Check_TEE_State;
 
bool  CAttestationKey:: Getdllfun(void)

{

	int len,y,r;
	char cwd[250];
	char kph_base_path[256];
	char kphadll_path[256];
	if ((len = GetCurrentDirectory(250, cwd)) < 0) {
		//return ;
	}
	sprintf(kph_base_path, "%s\\tee_stuff", cwd);
	sprintf(kphadll_path, "%s\\LibKPHA.dll", cwd);
	
	LOG("cwd = %s  kph_base_path = %s \n",cwd,kph_base_path);
	LOG("kphadll_path = %s \n",kphadll_path);//指定dll的路径

	LOG("come to .... ------>\n\n");
	int hander = m_META_HANDLE_Obj.Get_MainHandle();

	m_hlibkphaDll = LoadLibrary(kphadll_path); //用LoadLibrary() 来load dll

	if ( NULL == m_hlibkphaDll)
	  {
	  	LOG("load dll error ------>\n\n");
		return false;
	  }

	LOG("come to GetProcAddress .... ------>\n\n");
	
	KPHA_Check_TEE_State = (MY_KPHA_Check_TEE_State *)GetProcAddress(m_hlibkphaDll,"KPHA_Check_TEE_State");//GetProcAddress()获函数的地址
	y = (int)KPHA_Check_TEE_State(hander);
    LOG("load funtion KPHA_Check_TEE_State ok  y = %d------>\n\n",y);
	
	KPHA_Init2 = (MY_KPHA_Init2 *)GetProcAddress(m_hlibkphaDll,"KPHA_Init2"); //强转为指针
	r = (int)KPHA_Init2(hander ,(const char*)kph_base_path);// 调用 函数,但是现在函数是 个指针,强转为 原来的int型
	LOG("load funtion KPHA_Init2 ok  r = %d------>\n\n",r);
	
		if (r < 0) {
			Confirm(META_FAILED);
			LOG("1");
		}
		else {
			Confirm(META_SUCCESS);
		}
		r = (int)KPHA_Check_TEE_State(hander);
		if (r == 0) {
			Confirm(META_SUCCESS);
			return true;
		}
		else {
			Confirm(META_FAILED);
			LOG("2");
		}	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1)VsDllvs2010工程,编译生成动态库。 2)BCBCall为bcb工程,调用动态库 3)Viewdll.exe为查看DLL导出文件的工具。 注意事项一: VSDll里面有一个 KpHttps.def LIBRARY "KpHttps" EXPORTS InitKpHttp Project > Properties... > Linker > Input > Module Definition File 里面加入你刚才的KpHttps.def文件,重新编译就行了。出来的InitKpHttp函数前面没有下划线,后面没有@8或者@ABCDEF之类的。 注意事项二: 用C++Builder的implib工具生成DLL对应的lib文件。如:implib xx.lib xxx.dll。 生成lib文件之后,C++Builder便可以使用这个lib文件了。 注意事项三: 文件头的声明应该完全一样,否则(呵呵),会出现链接错误。 #ifdef DLL_API #define DLL_API extern "C" _declspec(dllexport) #else #define DLL_API extern "C" _declspec(dllimport) #endif #ifdef __cplusplus extern "C" { #endif DECLDIR int __stdcall InitKpHttp(void); DECLDIR int UnInitKpHttp(void); DECLDIR int KpHttpRequest(char *strurl,char *strhost,char *strresult,int &resultle;); DECLDIR int KpHttpsRequest(char *strurl,char *strhost,char *strresult,int &resultle;); DECLDIR int KpHttpPost(char *strurl,char *strhost,char *strPost,int postlen,char *strresult,int &resultle;); DECLDIR int KpHttpsPost(char *strurl,char *strhost,char *strPost,int postlen,char *strresult,int &resultle;); DECLDIR int KpUpLoadFile(char *strurl,char *strhost,char *filebuf,int filelen,char *strresult,int &resultle;); DECLDIR int KpDownLoadFile(char *strurl,char *strhost,char *filebuf,int &filelen;,char *strresult,int &resultle;); #ifdef __cplusplus } #endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值