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

dll如何得到主form的參數

答題得分者是:speedup
shyu_ting
一般會員


發表:13
回覆:25
積分:12
註冊:2002-11-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-01 17:26:59 IP:210.68.xxx.xxx 未訂閱
各位先進: 因為userid及password在主程式啟動(onshow)時鍵入,但當我要呼叫一個dll 時,如何將userid傳入dll,或者說當一個dll被叫用時如何取得主form 的參數,這樣描素不曉得各位先進能否了解.
ha0009
版主


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-01 18:02:25 IP:61.30.xxx.xxx 未訂閱
你好:     只要宣告就行了呀,範例參考看看。    
DLL 內的程序

function SayHello (UserID : String) : boolean; stdcall; export;    implementation    function SayHello (UserID : String) : boolean; stdcall; export;
begin
  MessageBox (0, PChar (format ('%s 你好久沒來了囁。', [UserID])), '測試對話框', 0);
  result := true;
end;        MainForm 內的程序    function SayHello (UserID : String) : boolean; stdcall;    implementation    function SayHello; stdcall; external 'TimerDLL.dll';    procedure TForm1.Button2Click(Sender: TObject);
begin
  if SayHello ('沉默羔羊') then
    ShowMessage ('呼叫成功')
  else
    ShowMessage ('呼叫失敗');
end;    
shyu_ting
一般會員


發表:13
回覆:25
積分:12
註冊:2002-11-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-02 10:26:06 IP:210.68.xxx.xxx 未訂閱
引言: 你好: 只要宣告就行了呀,範例參考看看。
DLL 內的程序

function SayHello (UserID : String) : boolean; stdcall; export;    implementation    function SayHello (UserID : String) : boolean; stdcall; export;
begin
  MessageBox (0, PChar (format ('%s 你好久沒來了囁。', [UserID])), '測試對話框', 0);
  result := true;
end;        MainForm 內的程序    function SayHello (UserID : String) : boolean; stdcall;    implementation    function SayHello; stdcall; external 'TimerDLL.dll';    procedure TForm1.Button2Click(Sender: TObject);
begin
  if SayHello ('沉默羔羊') then
    ShowMessage ('呼叫成功')
  else
    ShowMessage ('呼叫失敗');
end;    
感謝ha0009的回應,可能是我表達得不清楚,我再試試說清楚點, 我用了一個mainform,裡面treeview的各個node是動態載入的 當mouse雙撃該node時,動態去載入該node指定的dll檔,並將 控制權交給該dll檔,問題發生在userid及password是在mainform 鍵入的,當控制權交給dll時該怎麼把userid值傳給dll,而且每次 點選的dll都不會是同一個,但不管哪個dll都必須得到mainform 的userid值,希望各位先進能提供方法.謝謝
shyu_ting
一般會員


發表:13
回覆:25
積分:12
註冊:2002-11-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-09-02 10:55:30 IP:210.68.xxx.xxx 未訂閱
補充一下,我的dll是用以下方式動態載入,好像應該從這下手,但想不透, 請各位幫幫忙,又mainform和dll都是分開開發的,所以該怎麼傳參數實在困 擾著我 > >
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-09-02 11:51:14 IP:210.243.xxx.xxx 未訂閱
引言: 補充一下,我的dll是用以下方式動態載入,好像應該從這下手,但想不透, 請各位幫幫忙,又mainform和dll都是分開開發的,所以該怎麼傳參數實在困 擾著我 > > >< face="Verdana, Arial, Helvetica"> 將IPlugin 介面中的方法改為 procedure ShowModalForm(UserID:WideString); //<--記的用WideString型態千萬不要用String型態 接下來在實作該方法的程式碼中將UserID塞入xxxForm的Public 變數中(看你怎麼定義) 方法大致如此,但我不曉的你的IPlugin 是如何實作,無法給更詳細的程式碼建議 唉~
------
唉~
shyu_ting
一般會員


