C++ Builder 动态 DLL调用
时间:2011-12-06
来源:互联网
原来一直用DELPHI,对C/C++不熟。有如下问题,我用DELPHI写了一个DLL要用CB调用,
DLL中函数 声明是 function CreatMessageConnection(IP: string; Port: integer; CallForm: THandle): Boolean;stdcall;
用CB生成HPP 函数声明是extern PACKAGE bool __stdcall CreatMessageConnection(AnsiString IP, int Port, unsigned CallForm);
我现在想用 Library动态载入DLL,并执行CreatMessageConnection,如下代码中我的函数说明应该如何添加,谢谢大家,最好能给我讲讲原因!谢谢
HINSTANCE xx;
xx = LoadLibrary(RoDllName.c_str());
RO_DLL_HINSTANCE = xx;
if (int(xx)!=0) {
try
{
CM = GetProcAddress(xx,"CreatMessageConnection");
}
catch(Exception &e)
{
}
} else
{
}
DLL中函数 声明是 function CreatMessageConnection(IP: string; Port: integer; CallForm: THandle): Boolean;stdcall;
用CB生成HPP 函数声明是extern PACKAGE bool __stdcall CreatMessageConnection(AnsiString IP, int Port, unsigned CallForm);
我现在想用 Library动态载入DLL,并执行CreatMessageConnection,如下代码中我的函数说明应该如何添加,谢谢大家,最好能给我讲讲原因!谢谢
HINSTANCE xx;
xx = LoadLibrary(RoDllName.c_str());
RO_DLL_HINSTANCE = xx;
if (int(xx)!=0) {
try
{
CM = GetProcAddress(xx,"CreatMessageConnection");
}
catch(Exception &e)
{
}
} else
{
}
作者: liuhengwinner 发布时间: 2011-12-06
首先,从兼容性的方面考虑,我是不建议DLL的输出函数中使用String作为参数类型的,尽量用标准数据类型。
其次,就目前这个情况来说,可以这样调用:
C/C++ code
最后,貌似你的函数名字中少了一个e字?
其次,就目前这个情况来说,可以这样调用:
C/C++ code
typedef bool (__stdcall *CREATEMESSAGECONNECTION)(String IP, int Port, HANDLE CallForm); HINSTANCE h = ::LoadLibrary(RoDllName.c_str()); if (h) { CREATEMESSAGECONNECTION CreateMessageConnection = (CREATEMESSAGECONNECTION) ::GetProcAddress(h, "CreatMessageConnection"); if (CreateMessageConnection) { CreateMessageConnection("ip", 端口, 句柄); } else { // 获取函数地址失败 } ::FreeLibrary(h); } else { // 装载DLL失败 }
最后,貌似你的函数名字中少了一个e字?
作者: ccrun 发布时间: 2011-12-06
建议delphi中string改为pchar。
function CreatMessageConnection(IP: pchar; Port: integer; CallForm: THandle): Boolean;stdcall;
C/C++ code
function CreatMessageConnection(IP: pchar; Port: integer; CallForm: THandle): Boolean;stdcall;
C/C++ code
typedef bool __stdcall CreatMessageConnection (char *IP,int Port,THandle CallForm); CreatMessageConnection *pCreatMessageConnection=(CreatMessageConnection *)GetProcAddress(DLLHandle,"CreatMessageConnection"); if( pCreatMessageConnection!= NULL ) { if( pCreatMessageConnection("ip",0,Handle) != 0 ) { ShowMessage("OK");// } else { ShowMessage("error"); } } else { ShowMessage("pCreatMessageConnection load error"); }
作者: ksrsoft 发布时间: 2011-12-06

作者: ccrun 发布时间: 2011-12-06
妖哥就是快啊
作者: ksrsoft 发布时间: 2011-12-06
typedef bool __stdcall (*CreatMessageConnection)(AnsiString , int , void *);//函数指针声明
HMODULE dllModule=LoadLibrary(dllname);//加载
if(dllModule){//加载成功
CreatMessageConnection myfunc=(CreatMessageConnection )GetProcAddress(dllModule,"CreatMessageConnection");
if(myfunc)
{
myfunc(....);//函数调用
}
FreeLibrary(dllModule);
}
HMODULE dllModule=LoadLibrary(dllname);//加载
if(dllModule){//加载成功
CreatMessageConnection myfunc=(CreatMessageConnection )GetProcAddress(dllModule,"CreatMessageConnection");
if(myfunc)
{
myfunc(....);//函数调用
}
FreeLibrary(dllModule);
}
作者: bigfog 发布时间: 2011-12-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28