Discussion:
[Mingw-users] Weird problem loading a dll with win32 LoadLibrary() function
Eric Sokolowsky
2004-01-16 16:25:11 UTC
Permalink
I use mingw to compile an application that loads plugins with the Win32
API LoadLibrary() function. I create an installer program with
InnoSetup to be able to install the application on the user's hard
drive. I install the plugin .dll files in the same directory as the
executable and use a batch file to launch the executable. I noticed
that one particular plugin does not load on my laptop, which is running
Windows XP Home edition. However, the same plugin loads normally on my
Windows 2000 Professional desktop machine. On the laptop, the handle
returned from LoadLibrary() is 0, so I used the Win32 API function
SearchPath() to try to find the .dll. This function returns the proper
location for the .dll, but even if I pass in the full path returned from
SearchPath(), the LoadLibrary() function still returns 0 for the handle
to the .dll. Might there be another reason why this .dll is not able to
load? It's very strange because all of the other plugins in the same
directory are loadable on the laptop, and the same .dll is loadable on
the Windows 2000 desktop machine.

I called GetLastError() after the failed call to LoadLibrary() and it
said: "Error 487: Attempt to access invalid address." What does this
mean?
--
____ __ Eric Sokolowsky (GST) NASA Goddard Space Flight Center
/ __/__/_/__ Visualization Programmer Scientific Visualization Studio
/ __/ _/ / _/ 301.286.3751 Mailstop 935.0 Bldg 28 Rm E102
/___/_//_/__/ ***@gsfc.nasa.gov Greenbelt, MD 20771
Greg Chicares
2004-01-16 16:58:06 UTC
Permalink
Post by Eric Sokolowsky
I use mingw to compile an application that loads plugins with the Win32
API LoadLibrary() function. I create an installer program with
InnoSetup to be able to install the application on the user's hard
drive. I install the plugin .dll files in the same directory as the
executable and use a batch file to launch the executable. I noticed
that one particular plugin does not load on my laptop, which is running
Windows XP Home edition. However, the same plugin loads normally on my
Windows 2000 Professional desktop machine. On the laptop, the handle
returned from LoadLibrary() is 0, so I used the Win32 API function
SearchPath() to try to find the .dll. This function returns the proper
location for the .dll, but even if I pass in the full path returned from
SearchPath(), the LoadLibrary() function still returns 0 for the handle
to the .dll. Might there be another reason why this .dll is not able to
load? It's very strange because all of the other plugins in the same
directory are loadable on the laptop, and the same .dll is loadable on
the Windows 2000 desktop machine.
Here's something weird that happened to me once. An application
that uses a third-party dll worked only on msw95 and not msw2000.
It was in the same directory as the application, though it was
linked with an implicit library rather than LoadLibrary(). But
anyway, I got it to work by copying the file without its
extension:
cp foobar.dll foobar
I have no idea why this worked for me, or whether it'll work
in your case. Sometimes you just have to mumble every spell in
the grimoire.
Post by Eric Sokolowsky
I called GetLastError() after the failed call to LoadLibrary() and it
said: "Error 487: Attempt to access invalid address." What does this
mean?
Dunno. You might try googling for something like
LoadLibrary 487
or
LoadLibrary "Attempt to access invalid address"
looking in groups as well as the web.
Norman Vine
2004-01-16 17:10:01 UTC
Permalink
Post by Eric Sokolowsky
I use mingw to compile an application that loads plugins with the Win32
API LoadLibrary() function. I create an installer program with
InnoSetup to be able to install the application on the user's hard
drive. I install the plugin .dll files in the same directory as the
executable and use a batch file to launch the executable. I noticed
that one particular plugin does not load on my laptop, which is running
Windows XP Home edition. However, the same plugin loads normally on my
Windows 2000 Professional desktop machine. On the laptop, the handle
returned from LoadLibrary() is 0, so I used the Win32 API function
SearchPath() to try to find the .dll. This function returns the proper
location for the .dll, but even if I pass in the full path returned from
SearchPath(), the LoadLibrary() function still returns 0 for the handle
to the .dll. Might there be another reason why this .dll is not able to
load? It's very strange because all of the other plugins in the same
directory are loadable on the laptop, and the same .dll is loadable on
the Windows 2000 desktop machine.
I called GetLastError() after the failed call to LoadLibrary() and it
said: "Error 487: Attempt to access invalid address." What does this
mean?
Hard to say

What I usually do in these 'wierd' cases is load the program in
"Dependency Walker" http://www.dependencywalker.com/

so I can see what is actually happening

HTH

