1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C++ 调用 delphi dll

C++ 调用 delphi dll

时间:2023-03-11 03:35:11

相关推荐

C++ 调用 delphi dll

说明:

delphi 导出请加stdcall

---------------------- Delphi --------------------------

library DepDll;{ Important note about DLL memory management: ShareMem must be thefirst unit in your library's USES clause AND your project's (selectProject-View Source) USES clause if your DLL exports any procedures orfunctions that pass strings as parameters or function results. Thisapplies to all strings passed to and from your DLL--even those thatare nested in records and classes. ShareMem is the interface unit tothe BORLNDMM.DLL shared memory manager, which must be deployed alongwith your DLL. To avoid using BORLNDMM.DLL, pass string informationusing PChar or ShortString parameters. }usesSysUtils,Classes;//加法测试Function TestAdd(p1, p2 : Integer):Integer;stdcall;beginResult:=p1+p2;end;//字符串输入与输出测试Function ShowString(s1, s2:PChar; pOut:PChar):Integer;stdcall;varstrRes:String;beginstrRes:='this is from delphi s1:'+ StrPas(s1) + ' s2:' + StrPas(s2);Move(strRes[1], pOut^, Length(strRes));end;{$R *.res}//函数导出exportsTestAdd,ShowString;beginend.

---------------------- 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(" delphi", "param 2", pBuf);}delete[] pBuf;}FreeLibrary(hUKey);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。