Can I use LoadLibrary to load a DLL from memory rather then a file?

What I want to do is have an encrypted DLL file.
I wanted to load the file, decrypt it, and then "Load" it like you do a normal DLL from memory.
Is this possible? If so, how do I point to a section of memory and say that is where my DLL is and to load it from there?
[285 byte] By [DeepT] at [2007-11-20 1:23:09]
# 1 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
No, not directly.

Rather decrypt it, save to some disk temp. file and use it as a DLL.
Krishnaa at 2007-11-10 23:18:39 >
# 2 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
Yea, I thought about that. That is my last resort tactic. I suppose a .DLL doesn't need to be called a DLL to load though.
DeepT at 2007-11-10 23:19:44 >
# 3 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
I've never had occasion to use it - but would CMemFile be useful for this purpose?
John E at 2007-11-10 23:20:43 >
# 4 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
I was thinking about that of thing too, but then it seems that LoadLibrary only takes a string path (not a handle) to the file. What path string would you give to point to a memory file?
DeepT at 2007-11-10 23:21:48 >
# 5 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
Hmm, it seems then it must be a temporary file. Do you think that padding at the end of a DLL would hurt it?

The encryption I am using, Blowfish, has to have a buffer that is divisible by an 8 byte boundary. Normally if something isn't divisible by 8, I just pad it with NULLs. Do you think extra NULLs at the end of a DLL file will cause problems?
DeepT at 2007-11-10 23:22:44 >
# 6 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
I was thinking about that of thing too, but then it seems that LoadLibrary only takes a string path (not a handle) to the file.Well, it's a MASSIVE 'if' - but if the CMemFile's m_hFile member is a valid handle (which it probably isn't) you might just be able to use GetProcAddress() to retrieve addresses for the DLL's exported functions. Strictly speaking, GetProcAddress() requires a HMODULE as distinct from a file handle - but it's got to be worth trying.

The documentation for CFile (from which, CMemFile is derived) recommends that you should never use m_hFile directly. I've a sneaking suspicion that it's for exactly this kind of reason... :(

But I'd still give it a shot. You never know what's possible until you try...!
John E at 2007-11-10 23:23:43 >
# 7 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
Well, it's a MASSIVE 'if' - but if the CMemFile's m_hFile member is a valid handle (which it probably isn't) you might just be able to use GetProcAddress() to retrieve addresses for the DLL's exported functions. Strictly speaking, GetProcAddress() requires a HMODULE as distinct from a file handle - but it's got to be worth trying.

The documentation for CFile (from which, CMemFile is derived) recommends that you should never use m_hFile directly. I've a sneaking suspicion that it's for exactly this kind of reason... :(

But I'd still give it a shot. You never know what's possible until you try...!

Implementing loading a DLL and resolving Dependancies all by yourself would be next to impossible to implement, you will need to implement the PE Loader (http://msdn.microsoft.com/msdnmag/issues/02/03/Loader/) itself, and belive me it will one of those things you don't want to do. :D
Krishnaa at 2007-11-10 23:24:52 >
# 8 Re: Can I use LoadLibrary to load a DLL from memory rather then a file?
Hmm, it seems then it must be a temporary file. Do you think that padding at the end of a DLL would hurt it?

The encryption I am using, Blowfish, has to have a buffer that is divisible by an 8 byte boundary. Normally if something isn't divisible by 8, I just pad it with NULLs. Do you think extra NULLs at the end of a DLL file will cause problems?
No they wont cause you a problem except ofcourse the size is increased.
kumaresh_ana at 2007-11-10 23:25:51 >