ورود

View Full Version : مشکل در فراخوانی و آزاد کردن حافظه از Dll



hedi
چهارشنبه 01 خرداد 1387, 14:07 عصر
با سلام
سوال اول من این است که وقتی می خواهم Dll خود را با استفاده از دستور FreeLibrary از حافظه خارج کنم برنامه اصلی را هم می بندد.


procedure TForm1.Button3Click(Sender: TObject);
begin
Try
DLLHandle:=LoadLibrary('Demodll.dll');
if DLLHandle <> 0 then begin
@ShowDemoForm:=getProcAddress(DLLHandle,'ShowDemoF orm');

if addr(ShowDemoForm) <> nil then begin
ShowDemoForm;
end
else
showmessage ('function not exists ...');
end
else
showMessage('Dll Not Found!');

Finally
FreeLibrary(DLLHandle);
End;
end;
سوال دوم این است که من یک Function دارای پارامتر تعریف کردم وقتی آن را به صورت اتوماتیک بار گذاری می کنم و مقدار دهی می کنم درست پارامتر را می فرستد اما وقتی با روش Load Library استفاده می کنم کلمات اجق وجق را ارسال می کند

این Function در DLL
procedure PMenu(St:ShortString);stdcall;
begin
Form1 :=TForm1.Create(nil);
ShowMessage(St);
Form1.Label1.Caption:=St;
Form1.ShowModal;
Form1.Free;
end;

اگر می شود در خصوص سوال دوم برای Dll های پارامتری به روش لود دینامیکی یک نمونه بگذارید متشکرم

vcldeveloper
پنج شنبه 02 خرداد 1387, 16:32 عصر
سوال اول من این است که وقتی می خواهم Dll خود را با استفاده از دستور FreeLibrary از حافظه خارج کنم برنامه اصلی را هم می بندد.
از روی این کدی که گذاشتید چیز خاصی بدست نمیاد. فقط دو نکته:
1- DLLHandle را بصورت Local در داخل خود Procedure تعریف کنید.
2- چرا کد مربوط به LoadLibrary را داخل بلوک try-finally نوشتید؟ در این کد، اگر LoadLibrary مقدار صفر را هم برگرداند (یعنی DLL لود نشه) باز هم FreeLibrary اجرا میشه!


اگر می شود در خصوص سوال دوم برای Dll های پارامتری به روش لود دینامیکی یک نمونه بگذارید
کدی که این تابع را فراخوانی می کند را هم قرار بدید.

دنیای دلفی
پنج شنبه 02 خرداد 1387, 16:55 عصر
نحوه صحيح استفاده از DLL به صورت Dynamic


unit DynaForm;

interface

uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Spin;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
SpinEdit1: TSpinEdit;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

type
TIntFunction = function (I: Integer): Integer; stdcall;

const
DllName = 'Firstdll.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
HInst: THandle;
FPointer: TFarProc;
MyFunct: TIntFunction;
begin
HInst := SafeLoadLibrary (DllName);
if HInst > 0 then
try
FPointer := GetProcAddress (HInst,
PChar (Edit1.Text));
if FPointer <> nil then
begin
MyFunct := TIntFunction (FPointer);
SpinEdit1.Value := MyFunct (SpinEdit1.Value);
end
else
ShowMessage (Edit1.Text + ' DLL function not found');
finally
// with Delphi 7 this call causes a system error,
// which was not found on previous versions of Delphi...
FreeLibrary (HInst);
end
else
ShowMessage (DllName + ' library not found');
end;

end.