C++ 调用 delphi dll

说明:

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值