unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; //MB 函數的聲明: function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall; implementation {$R *.dfm} {調用外部 DLL 中的函數,譬如調用系統 user32.dll 中的 MessageBoxA} //function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; // stdcall; external user32 name 'MessageBoxA'; {其中 user32 是 Delphi 定義的常量 'user32.dll',可以直接寫成:} //function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; // stdcall; external 'user32.dll' name 'MessageBoxA'; {name 后面說明函數的真實名字} {external 子句說明單元載入時就加載函數,也就是早綁定;如果晚綁定需要用 LoadLibrary} {stdcall 指令表示參數傳遞是從右到左(Pascal則反之),不通過CPU寄存器傳遞} {4個參數的數據類型可以使用對應的 Delphi 數據類型,譬如:} //function MB(hWnd: LongWord; lpText, lpCaption: PChar; uType: LongWord): Integer; // stdcall; external 'user32.dll' name 'MessageBoxA'; {或者是:} //function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer; // stdcall; external 'user32.dll' name 'MessageBoxA'; {如果函數在此單元聲明后,需要給其他單元調用,需要先在 interface 區聲明:} //function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer; // stdcall; {本例已經這樣做了,如果要測試其他幾種情況,需要先注釋掉它} {然后在 implementation 區,說明函數的來源:} function MB; external 'user32.dll' name 'MessageBoxA'; //調用測試: procedure TForm1.Button1Click(Sender: TObject); var t,b: PChar; begin t := '標題'; b := '內容'; MB(0,b,t,0); end; end.