LoadLibrary failed at 2nd time
I have a dll named as "hello.dll", which created under Cygwin(gcc 2.95)
by this way: gcc -shared -o hello.dll hello.o
then I load the dll in Visual C++ program, At the first time, LoadLibrary() successfully,
and I can call the function in the dll, at last I called FreeLibrary(),
But when I called LoadLibrary() the second time, the program crashed.
I don't konw why and how to solve the problem,
Thanks for any direction!
[462 byte] By [
astar] at [2007-11-19 0:23:09]

# 1 Re: LoadLibrary failed at 2nd time
FreeLibrary should be returning non-zero if successful - does it?
If not, perhaps the hModule you're storeing is not correct.
You should call GetLastError if LoadLibrary fails. This should indicate the source of the problem.
Also you could try loading, unloading then loading again before calling the DLL functions. If it is only after calling the DLL function that you see the problem, then the problem is within the DLL, not your calling application.
You're not doing any of this loading within InitInstance or ExitInstance are you? If so, this could be the problem (can't load or unload DLLs there)...
Hope this helps,
- Nigel
NigelQ at 2007-11-11 0:55:39 >

# 2 Re: LoadLibrary failed at 2nd time
You can use GetLastError() and check against the error code table to understand what goes wrong.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp
Kheun at 2007-11-11 0:56:44 >

# 3 Re: LoadLibrary failed at 2nd time
I've tried as the following:
try
{
hd = LoadLibrary("hello.dll");
if (hd)
{
(..)aa = GetProcAddress(...);
aa(); //successful
int a = FreeLibrary(hd); //a is not-zero
}
}
catch(...)
{
int a = GetLastError();
}
//but at the 2nd time calling loadlibrary(), the program crashed, never come into catch()
astar at 2007-11-11 0:57:43 >

# 4 Re: LoadLibrary failed at 2nd time
I've tried as the following:
try
{
hd = LoadLibrary("hello.dll");
if (hd)
{
(..)aa = GetProcAddress(...);
aa(); //successful
int a = FreeLibrary(hd); //a is not-zero
}
}
catch(...)
{
int a = GetLastError();
}
//but at the 2nd time calling loadlibrary(), the program crashed, never come into catch()
astar at 2007-11-11 0:58:40 >

# 5 Re: LoadLibrary failed at 2nd time
LoadLibrary doesn't throw any exception, so the catch block will never be reached.
Regarding your problem: what else does your application do between the 2 LoadLibrary calls?
PS: use code tags as it is easier to read your post.
# 6 Re: LoadLibrary failed at 2nd time
The LoadLibrary() function doesn't throw any exceptions when an error occurs.
If its return value is NULL, then you should call GetLastError() to get an error code.
# 7 Re: LoadLibrary failed at 2nd time
In other words, call GetLastError() right after LoadLibrary().
Kheun at 2007-11-11 1:01:52 >

# 8 Re: LoadLibrary failed at 2nd time
LoadLibrary() the 2nd time, then the program will crash.
GetLastError() right after LoadLibrary() will useless.
then I can't get any error code
the program and the dll are both simple
CXXX::OnBtnAA()
{
LoadLibrary();
...
FreeLibrary();
}
when I clicked the button the 2nd time, the programe crashed after LoadLibrary()
Notice: the dll is created under Cygwin/gcc;
gcc -shared -o hello.dll hello.o
astar at 2007-11-11 1:02:53 >

# 9 Re: LoadLibrary failed at 2nd time
Have you debugged your app to see if the crash occurs in the DLL or in the application?
# 10 Re: LoadLibrary failed at 2nd time
LoadLibrary() the 2nd time, then the program will crash.
GetLastError() right after LoadLibrary() will useless.
then I can't get any error code
the program and the dll are both simple
CXXX::OnBtnAA()
{
LoadLibrary();
...
FreeLibrary();
}
when I clicked the button the 2nd time, the programe crashed after LoadLibrary()
Notice: the dll is created under Cygwin/gcc;
gcc -shared -o hello.dll hello.o
astar at 2007-11-11 1:04:46 >

