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

[問題] DLL 在freelibrary 出現錯誤

答題得分者是:pceyes
Oistrakh
一般會員


發表:4
回覆:3
積分:1
註冊:2006-07-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-05 13:55:10 IP:61.58.xxx.xxx 訂閱
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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-11-06 23:58:46 IP:61.64.xxx.xxx 訂閱
你 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

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