编写库代码。
extern"C"int__declspec(dllexport) hello2(inta, charsb[])
{
printf("%s%d\n", sb, a);
MessageBoxA(NULL, sb, sb, MB_OK);
return 1;
}
Delphi代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{Private declarations }
public
{Public declarations }
end;
TFunc = function(i:Integer; name:PChar):Integer;cdecl;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender:TObject);
var
pfunc :TFunc;
hdl: THAndle;
retval :Integer;
begin
hdl := loadlibrary('d:\\firstdll.dll');
if hdl > 0 then
begin
pfunc := getprocaddress(hdl, 'hello2');
retval := pfunc(1, PChar(Edit1.Text));
MessageBox(Form1.Handle,PAnsiChar(IntToStr(retval)), PAnsiChar(IntToStr(retval)), 0);
end;
freelibrary(hdl);
end;
end.