您的位置:首页 > 编程语言 > Delphi

C++ 调用 delphi dll

2018-03-07 18:03 489 查看
说明:
delphi 导出请加stdcall
---------------------- Delphi --------------------------library DepDll;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes;

//加法测试
Function TestAdd(p1, p2 : Integer):Integer;stdcall;
begin
Result:=p1+p2;
end;

//字符串输入与输出测试
Function ShowString(s1, s2:PChar; pOut:PChar):Integer;stdcall;
var
strRes:String;
begin
strRes:='this is from delphi s1:'+ StrPas(s1) + ' s2:' + StrPas(s2);
Move(strRes[1], pOut^, Length(strRes));
end;

{$R *.res}

//函数导出
exports
TestAdd,ShowString;

begin
end.
---------------------- C++ ----------------------CString strPath = _T("D:\\Program Files (x86)\\Borland\\Delphi7\\Projects\\DepDll.dll");

HINSTANCE hUKey = LoadLibrary(strPath);
if (hUKey != NULL){
//参数为整型的加法测试
{
int nRes;
typedef int(__stdcall *TADD)(int, int);
TADD pAdd = (TADD)GetProcAddress(hUKey, "TestAdd");
if (pAdd != 0){
nRes = pAdd(100, 123);
}
}

//参数为字符串的字符串测试
{
int nRes;
typedef int(__stdcall *SHOWSTR)(char*, char*, char*);
SHOWSTR pShowStr = (SHOWSTR)GetProcAddress(hUKey, "ShowString");
char* pBuf = new char[512];
memset(pBuf, 0, 512);
if (pShowStr != 0){
nRes = pShowStr("2018 delphi", "param 2", pBuf);
}
delete[] pBuf;
}

FreeLibrary(hUKey);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  delphi