Norman
Eric Sokolowsky
2004-01-16 17:30:01 UTC
Permalink
Post by Greg Chicares
Post by Eric Sokolowsky
I use mingw to compile an application that loads plugins with the Win32
API LoadLibrary() function. I create an installer program with
InnoSetup to be able to install the application on the user's hard
drive. I install the plugin .dll files in the same directory as the
executable and use a batch file to launch the executable. I noticed
that one particular plugin does not load on my laptop, which is running
Windows XP Home edition. However, the same plugin loads normally on my
Windows 2000 Professional desktop machine. On the laptop, the handle
returned from LoadLibrary() is 0, so I used the Win32 API function
SearchPath() to try to find the .dll. This function returns the proper
location for the .dll, but even if I pass in the full path returned from
SearchPath(), the LoadLibrary() function still returns 0 for the handle
to the .dll. Might there be another reason why this .dll is not able to
load? It's very strange because all of the other plugins in the same
directory are loadable on the laptop, and the same .dll is loadable on
the Windows 2000 desktop machine.
Here's something weird that happened to me once. An application
that uses a third-party dll worked only on msw95 and not msw2000.
It was in the same directory as the application, though it was
linked with an implicit library rather than LoadLibrary(). But
anyway, I got it to work by copying the file without its
cp foobar.dll foobar
I have no idea why this worked for me, or whether it'll work
in your case. Sometimes you just have to mumble every spell in
the grimoire.
Thanks for the suggestion, but it didn't seem to solve the problem. I
tried a few more things, such as installing the application in a
location that did not have any spaces in the path, but to no avail.
Post by Greg Chicares
Post by Eric Sokolowsky
I called GetLastError() after the failed call to LoadLibrary() and it
said: "Error 487: Attempt to access invalid address." What does this
mean?
Dunno. You might try googling for something like
LoadLibrary 487
or
LoadLibrary "Attempt to access invalid address"
looking in groups as well as the web.
I found the following article:

http://groups.google.com/groups?hl=en&lr=lang_en|lang_it|lang_es&ie=UTF-8&oe=UTF-8&safe=active&threadm=3ea929cd.103745948%40News.CIS.DFN.De&rnum=1&prev=/groups%3Fnum%3D100%26hl%3Den%26lr%3Dlang_en%257Clang_it%257Clang_es%26ie%3DUTF-8%26oe%3DUTF-8%26safe%3Dactive%26q%3DLoadLibrary%2B487%26btnG%3DGoogle%2BSearch

Which suggests using LoadLibraryEx(dllname, NULL,
LOAD_LIBRARY_AS_DATAFILE). I am now able to get a handle to the dll.
It is still not running the right routine in the dll yet, but I am
getting closer. I hope.
--
____ __ Eric Sokolowsky (GST) NASA Goddard Space Flight Center
/ __/__/_/__ Visualization Programmer Scientific Visualization Studio
/ __/ _/ / _/ 301.286.3751 Mailstop 935.0 Bldg 28 Rm E102
/___/_//_/__/ ***@gsfc.nasa.gov Greenbelt, MD 20771
Eric Sokolowsky
2004-01-16 19:43:03 UTC
Permalink
Post by Norman Vine
Post by Eric Sokolowsky
I use mingw to compile an application that loads plugins with the Win32
API LoadLibrary() function. I create an installer program with
InnoSetup to be able to install the application on the user's hard
drive. I install the plugin .dll files in the same directory as the
executable and use a batch file to launch the executable. I noticed
that one particular plugin does not load on my laptop, which is running
Windows XP Home edition. However, the same plugin loads normally on my
Windows 2000 Professional desktop machine. On the laptop, the handle
returned from LoadLibrary() is 0, so I used the Win32 API function
SearchPath() to try to find the .dll. This function returns the proper
location for the .dll, but even if I pass in the full path returned from
SearchPath(), the LoadLibrary() function still returns 0 for the handle
to the .dll. Might there be another reason why this .dll is not able to
load? It's very strange because all of the other plugins in the same
directory are loadable on the laptop, and the same .dll is loadable on
the Windows 2000 desktop machine.
I called GetLastError() after the failed call to LoadLibrary() and it
said: "Error 487: Attempt to access invalid address." What does this
mean?
Hard to say
What I usually do in these 'wierd' cases is load the program in
"Dependency Walker" http://www.dependencywalker.com/
so I can see what is actually happening
Thanks for this suggestion. I ran my application with Dependency Walker,
and here is a snippet of the results (Windows XP Home laptop):

