Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FreeLibrary Problem!! Need Help

103 views
Skip to first unread message

simon carrier

unread,
Jun 5, 2002, 2:21:59 PM6/5/02
to
Hi, i'm trying to dynamicaly load a dll by my application. I've tried some
methods that always turn to an access violation. The access violation his
raised on the call to the freeLibrary function. If i don't call the
Freelibrary, there's no problem, if we forget the memory managing. So i
think it might be useful to call this function(FreeLibrary). What's the
problem!!!

I'm using delphi4

Could someone help me?!

Here's the code:
procedure tform1.connect;
var
//for the loading of the dll
LibHandle: THandle;
pPath : pchar;
loginDB : function (var AServer, ADatabase, AUser, APassword,
AODBCAlias, ASQLServerType: Pchar; Database : pointer) : Integer; stdcall;
//as it name tell
loginResult : Integer;
// var passed to the dll
AServer, ADatabase, AUser, APassword, AODBCAlias, ASQLServerType : Pchar;
begin
try
try
pPath :=
pchar(extractfilepath(application.exename)+'loginDlg.dll');
LibHandle := LoadLibrary(pPath);
if LibHandle = 0 then
begin
messagedlg('Contact your dealer', mtInformation,[mbOk],0);
exit;
end else
begin
loginDB := GetProcAddress(Libhandle, 'loginDB');
AODBCAlias := pchar(Database.AliasName);
ASQLServerType:= 'SQL Server';
loginResult:= loginDB(AServer,ADatabase,AUser,APassword,
AODBCAlias, ASQLServerType,nil);
//error on logon
if loginResult=-1 then
begin
raise Exception.Create('You must connect to database first
!');
end else if loginResult > -1 then
begin
Database.Params.Add('USER NAME= '+AUser);
Database.Params.Add('PASSWORD='+APassword);
Database.Connected:=true;
end else if loginResult = -2 then
begin
//on cancel button click
Application.ProcessMessages;
Application.Terminate;
end;
end;
except
on exception do raise;
end;
finally
if LibHandle <> 0 then
begin
try
FreeLibrary(LibHandle);
except
on exception do raise;
end;
end;
end;


Lucian

unread,
Jun 5, 2002, 3:42:39 PM6/5/02
to
try this:

LibHandle := LoadLibrary(
pchar(extractfilepath(application.exename)+'loginDlg.dll') );


Lucian


CMad

unread,
Jun 6, 2002, 2:34:42 AM6/6/02
to
Here is a correct example of dynamically loading a DLL (in Delphi 6, 5, 4,
and maybe some older):

procedure LoadDLL;
const
DLLName = 'MyDll.DLL';
type
TMyFunction : function (par1, par2 : Integer); stdcall;
var
HInst : THandle;
FPointer : TFarProc;
MyFunct : TMyFynction (par1, par2 : Integer);
Loginresult : Integer;
begin
HInst := LoadLibrary('THEDLLPATH' + DLLName);
if HInst > 0 then
try
FPointer := GetProcAddress(HInst, 'MyFunction');
if FPointer <> nil then
begin
MyFunct := TMyFunction(FPointer);
Loginresult := MyFunct (a, b); {for example}
end
else MessageDlg('Could not find function', mtError, [mbOK], 0);
finally
FreeLibrary(HInst);
end
else MessageDlg('Could not find library (DLL)', mtError, [mbOK], 0);
end;

CMAD

PS. It would be better if you do what you want with the DLL (in the middle
of the program the begin-end).


"simon carrier" <mano...@sympatico.ca> wrote in message
news:3cfe573c_2@dnews...

0 new messages