Solved

Problem with LoadLibrary

Posted on 2000-04-25
15
2,269 Views
Last Modified: 2012-08-14
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
Comment
Question by:mhoughton
15 Comments
 
LVL 3

Expert Comment

by:f15iaf
ID: 2746896
check if a dll file exists on other computer and located in "c:\windows\system" directory.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2746963
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
 
LVL 1

Expert Comment

by:mscatena
ID: 2746982
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
 

Author Comment

by:mhoughton
ID: 2746985
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
 

Expert Comment

by:mike_shay
ID: 2746991
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
 

Author Comment

by:mhoughton
ID: 2746994
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2746997
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:Lischke
ID: 2747002
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
 

Author Comment

by:mhoughton
ID: 2747028
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
 

Author Comment

by:mhoughton
ID: 2747091
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2747118
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
 

Author Comment

by:mhoughton
ID: 2747208
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
 
LVL 10

Accepted Solution

by:
Lischke earned 200 total points
ID: 2747249
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2747416
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
 

Author Comment

by:mhoughton
ID: 2747427
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now