Discussion:
FreeLibrary on kernel32.dll
(too old to reply)
vecchio56
2009-05-07 19:25:19 UTC
Permalink
Hello,

I've got a program which browses resources contained in a dll chosen by
the program user.
I'm using LoadLibrary/FreeLibrary, and I have a strange behavior when
the dll I browse is a copy of kernel32.dll :
For example (aaa.dll is a copy of kernel32.dll) :

int main()
{
HMODULE hMod;
BOOL bRet;

hMod = LoadLibrary(TEXT("C:\\test\\aaa.dll")); // OK
bRet = FreeLibrary(hMod); // OK

hMod = LoadLibrary(TEXT("C:\\test\\aaa.dll")); // First chance
exception -> hMod = 0
// starting here, I can't load aaa.dll anymore
bRet = FreeLibrary(hMod);

// but it still works on the system kernel32.dll
hMod = LoadLibrary(TEXT("kernel32.dll")); // OK
bRet = FreeLibrary(hMod); // OK

hMod = LoadLibrary(TEXT("kernel32.dll")); // OK
bRet = FreeLibrary(hMod); // OK
}

I don't have the problem with other dll like shell32.dll.
LoadLibrary and FreeLibrary are defined in kernel32.dll but it doesn't
explain this problem.

Please help!

Thanks
Kornél Pál
2009-05-07 20:57:34 UTC
Permalink
Basic system dlls don't like relocation. The copied DLL gets loaded to a
different address so hopefully doesn't affect the original kernel32.dll
but you are lucky that your process remains in a usable state after this
experiment.

You shouldn't execute it's entry point or call any function from dlls
loaded for browsing resources.

Use LoadLibraryEx LOAD_LIBRARY_AS_DATAFILE. If that doesn't work for you
use DONT_RESOLVE_DLL_REFERENCES but make sure that you never call
GetProcAddress on that handle.

Kornél
Post by vecchio56
Hello,
I've got a program which browses resources contained in a dll chosen by
the program user.
I'm using LoadLibrary/FreeLibrary, and I have a strange behavior when
int main()
{
HMODULE hMod;
BOOL bRet;
hMod = LoadLibrary(TEXT("C:\\test\\aaa.dll")); // OK
bRet = FreeLibrary(hMod); // OK
hMod = LoadLibrary(TEXT("C:\\test\\aaa.dll")); // First chance
exception -> hMod = 0
// starting here, I can't load aaa.dll anymore
bRet = FreeLibrary(hMod);
// but it still works on the system kernel32.dll
hMod = LoadLibrary(TEXT("kernel32.dll")); // OK
bRet = FreeLibrary(hMod); // OK
hMod = LoadLibrary(TEXT("kernel32.dll")); // OK
bRet = FreeLibrary(hMod); // OK
}
I don't have the problem with other dll like shell32.dll.
LoadLibrary and FreeLibrary are defined in kernel32.dll but it doesn't
explain this problem.
Please help!
Thanks
vecchio56
2009-05-07 22:21:23 UTC
Permalink
Calling LoadLibraryEx with the DONT_RESOLVE_DLL_REFERENCES flag worked
perfectly.
Thank you
Post by Kornél Pál
Basic system dlls don't like relocation. The copied DLL gets loaded to a
different address so hopefully doesn't affect the original kernel32.dll
but you are lucky that your process remains in a usable state after this
experiment.
You shouldn't execute it's entry point or call any function from dlls
loaded for browsing resources.
Use LoadLibraryEx LOAD_LIBRARY_AS_DATAFILE. If that doesn't work for you
use DONT_RESOLVE_DLL_REFERENCES but make sure that you never call
GetProcAddress on that handle.
Kornél
Post by vecchio56
Hello,
I've got a program which browses resources contained in a dll chosen
by the program user.
I'm using LoadLibrary/FreeLibrary, and I have a strange behavior when
int main()
{
HMODULE hMod;
BOOL bRet;
hMod = LoadLibrary(TEXT("C:\\test\\aaa.dll")); // OK
bRet = FreeLibrary(hMod); // OK
hMod = LoadLibrary(TEXT("C:\\test\\aaa.dll")); // First chance
exception -> hMod = 0
// starting here, I can't load aaa.dll anymore
bRet = FreeLibrary(hMod);
// but it still works on the system kernel32.dll
hMod = LoadLibrary(TEXT("kernel32.dll")); // OK
bRet = FreeLibrary(hMod); // OK
hMod = LoadLibrary(TEXT("kernel32.dll")); // OK
bRet = FreeLibrary(hMod); // OK
}
I don't have the problem with other dll like shell32.dll.
LoadLibrary and FreeLibrary are defined in kernel32.dll but it doesn't
explain this problem.
Please help!
Thanks
Loading...