當前位置:編程學習大全網 - 編程語言 - Delphi中如何建立dll(動態鏈接庫)?

Delphi中如何建立dll(動態鏈接庫)?

好久沒有親自建立DLL了,***同復習壹下。?分三步:

第壹步:建立DLL工程文件

library?pDll;

uses

SysUtils,

Classes,

uSum?in?'uSum.pas';

exports

Sum;

{$R?*.res}

begin

end.

第二部:在工程文件內加入UNIT(uSum.pas),可寫入各種所需要的過程或函數並在工程文件內標記為輸入(exports?Sum;)

unit?uSum;

interface

uses

Windows,Messages,SysUtils;

function?Sum(ss:?String):?Integer;

implementation

function?Sum(ss:?String):?Integer;

var

i,ct,asc:?Integer;

begin

Result?:=?0;

ct?:=?Length(ss); //輸入串的長度

if?ct?>?0?then?begin //ss?=?''?結果為?0?並退出

for?i?:=?1?to?ct?do?begin //檢查每個字符

asc?:=?Ord(ss[i]); ? //asc?為字符的ASCII碼

if?(asc?>?47)?and?(asc?<?58)?then?begin? //是0..9進入

Inc(Result,?asc?-?48); //0的ASCII碼為48

end;

end;

end;

end;

end.

編譯生成文件:pDll.dll

第三部:動態調用(註意把pDll.dll放在同壹目錄或應用程序能找到的地方)

....

procedure?TForm1.Button1Click(Sender:?TObject);

const

DllName?=?'pDll.dll';

var

DllHandle:?THandle;

sm:?Integer;

pt:?Function(sm:String):?Integer;

begin

DllHandle?:=?LoadLibrary(DllName);

try

if?DllHandle?<>?0?then?begin

@pt?:=?GetProcAddress(DllHandle,?'Sum');

if?@pt?<>?nil?then?begin

sm?:=?pt(Memo1.Text);

Edit1.Text?:=?IntToStr(sm);

end?else?Edit1.Text?:=?'';

end;

finally

FreeLibrary(DllHandle);

end;

end;

....

效果圖:

  • 上一篇:迷妳編程如何自由編程?
  • 下一篇:太原理工大學計算機科學與技術學院在哪
  • copyright 2024編程學習大全網