製作DLL

開啟Delphi

File>New>Other>找到 "Dynamic-link Libray">OK

可以直接在專案檔裡面寫 也可以新開一個unit 分開寫

以下是新開一個unit分開寫的範例

專案檔

library Project1;
 
uses
  SysUtils,
  Classes,
  Unit1 in 'Unit1.pas';
 
{$R *.res}
 
exports MyAdd;
 
begin
end.
 
單元檔
 
unit Unit1;
 
interface
 
function MyAdd(i,j: Integer):Integer; stdcall;
 
implementation
 
function MyAdd(i,j: Integer):Integer;
begin
  MyAdd:= i + j;
end;
 
end.
 
製作完成之後
 
右上方專案視窗 project1.dll 右鍵 build 即可生成一個project1.dll檔案
 

使用DLL檔
 
以下範例是"靜態引用DLL" 即 程式一執行就會去偵測有沒有這個DLL 若沒有會報錯
 
以後有空在新增 "動態引用DLL"的方法
 
引用DLL
function MyAdd(i,j: Integer):Integer; stdcall; external 'Project1.dll';
 
使用DLL
Caption:= IntToStr(MyAdd(1,2));
arrow
arrow
    文章標籤
    Delphi DLL
    全站熱搜
    創作者介紹
    創作者 抓狂小白 的頭像
    抓狂小白

    抓狂小白的程式筆記

    抓狂小白 發表在 痞客邦 留言(0) 人氣()