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");
}
}