發表:13
回覆:25
積分:12
註冊:2002-11-04

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-09-02 12:51:31 IP:203.70.xxx.xxx 未訂閱
引言: 將IPlugin 介面中的方法改為 procedure ShowModalForm(UserID:WideString); //<--記的用WideString型態千萬不要用String型態 接下來在實作該方法的程式碼中將UserID塞入xxxForm的Public 變數中(看你怎麼定義) 方法大致如此,但我不曉的你的IPlugin 是如何實作,無法給更詳細的程式碼建議 唉~
感謝您的回應,您提到修改precedure ShowModalForm(UserID:WideString); 已經完成,但要塞入xxxform的public變數就不知如何作了,我把iplugin也放上來,其實這種動態載入dll的方式是參考蔡煥麟前輩的,只是應用上遇到一些問題,還請各位先進不吝指教 interface uses Windows, Messages, SysUtils, Classes, Forms, Controls; type IPlugin = interface ['{D3F4445A-C704-42BC-8283-822541668919}'] // 按 Ctrl Shift G 產生 GUID function CreateForm(hMainForm: THandle): THandle; procedure DestroyForm; procedure ShowModalForm(UserID:WideString); //已修改 end; TCreatePluginFunc = function (hApp: THandle): IPlugin; stdcall; function DllCreatePlugin(hLib, hApp: THandle): IPlugin; implementation resourcestring sErrorLoadingDLL = '無法載入模組!'; sErrorDllProc = '無法呼叫 DLL 函式: %s'; const SDllCreatePluginFuncName = 'CreatePlugin'; function DllCreatePlugin(hLib, hApp: THandle): IPlugin; var pProc: TFarProc; CreatePluginFunc: TCreatePluginFunc; begin Result := nil; if hLib = 0 then Exit; pProc := GetProcAddress(hLib, PChar(SDllCreatePluginFuncName)); if pProc = nil then raise Exception.CreateFmt(sErrorDllProc, [SDllCreatePluginFuncName]); CreatePluginFunc := TCreatePluginFunc(pProc); Result := CreatePluginFunc(hApp); end; end.
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-09-03 14:12:09 IP:210.243.xxx.xxx 未訂閱
引言: 感謝您的回應,您提到修改precedure ShowModalForm(UserID:WideString); 已經完成,但要塞入xxxform的public變數就不知如何作了,我把iplugin也放上來,其實這種動態載入dll的方式是參考蔡煥麟前輩的,只是應用上遇到一些問題,還請各位先進不吝指教 interface uses Windows, Messages, SysUtils, Classes, Forms, Controls; type IPlugin = interface ['{D3F4445A-C704-42BC-8283-822541668919}'] // 按 Ctrl Shift G 產生 GUID function CreateForm(hMainForm: THandle): THandle; procedure DestroyForm; procedure ShowModalForm(UserID:WideString); //已修改 end; ......
你放上去的Source Code 並不是關鍵,我上網努力找了一下蔡煥麟前輩的文章(好多第方多被砍了,不過好文章就是好文章,用搜尋引擎一下就找到了) 若是你採用該文章的架構發展大概要改以下 //原始(在DllExport.pas中) function TPlugin.ShowModalForm: Integer; begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); Result := FForm.ShowModal; end; //改為 procedure ShowModalForm(UserID:WideString); begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); FForm.UserID := UserID; Result := FForm.ShowModal; end; 另外,記的在TBaseForm Class中記的在Public區段加入UserID:WideString 變數,或對上述FForm變數作轉型如 TMyCreateForm(FForm).UserID := UserID;其中,在自己建立的TMyCreateForm中同樣必須的在Public區段加入UserID:WideString 變數 最後,提醒一下,在實作的TMyCreateForm中,使用UserID 的時機原則上在OnShowForm事件之中或之後,在OnCreate事件尚未收到 (ps TMyCreateForm指的是你的程式中用來實作的Form類別,實際上叫啥名稱我也不知道) 唉~
------
唉~
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-09-03 14:12:27 IP:210.243.xxx.xxx 未訂閱
引言: 感謝您的回應,您提到修改precedure ShowModalForm(UserID:WideString); 已經完成,但要塞入xxxform的public變數就不知如何作了,我把iplugin也放上來,其實這種動態載入dll的方式是參考蔡煥麟前輩的,只是應用上遇到一些問題,還請各位先進不吝指教 interface uses Windows, Messages, SysUtils, Classes, Forms, Controls; type IPlugin = interface ['{D3F4445A-C704-42BC-8283-822541668919}'] // 按 Ctrl Shift G 產生 GUID function CreateForm(hMainForm: THandle): THandle; procedure DestroyForm; procedure ShowModalForm(UserID:WideString); //已修改 end; ......
你放上去的Source Code 並不是關鍵,我上網努力找了一下蔡煥麟前輩的文章(好多第方多被砍了,不過好文章就是好文章,用搜尋引擎一下就找到了) 若是你採用該文章的架構發展大概要改以下 //原始(在DllExport.pas中) function TPlugin.ShowModalForm: Integer; begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); Result := FForm.ShowModal; end; //改為 procedure ShowModalForm(UserID:WideString); begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); FForm.UserID := UserID; Result := FForm.ShowModal; end; 另外,記的在TBaseForm Class中記的在Public區段加入UserID:WideString 變數,或對上述FForm變數作轉型如 TMyCreateForm(FForm).UserID := UserID;其中,在自己建立的TMyCreateForm中同樣必須的在Public區段加入UserID:WideString 變數 最後,提醒一下,在實作的TMyCreateForm中,使用UserID 的時機原則上在OnShowForm事件之中或之後,在OnCreate事件尚未收到 (ps TMyCreateForm指的是你的程式中用來實作的Form類別,實際上叫啥名稱我也不知道) 唉~
------
唉~
shyu_ting
一般會員


