Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2395
  • Last Modified:

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.
0
mhoughton
Asked:
mhoughton
1 Solution
 
f15iafCommented:
check if a dll file exists on other computer and located in "c:\windows\system" directory.
0
 
LischkeCommented:
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
0
 
mscatenaCommented:
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".
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mhoughtonAuthor Commented:
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.
0
 
mike_shayCommented:
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.
0
 
mhoughtonAuthor Commented:
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.
0
 
LischkeCommented:
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
0
 
LischkeCommented:
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
0
 
mhoughtonAuthor Commented:
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!
0
 
mhoughtonAuthor Commented:
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.
0
 
LischkeCommented:
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
0
 
mhoughtonAuthor Commented:
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?
0
 
LischkeCommented:
You can only accept a comment in this question and open a dummy question for the other guy or you delete this question and open two dummy questions.

I'd prefer the first, although it will cost you more points, because this will save the discussion from this question.

BTW: Who will you award additional points? I think I'm the only one/first one who gave useful information. The missing DLL extension was mentioned first by me and Michael's contribution was ... mmh ... a bit too general.

Ciao, Mike
0
 
kretzschmarCommented:
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 ;-)
0
 
mhoughtonAuthor Commented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now