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!
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
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/de...rror_codes.asp
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()
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()
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.
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.
Re: LoadLibrary failed at 2nd time
In other words, call GetLastError() right after LoadLibrary().
Re: LoadLibrary failed at 2nd time
Have you debugged your app to see if the crash occurs in the DLL or in the application?
the 2nd time of LoadLibrary() then crash
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
info in call stack window
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! ****
Re: LoadLibrary failed at 2nd time
Quote:
Originally Posted by NigelQ
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)
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)
Re: LoadLibrary failed at 2nd time
Quote:
Originally Posted by astar
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?
Code:
#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