//项目1: 在Delphi中的DLL实现
library MyDLL;
uses
SysUtils,
Classes;
{$R *.res}
function GetDateTime(Buffer: PChar): Integer;
begin
StrCopy(Buffer, PChar(FormatDateTime('yyyy.mm.dd hh:mm:ss', Now)));
Result := 123;
end;
exports
GetDateTime;
begin
end.
//项目2: 在C++ Builder中的DLL调用
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
typedef int (__stdcall *_GetDateTime)(char *);
HINSTANCE hDLL;
_GetDateTime GetDateTime;
char sBuf[50]="";
int rs;
hDLL = LoadLibrary("MyDLL.dll");
if (hDLL){
GetDateTime = (_GetDateTime)GetProcAddress(hDLL, "GetDateTime");
if (GetDateTime){
rs = GetDateTime(sBuf);
ShowMessage(Format("函数调用结果: /n日期: %s, 返回值: %d",
ARRAYOFCONST((sBuf, rs))));
}else
MessageBox(Handle, "函数导入失败!", "DLL调用" ,MB_ICONERROR);
}else
MessageBox(Handle, "加载动态链接库失败!", "DLL调用", MB_ICONERROR);
FreeLibrary(hDLL);
}
//---------------------------------------------------------------------------