[問題] DLL 在freelibrary 出現錯誤 |
答題得分者是:pceyes
|
Oistrakh
一般會員 發表:4 回覆:3 積分:1 註冊:2006-07-19 發送簡訊給我 |
Dear all :
小弟寫了一個簡單的程式,在Program的FreeLibrary(handle);發生錯誤, 錯誤是 many consecutive exceptions 請問是怎樣的原因呢 又該如何解決? 我有夾帶檔案 DLL如下 ////DLL Begin library TestDLL; uses ShareMem, SysUtils, Classes, Unit1 in 'Unit1.pas'; {$R *.res} exports AddInt; begin end. ////DLL END 以下是程式碼的部份 ////Program Begin unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type //THandle = integer; TAddInt = function(a: integer; b: integer): integer; stdcall; TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit; Label2: TLabel; Label3: TLabel; Edit2: TEdit; Button1: TButton; Edit3: TEdit; procedure Button1Click(Sender: TObject); procedure Edit1Enter(Sender: TObject); procedure Edit2Enter(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var itemp: integer; Myfunc: TAddInt; handle: THandle; fPointer: TFarProc; begin if (not tryStrToInt(Edit1.Text,itemp)) or (not tryStrToInt(Edit2.Text,itemp)) then begin ShowMessage('加數與被加數請輸入整數!!'); Edit1.SetFocus; Exit; end; handle:= LoadLibrary('TestDLL'); if handle <> 0 then try fPointer:= GetProcAddress(handle,'AddInt'); if fPointer <> nil then begin Myfunc:= TAddInt(fPointer); itemp:= 0; itemp:= Myfunc(StrToInt(Edit1.Text),StrToInt(Edit2.Text)); Edit3.Text:= IntToStr(itemp); end; finally FreeLibrary(handle); end else ShowMessage('TestDll.dll is not found!!'); end; procedure TForm1.Edit1Enter(Sender: TObject); begin Edit3.Clear; end; procedure TForm1.Edit2Enter(Sender: TObject); begin Edit3.Clear; end; end. ///program End |
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
你 DLL 的使用方式哪學的?這招「脫褲放屁」,還是不要用的好
@Myfunc:= GetProcAddress(handle,'AddInt'); 這樣不是很好嗎? 有空多看 Help @StrComp := GetProcAddress(KernelHandle, 'lstrcmpi'); calls the GetProcAddress function and points StrComp to the result. Any procedural variable can hold the value nil, which means that it points to nothing. But attempting to call a nil-valued procedural variable is an error. To test whether a procedural variable is assigned, use the standard function Assigned: if Assigned(OnClick) then OnClick(X); 另外 not tryStrToInt(Edit1.Text,itemp) 也可以省省,直接用一個 try 去處理,並馬上轉成 int 就不用一個簡單的目標,要重複呼叫 StrToInt 去浪費時間 剩下的,還要請你自己多加由囉! |
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |