線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1306
推到 Plurk!
推到 Facebook!

dll的傳值問題的應用

尚未結案
snoopyet0909
一般會員


發表:2
回覆:6
積分:1
註冊:2005-06-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-09 15:29:23 IP:218.162.xxx.xxx 未訂閱
以下是程式部份: 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; function test(i:string):string; stdcall; var Fa:TForm1; implementation {$R *.dfm} function test(i:string):string; begin Fa:=TForm1.Create(Application); Fa.ShowModal; result:=test(i); end; procedure TForm1.Button1Click(Sender: TObject); var a:integer; begin a:=strtoint(edit1.Text); end; end. ------------------------------------------------------------- unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function test():string; stdcall; external'project2.dll'; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin label1.Caption:=test(); end; end. ------------------------------------------------------------- library Project2; { 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' {Form1}; {$R *.res} exports test; begin end. ------------------------------------------------------------- 問題 1.以上三個程式,可不可以麻煩妳們告訴我哪一個是主程式? 因為我怎麼看都看不出來! 2.上面的程式是屬於不完整的程式 因為執行結果是這樣 按下按鈕後會出現一個表單 然後輸入數值 接下來所輸入的數值會傳回原本的表單 而那個突然跑出來的表單會不見 可是呀 我不管怎麼寫 都不會出來 所以想麻煩大家幫幫我的忙!! 先在這邊向各位說聲謝謝.....!!
supman
尊榮會員


發表:29
回覆:770
積分:924
註冊:2002-04-22

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-09 15:41:01 IP:61.70.xxx.xxx 未訂閱
您好: Unit1是主程式,Unit2是DLL 請參考我以下這篇 http://delphi.ktop.com.tw/topic.php?topic_id=69648 或可以搜尋站內"dll"有很多資料.
snoopyet0909
一般會員


發表:2
回覆:6
積分:1
註冊:2005-06-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-09 23:38:51 IP:220.141.xxx.xxx 未訂閱
謝謝你的回覆 由於我算是Delphi的初級使用者 所以可能會看不大懂 可是 能否告訴我 我PO在上面的那個程式哪裡有錯 或者應該怎麼改好嗎 先在這邊說聲謝謝
jest0024
高階會員


發表:11
回覆:310
積分:224
註冊:2002-11-24

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-10 00:17:01 IP:59.104.xxx.xxx 未訂閱
引言: 以下是程式部份: unit Unit1; //<-- 單元名稱: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; function test(i:string):string; stdcall; var Fa:TForm1; implementation {$R *.dfm} function test(i:string):string; begin Fa:=TForm1.Create(Application); //<--創建後,沒釋放 Fa.ShowModal; result:=test(i); //<--怎一直呼叫自己的函數呢 end; procedure TForm1.Button1Click(Sender: TObject); //<--完全不懂這是做什麼用的 var a:integer; begin a:=strtoint(edit1.Text); end; end. ------------------------------------------------------------- unit Unit2;//<-- 單元名稱:Unit1,這是主程式的單元檔案 interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function test():string; stdcall; external'project2.dll'; //<--導入Project2.dll檔的test函數 var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin label1.Caption:=test(); end; end. ------------------------------------------------------------- library Project2; //<--DLL專案程式 { 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' {Form1}; //<--包函了某個單元 {$R *.res} exports test; //<--輸出的DLL函數 begin end. ------------------------------------------------------------- 問題 1.以上三個程式,可不可以麻煩妳們告訴我哪一個是主程式? 因為我怎麼看都看不出來! 2.上面的程式是屬於不完整的程式 因為執行結果是這樣 按下按鈕後會出現一個表單 然後輸入數值 接下來所輸入的數值會傳回原本的表單 而那個突然跑出來的表單會不見 可是呀 我不管怎麼寫 都不會出來 所以想麻煩大家幫幫我的忙!! 先在這邊向各位說聲謝謝.....!!
發表人 - jest0024 於 2005/06/10 00:20:45
snoopyet0909
一般會員


發表:2
回覆:6
積分:1
註冊:2005-06-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-06-10 17:20:12 IP:218.162.xxx.xxx 未訂閱
根據上述 我忘記告訴大家了 執行過程: 1.表單一上有一個button1和一個Label1. 按下button1後,會出現表單二. 2.表單二上有一個button2和一個edit1. 將數值輸入到edit1後,按下button2後 數值就會傳到表單一的Label1上, 並且表單二會自動關閉. 所以可不可以麻煩大家 告訴我如何修改上面我所弄上去的程式呢!! 先在這邊說聲謝謝..
jest0024
高階會員


發表:11
回覆:310
積分:224
註冊:2002-11-24

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-06-10 19:56:04 IP:59.104.xxx.xxx 未訂閱
Dll Function
function Test:PChar;stdcall;
var
  Form:TForm1;
  S   :PChar;
begin
  Form:=TForm1.Create(nil);
  if Form.ShowModal=mrOk then S:=PChar(Form.Edit1.Text)
                         else S:='';
  Form.Free;
  Result:=S;
end;    Exe Function
function test:PChar;stdcall; external'project2.dll';    
snoopyet0909
一般會員


發表:2
回覆:6
積分:1
註冊:2005-06-09

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-06-10 22:36:44 IP:220.140.xxx.xxx 未訂閱
根據你給的程式 請問"mrOk"這個是啥米?
jest0024
高階會員


發表:11
回覆:310
積分:224
註冊:2002-11-24

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-06-10 23:50:15 IP:59.104.xxx.xxx 未訂閱
引言: 根據你給的程式 請問"mrOk"這個是啥米?
ShowModal回傳值!! mrOk代表按下OK鍵,另外還有mrOk,mrCancel,mrAbort,mrRetry,mrIgnore等等...請參考TModalResult 發表人 - jest0024 於 2005/06/10 23:51:25
snoopyet0909
一般會員


發表:2
回覆:6
積分:1
註冊:2005-06-09

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-06-11 09:41:06 IP:220.140.xxx.xxx 未訂閱
謝謝你 我成功了
系統時間:2024-04-26 15:36:04
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!