Link to home
Start Free TrialLog in
Avatar of mhoughton
mhoughton

asked on

Problem with LoadLibrary

Has anyone had problems loading a DLL dynamically in Delphi 5?

It works fine on my machine but if I copy it to another, it fails to load.  LoadLibrary returns 0, and if I check GetLastError, that returns 0 too!

There are no exceptions raised.

This is really puzzling me.

Is it that I've got some run-time component that the other machine hasn't?  I would have thought that would raise some kind of error.  If that is likely, what's the best strategy for finding out which one.

I doubt it though, because the main .exe has been compiled by me and that runs fine.  It's just the .dll that won't load.

Thanks.
Avatar of f15iaf
f15iaf

check if a dll file exists on other computer and located in "c:\windows\system" directory.
Well, there can be several reasons. First if of course to check if the DLL exists. Then you need to specify the extension all the time (LoadLibrary('MyDll.DLL');). Don't rely on automatisms. Additionally, if the DLL is not in one of the search pathes (%windir%, %windir%\system, %windir%\system32 etc. or the folder from where the application started) then you need to specify the full path to the DLL too.

Do you run your application on different OSs (say Win98 and WinNT)? If yes on which does it not work?

Ciao, Mike
There are differences between Win95/98 and WinNT/2000 regarding the file extension. In one of them you may no supply the extension, in the other you must.

Always specify the file extension, even if it's  ".DLL".
Avatar of mhoughton

ASKER

It's in the same directory as the EXE and I've tried it with and without the .dll extension.

I've even tried forcing the full path and copying it to windows\system.

My development machine is Win98 v2 and the test machine is Win98 v1.

I'm very puzzled.

Is there any way to get windows to give up what the problem is?  As I said, GetLastError tells me nothing.
I had the same problem with Delphi 4.
Then I tried in DLL to open some connection to database through ODBC. The application was terminated without raising any exception.
You can try to debug the DLL separately, as an EXE, to find a problem.

Sincerely Yours, Michael Shaikevich.
It's in the same directory as the EXE and I've tried it with and without the .dll extension.

I've even tried forcing the full path and copying it to windows\system.

My development machine is Win98 v2 and the test machine is Win98 v1.

I'm very puzzled.

Is there any way to get windows to give up what the problem is?  As I said, GetLastError tells me nothing.
Mmmh, in this case I can only guess. Maybe the DLL initialization fails on one machine? In this case Windows (GetLastError) returns an error code set by the DLL (if I recall correctly). If this error code is not set then 0 is returned.

Another idea: you haven't accidently used a Word variable for the result of LoadLibrary? Quite unlikely, I know, but in the lack of better ideas...

Ciao, Mike
mike_shay,

your behaviour is very unpolite! You proposed an answer (which isn't really one) although you see that we already discussing the problem. Please use comments if you aren't the first one in a discussion.

mhoughton,

don't use your browser's refresh, otherwise your formular data will be resend. There's a link on each page (upper right corner) labeled "Reload Question".

Ciao, Mike
I'll reject the answer for now, because I'm pretty sure it isn't right.

The DLL just isn't loading.  No code is called because the library handle returned is 0.

If I'm wrong, then I'll eat my words and get you to repost.

A lot of the answers so far would cause problems generally, but as I said it works fine on the development machine.  I may be able to test it on another machine later.

As for hitting refresh, yes I realised that about a milisecond after doing it. DOH!
I've just written a little test app with a listbox and a button...

procedure TForm1.Button1Click(Sender: TObject);
var
  LibHandle : LongWord;
begin
  ListBox1.Items.Add('Loading...');

  LibHandle := LoadLibrary('EPRINT_WINDOWS.DLL');

  ListBox1.Items.Add('Handle: '+IntToStr(LibHandle));
  ListBox1.Items.Add('Error: '+IntToStr(GetLastError));

  if LibHandle <> 0 then
  begin
    ListBox1.Items.Add('Freeing...');
    FreeLibrary(LibHandle);
    ListBox1.Items.Add('Done');
  end;
end;

I've put the two files in isolation on a shared drive and run them on each PC.

On my PC it produces:

Loading...
Handle: 20905984
Error: 0
Freeing...
Done

On the other PC it produces:

Loading...
Handle: 0
Error: 1157
Freeing...
Done

So at least I've got it to produce an error code.

1157 is ERROR_DLL_NOT_FOUND.  So I guess that the DLL is statically linking another DLL which it can't find.
Mmmh, this sounds like a good reason. You can check the import table of the DLL with tdump.exe in the Delphi5\Bin folder.

Ciao, Mike
Hurray!  Got it.

It was the dreaded Borland Memory Manager, borlndmm.dll.

I had it (obviously), the other PC didn't.

You'd have thought it would have been a bit more forthcoming with error messages.  Oh well.

Thanks for all the help everyone, particularly Lischke.

Now, how do I distribute these points?
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
hmm, mhoughton,
at least you've solved it mostly byself,
grade the expert which does mostly helped you in this q. not me, of course :-)
meikl ;-)
I think you have been by far the most helpful.

The DLL extension and location were red herrings - although useful information none the less.

But the most useful comment was pointing me towards tdump.  I always forget about this utility.

Anyway, thanks very much everyone, but Lischke gets it.