發表:13
回覆:25
積分:12
註冊:2002-11-04

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-09-03 16:40:35 IP:203.70.xxx.xxx 未訂閱
引言: ..... //原始(在DllExport.pas中) function TPlugin.ShowModalForm: Integer; begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); Result := FForm.ShowModal; end; //改為 procedure ShowModalForm(UserID:WideString); //需要result是否應該使用function begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); FForm.UserID := UserID; //這行compile時會出現FForm未定義userid,但我在TBaseForm Class中有定義 Result := FForm.ShowModal; end; 另外,記的在TBaseForm Class中記的在Public區段加入UserID:WideString 變數,或對上述FForm變數作轉型如 TMyCreateForm(FForm).UserID := UserID;其中,在自己建立的TMyCreateForm中同樣必須的在Public區段加入UserID:WideString 變數 最後,提醒一下,在實作的TMyCreateForm中,使用UserID 的時機原則上在OnShowForm事件之中或之後,在OnCreate事件尚未收到 (ps TMyCreateForm指的是你的程式中用來實作的Form類別,實際上叫啥名稱我也不知道) 唉~
非常感謝speedup兄的回應,但問題仍未解決,上面紅色標示的地方會出錯,還期待您指示,小弟還是新手,有些關於OO的觀念還不是很清楚,謝謝各位的幫忙.
shyu_ting
一般會員


發表:13
回覆:25
積分:12
註冊:2002-11-04

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-09-04 09:54:03 IP:210.68.xxx.xxx 未訂閱
引言: ..... //speedup兄所改的 procedure ShowModalForm(UserID:WideString); begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); FForm.UserID := UserID; Result := FForm.ShowModal; end; //改為 procedure TPlugin .ShowModalForm(userid:widestring); begin if FForm = nil then raise Exception.Create('DllExoprt: 視窗尚未建立!'); user_id:=userid; FForm.ShowModal; end;
有了speedup兄的提示,加上同事的幫忙,以speedup兄所提示的內容加以修改 (上面紅色的部份),終於達到我的需求了,再次感謝speedup兄的協助
yuhboy
一般會員


發表:12
回覆:27
積分:12
註冊:2004-11-01

發送簡訊給我
#11 引用回覆 回覆 發表時間:2008-11-30 01:28:31 IP:125.224.xxx.xxx 訂閱
以下是自己最近使用的心得,不曉得你適不適合用
可以將參數傳進另一個Dll 的 form中
Dll 檔-----
[code delphi]
library ShowForm;
//...
exports ShowFormClass;

//在Dll中再新增一個 Form 內容-----
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
FUserID : string;
public
{ Public declarations }
Property UserID : string Read FUserID Write FUserID;
end;
procedure ShowFormClass(UserID : string);export;stdcall;
implementation
{$R *.dfm}
procedure ShowFormClass(UserID : string);export;stdcall;
var
Form1: TForm1;
begin
try
Form1 := TForm1.Create(nil);
Form1.UserID := UserID;
Form1.ShowModal;
finally
freeandnil(Form1);
end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
Label1.Caption := FUserID;
end;
end.
[/code]

呼叫Dll的程式為-----

[code delphi]
procedure TForm1.Button1Click(Sender: TObject);
type
TShowForm = procedure(UserID : string);stdcall;
var
ShowForm1 : TShowForm;
LibHandle : Cardinal;
begin
LibHandle := LoadLibrary('ShowForm.dll');
if LibHandle <> 0 then begin
@ShowForm1 := GetProcAddress(LibHandle , 'ShowFormClass');
ShowForm1('0003');
FreeLibrary(LibHandle);
end;
ShowForm1 := nil;
end;

[/code]
系統時間:2024-04-27 5:47:01
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!