-
September 15th, 2004, 09:22 PM
#1
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!
-
September 15th, 2004, 09:52 PM
#2
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
-
September 16th, 2004, 01:53 AM
#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()
-
September 16th, 2004, 02:13 AM
#4
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.
"UNIX is simple; it just takes a genius to understand its simplicity!"
-
September 16th, 2004, 04:01 AM
#5
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
-
September 16th, 2004, 04:44 AM
#6
Re: LoadLibrary failed at 2nd time
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)
there are 10 kinds of people. those who understand binary and those who don't...
rate a post if you find it usefull, thx
check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/
-
September 16th, 2004, 04:47 AM
#7
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)
there are 10 kinds of people. those who understand binary and those who don't...
rate a post if you find it usefull, thx
check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/
-
September 16th, 2004, 09:47 PM
#8
Re: LoadLibrary failed at 2nd time
Originally Posted by bigBA
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
-
September 17th, 2004, 02:01 AM
#9
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.
there are 10 kinds of people. those who understand binary and those who don't...
rate a post if you find it usefull, thx
check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/
-
September 17th, 2004, 10:07 AM
#10
Re: LoadLibrary failed at 2nd time
-
September 15th, 2004, 10:11 PM
#11
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
-
September 16th, 2004, 01:51 AM
#12
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()
-
September 16th, 2004, 01:54 AM
#13
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.
Har Har
-
September 16th, 2004, 02:25 AM
#14
Re: LoadLibrary failed at 2nd time
In other words, call GetLastError() right after LoadLibrary().
-
September 16th, 2004, 03:56 AM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|