visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Delphi :: Loadind a DLL in Delphi is giving me an Access Denied Error


By: ramoreia Spain  Date: 10/06/2003 00:00:00  English  Points: 125 Status: Answered
Quality : Excellent
I´m stuck on a code that i can´t fix it, please anyone help me. Here´s the code

Handle := LoadLibrary(NameDLL);
if (Handle > Int64(-1)) and (Handle <= 32) then
begin
MsgErro('ERRO','An error occured:'+#13#10+
SysErrorMessage(GetLastError),True);
Handle := 0;

first i try to loda the library. The Var "NameDLL" gives the the whole path on wich the dll is. Example: 'C:\BIS\Clients' . The dll name is Clients.dll but the path doesn´t have to have the extension to load it i guess. Is this code wrong and what´s the easiest way. Thanx !!!
By: VGR Date: 10/06/2003 04:48:00 English  Type : Answer
Thank Dr Bob ;-)

1) Implicit Linking
Implicit linking happens when you Use an import unit for the DLL or declare a DLL function in your code with external 'DLLNAME'. In this case the linker will place references to the DLL functions into the imported name table of the produced EXE file and the Windows loader will automatically load the DLLs (at which time their main block is executed) with the EXE file:

procedure Foo(X: Integer); external 'BAR' index 1;

If during an implicit link (also called "static link") Windows cannot find the DLL, you will get a notification - most often a File Error MessageBox (one that does not indicate which file is missing, by the way - most helpful!). So you must be absolutely sure that the DLL will be available at all times when you want to use implicit import units.

2) Explicit Linking
Dynamically linking a DLL using an explicit import unit is the method VB uses and which you can also use, but "by hand". You load the DLL when you need it with LoadLibrary, obtain function pointers to the exported functions you want to use with GetProcAddress and free the DLL again with FreeLibrary when you no longer need it:

var
Hbar: Thandle;
Foo: procedure (X: Integer); {$IFDEF WIN32} stdcall; {$ENDIF}
begin
Hbar := LoadLibrary('BAR.DLL');
if Hbar >= 32 then { success }
begin
Foo := GetProcAddress(HBar, 'FOO');
...
Foo(1);
...
FreeLibrary(HBar);
end
else
MessageDlg('Error: could not find BAR.DLL', mtError, [mbOk], 0)
end.

Note that when we are using an explicit link (also called "dynamic link"), we can detect whether or not the LoadLibrary failed, and whether or not the DLL is actually loaded. If the DLL is not loaded, the program can continue (and just disable the functions or options that needed the DLL in the first place). This is also one way to write components that need DLLs, so your Delphi Component Library or Package will still load if the DLL is not present.
By: Workshop_Alex Date: 10/06/2003 17:00:00 English  Type : Comment
<A HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp</a>

It is a common mistake that values 1 to 31 also mean that LoadLibrary failed. However, according to MSDN, LoadLibrary only returns 0 if it failed to load a DLL. So just check if LoadLibrary('BAR.DLL') = 0 or not.

I know, it's a common mistake...

And the result value is NOT an Int64 but a normal THandle, which is only 4 bytes, not 8.

And you do need to add the .DLL extension to the DLL name.

Do register to be able to answer

EContact
browser fav
page generated in 270.884040 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page