LoadLibraryA("libosgdb_gif.dll") called from "LIBOSGDB.DLL" at address 0x6C04694A.
Loaded "LIBOSGDB_GIF.DLL" at address 0x6A980000. Successfully hooked module.
LDR: LdrRelocateImageWithBias() failed 0xc0000018
LDR: OldBase : 10000000
LDR: NewBase : 06760000
LDR: Diff : 0x77f75f420ce8e4ec
LDR: NextOffset : 00000000
LDR: *NextOffset : 0x0
LDR: SizeOfBlock : 0x6760000
Unloaded "Unknown" at address 0x06760000.
Unloaded "LIBOSGDB_GIF.DLL" at address 0x6A980000.
LoadLibraryA("libosgdb_gif.dll") returned NULL. Error: Attempt to access invalid address (487).




Here is the same part of a run on a machine that works (Windows 2000 desktop):

LoadLibraryA("libosgdb_gif.dll") called from "LIBOSGDB.DLL" at address 0x6C04694A.
Loaded "LIBOSGDB_GIF.DLL" at address 0x6A980000. Successfully hooked module.
Loaded "LIBUNGIF.DLL" at address 0x10000000. Successfully hooked module.
DllMain(0x10000000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBUNGIF.DLL" called.
DllMain(0x10000000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBUNGIF.DLL" returned 1 (0x1).
DllMain(0x6A980000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBOSGDB_GIF.DLL" called.
DllMain(0x6A980000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBOSGDB_GIF.DLL" returned 1 (0x1).
LoadLibraryA("libosgdb_gif.dll") returned 0x6A980000.



So it looks like libungif.dll isn't being loaded properly. Am I
interpreting these results correctly? Why would it not want to load
libungif.dll? I have verified that this dll is in the same place as the
others.
--
____ __ Eric Sokolowsky (GST) NASA Goddard Space Flight Center
/ __/__/_/__ Visualization Programmer Scientific Visualization Studio
/ __/ _/ / _/ 301.286.3751 Mailstop 935.0 Bldg 28 Rm E102
/___/_//_/__/ ***@gsfc.nasa.gov Greenbelt, MD 20771
Norman Vine
2004-01-16 20:20:01 UTC
Permalink
Post by Eric Sokolowsky
Post by Norman Vine
Post by Eric Sokolowsky
Might there be another reason why this .dll is not able to
load? It's very strange because all of the other plugins in the same
directory are loadable on the laptop, and the same .dll is loadable on
the Windows 2000 desktop machine.
I called GetLastError() after the failed call to LoadLibrary() and it
said: "Error 487: Attempt to access invalid address." What does this
mean?
Hard to say
What I usually do in these 'wierd' cases is load the program in
"Dependency Walker" http://www.dependencywalker.com/
so I can see what is actually happening
Thanks for this suggestion. I ran my application with Dependency Walker,
LoadLibraryA("libosgdb_gif.dll") called from "LIBOSGDB.DLL" at address 0x6C04694A.
Loaded "LIBOSGDB_GIF.DLL" at address 0x6A980000. Successfully hooked module.
LDR: LdrRelocateImageWithBias() failed 0xc0000018
LDR: OldBase : 10000000
LDR: NewBase : 06760000
LDR: Diff : 0x77f75f420ce8e4ec
LDR: NextOffset : 00000000
LDR: *NextOffset : 0x0
LDR: SizeOfBlock : 0x6760000
Unloaded "Unknown" at address 0x06760000.
Unloaded "LIBOSGDB_GIF.DLL" at address 0x6A980000.
LoadLibraryA("libosgdb_gif.dll") returned NULL. Error: Attempt to access invalid address (487).
LoadLibraryA("libosgdb_gif.dll") called from "LIBOSGDB.DLL" at address 0x6C04694A.
Loaded "LIBOSGDB_GIF.DLL" at address 0x6A980000. Successfully hooked module.
Loaded "LIBUNGIF.DLL" at address 0x10000000. Successfully hooked module.
DllMain(0x10000000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBUNGIF.DLL" called.
DllMain(0x10000000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBUNGIF.DLL" returned 1 (0x1).
DllMain(0x6A980000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBOSGDB_GIF.DLL" called.
DllMain(0x6A980000, DLL_PROCESS_ATTACH, 0x00000000) in "LIBOSGDB_GIF.DLL" returned 1 (0x1).
LoadLibraryA("libosgdb_gif.dll") returned 0x6A980000.
So it looks like libungif.dll isn't being loaded properly. Am I
interpreting these results correctly? Why would it not want to load
libungif.dll? I have verified that this dll is in the same place as the
others.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/rebaseimage.asp
http://www.codeproject.com/dll/rebase.asp

Cygwin command line tool which might just work inside of MSYS
or even a straight MingW32 environment

http://www.tishler.net/jason/software/rebase/

HTH

Norman

Loading...