说明:
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);}