全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2270
推到 Plurk!
推到 Facebook!

簡易的Dll檔案製作--新手適用

 
juneo
高階會員


發表:103
回覆:190
積分:118
註冊:2004-05-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-05-26 16:31:24 IP:211.75.xxx.xxx 未訂閱
剛入門delphi 因為在這學到不少,所以把這幾天用delphi寫dll的心得寫出來分享一下,希望新手如果跟我一樣有這方面的需要,這份文件能夠幫上忙 不知道大家寫程式比較喜歡一各檔案重頭寫到尾,還是切割成很多檔案分開寫 廢話不多說了以下是dll檔的撰寫,也歡迎高手指教一下 這篇文章很簡單但是卻可以讓新手釐清DLL的運作模式 --------------------------------------------------------------------- 程式目的;製作DLL檔讓主程式呼叫後經過DLL處理再把函數回傳 總共檔案 1.Project1.dpr 2.Unit1.pas 3.inttohex.dpr --------------------------------------------------------------------- dll檔案部分 1.Project1.dpr { 程式名稱:project1 程式目的:產生DLL檔 產生日期:2004/0526 作者: jongwaye } library Project1; { Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. } uses SysUtils, Classes, Unit1 in 'Unit1.pas'; {$R *.res} exports intetohex; //設定要輸出的函數 begin end. --------------------------------------------------------------------- 2.Unit1.pas { 程式名稱:unit1.pas 程式目的:測試Dll傳入數值,改變後再傳回原來的程式 產生日期:2004/0526 作者: jongwaye } unit Unit1; interface uses Dialogs; function intetohex(i,j:string):string;export; //設定函數 implementation function intetohex(i,j:string):string; //開始寫函數需要處理的事項 begin j := j '8'; //把J 上文字(string)8 Result := j; //把j傳回呼叫的檔案 showmessage('這是DLL改過的內容:' 'i=' i ',' 'j=' j); //顯示畫面表示DLL有作用 end; end. --------------------------------------------------------------------- 3.inttohex.dpr (主程式) { 程式名稱:testinttohex 程式目的:呼叫 DLL 產生日期:2004/0526 作者: jongwaye } unit testinttohex; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Button1: TButton; Button2: TButton; Label1: TLabel; Label2: TLabel; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; function intetohex(i,j:string):string; far;external 'Project1.dll'; //設定要呼叫的函數名稱 implementation {$R *.dfm} procedure TForm1.Button2Click(Sender: TObject); begin close; //離開按鈕 end; procedure TForm1.Button1Click(Sender: TObject); var i,j,a : string; //設定變數 i,j a=取回Dll後的暫存變數 x ,y ,z :integer; //這些變數目的是做文字轉數字暫存 begin i := Edit1.Text; j := Edit2.Text; showmessage('i=' i ',' 'j=' j); //顯示輸入的文字 a := intetohex(i,j); //dll加工後將變數j傳回給a // showmessage('j=' j); //測試傳遞變數是否有成功用 // showmessage('a=' a); x := strtoint(i); //把文字的資料轉成整數 y := strtoint(a); z := x y; //計算兩變數的總計 showmessage('總計=' (inttostr(z))); //顯示總合,並且轉換為文字 在輸出 end; end. ---------------------------------------------------------------------
ha0009
版主


發表:16
回覆:507
積分:639
註冊:2002-03-16

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-09-06 11:52:21 IP:219.80.xxx.xxx 未訂閱
[cde] 你好: 你的範例解說的不錯,但 DLL 中的傳回值如果為字串應該使用指標的方式才不會在傳遞途中產生錯誤喔(見下面宣告),不然就是 uses ShareMem 。給你參考 > < href="http://SourceProvide.deepen.com.tw/k_top/測試 DLL.exe">http://SourceProvide.deepen.com.tw/k_top/測試 DLL.exe [/code]
juneo
高階會員


發表:103
回覆:190
積分:118
註冊:2004-05-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-09-06 12:03:15 IP:211.20.xxx.xxx 未訂閱
感謝h0009大大提醒    關於DLL使用String字串傳遞會造成一些問題 我與h0009大大有一篇討論到這方面的資訊 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=55744    這樣DLL需要如何傳遞就更清楚 分享比獲得更快樂--
系統時間:2024-04-19 5:40:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!