# 11 Re: LoadLibrary failed at 2nd time
the 2nd time when the program stop at the line of "LoadLibrary()"
then I press F11,
one error message popped up "The thread 0x**** has exited with code 1"
and the info in call stack window is that:
NTDLL! ****
KERNEL32! ****
CYGWIN1! ****
KERNEL32! ****
astar at 2007-11-11 1:05:55 >

# 12 Re: LoadLibrary failed at 2nd time
You're not doing any of this loading within InitInstance or ExitInstance are you? If so, this could be the problem (can't load or unload DLLs there)...
why you can't load/freelibrary in Init/exitInstance of your CWinApp/Thread derived classes?
it if for example a normal (and only suitable) way fpr loading/freeing resource dlls... so why yo you think one can't load/freelibrary dlls in init/exitinstance?
@astar:
why do you want to load/freelibrary the dll in a OnXXX Handler? why you don't load it on OnCreate and free it in OnDestroy or in the constructor/destructor?
why such overhead?
(LoadLibrary maps the desired dll (or at least tries it) in the processes adress space and FreeLibrary unmaps it from the processes adress space, so every time the handler is called windows has to map and unmap the dll)
bigBA at 2007-11-11 1:06:51 >

# 13 Re: LoadLibrary failed at 2nd time
what are you doing in your dlls DllMain Function? maybe there is a problem... (LoadLibrary calls the DllMain function)
bigBA at 2007-11-11 1:07:57 >

# 14 Re: LoadLibrary failed at 2nd time
But when I called LoadLibrary() the second time, the program crashed.
I don't konw why and how to solve the problem,
Thanks for any direction!
So you are saying that this fails?
#include <windows.h>
int main()
{
HMODULE hMod;
for ( int i = 0; i < 2; ++i )
{
hMod = LoadLibrary("hello.dll");
if ( hMod )
FreeLibrary( hMod );
}
}
Does this code work (it calls LoadLibrary/FreeLibrary 2 times)? If it does, the problem is *not* just calling "FreeLibrary" a second time. So you should confirm with a very simple program first to make sure the problem is what you think it is. If it does work, then your program is causing some other bug that is affecting the call to FreeLibrary. In other words, FreeLibrary crashing is a symptom, and not the cause of the problem.
Regards,
Paul McKenzie
# 15 Re: LoadLibrary failed at 2nd time
why you can't load/freelibrary in Init/exitInstance of your CWinApp/Thread derived classes?
it if for example a normal (and only suitable) way fpr loading/freeing resource dlls... so why yo you think one can't load/freelibrary dlls in init/exitinstance?
@astar:
why do you want to load/freelibrary the dll in a OnXXX Handler? why you don't load it on OnCreate and free it in OnDestroy or in the constructor/destructor?
why such overhead?
(LoadLibrary maps the desired dll (or at least tries it) in the processes adress space and FreeLibrary unmaps it from the processes adress space, so every time the handler is called windows has to map and unmap the dll)
When I clicked the button, I want to call the functions in the DLL. If I put load/free into constructor/destructor, the variable in DLL will remain the value at the last time be called, which result to call the functions in the DLL in 2nd time, 3rd time ... failed. So I need unmap everything in the memory before I call it, could you tell me how to? Thanks
astar at 2007-11-11 1:09:59 >

# 16 Re: LoadLibrary failed at 2nd time
hm.. maybe i don't really get your point...
just for making clear what you want to achieve:
- you have a dll
- the dll exports a function
- which returns a value of a global variable in the dll (??)
what kind of variable or value does the function return? (can you post the code?)
but if you load the dll everytime the handler is called(and in case that you only have one of this button in your app, the dll gets mapped and unmapped every time the handler is called... hm... no... it does get mapped/unmaped every time, also if you have more of these buttons (because your app isn't multithreaded, is it?)......
back to the variable and the value... every time your handler gets called, the dll gets mapped, and so the variable with its initial value. i really don't know what you do in your function... but if you just return this variable, it will have the same value (it also will, if you doesn't modify its contents...) every time you call the function.
also if you modify the variable, right after the modify your dll (and so the variable) gets unmapped from adress space. and the next time your handler gets called the dll gets mapped again (with the variable, with its initial value......)
hm... no idea.
please post the code of this function and the mentioned variables.
bigBA at 2007-11-11 1:10:57 >

# 17 Re: LoadLibrary failed at 2nd time
[ Merged threads ]