Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to load DLL from memory

88 views
Skip to first unread message

George Wilkin

unread,
Jun 3, 2003, 7:00:50 AM6/3/03
to
Hello, my app uses some DLL files (that it load dynamically).
I dont wat these files to be separate DLL's, and I want to include them into
my exe.
My solution is to include these DLLs as binary resources into my EXE, and
then, when my EXE runs, extract them to disk, and call LoadLibrary on these
fresh-created files. But it implies creating files on disk. May be there is
another approach? The objective is to include all DLLs into a single EXE.
Maybe I can load DLL from memory image? Any clues welcome. Thank you.


arkadyf

unread,
Jun 3, 2003, 8:42:39 AM6/3/03
to
I wrote to you in
http://216.239.37.104/groups?q=arkadyf+BINRES&hl=en&lr=&ie=UTF-8&oe=UTF-8&sc
oring=d&selm=%230eY7fUGDHA.1904%40TK2MSFTNGP10.phx.gbl&rnum=1
( aware of wrap ) how to have dll into exe .
LockResource of such dll , which sit into the belly of exe before save it as
dll on disk, give you pointer on first byte of it , so now you need to
implement GetProcAddress searching PE header to find where function start
( from starting pointer you have )
Arkady


"George Wilkin" <g...@dontspam.com> wrote in message
news:bbhv5c$jbp$1...@nsnmpen2-gest.nuria.telefonica-data.net...

Phil Barila

unread,
Jun 3, 2003, 11:14:19 AM6/3/03
to
"arkadyf" <ark...@hotmail.com> wrote in message
news:eH3npUcK...@TK2MSFTNGP11.phx.gbl...

Why would you go to that much trouble for an ugly and questionably portable
hack? Unless you are hijacking someone else's DLL without their permission,
you should be able to get a static lib version of the library. Just
statically link it, no DLL required.

If you can't get a static lib, how did you get permission to use the DLL?
How did you get the interface definition?

Phil
--
Philip D. Barila
Seagate Technology, LLC
(720) 684-1842
As if I need to say it: Not speaking for Seagate.
E-mail address is pointed at a domain squatter. Use reply-to instead.


George Wilkin

unread,
Jun 3, 2003, 10:17:11 AM6/3/03
to
Thank you.. and how do I find a function by name, in DLL image? And is it
enough just to have DLL image in memory? What about relocations and other
preparations?

"arkadyf" <ark...@hotmail.com>

George Wilkin

unread,
Jun 3, 2003, 12:20:12 PM6/3/03
to
For example, in my case I use audio codec, MSGSM32.ACM. I dont want to
oblige the user to install this codec, and I can dynamically load this
library and register it using LoadLibrary and acmAddDriver(). And to avoid
storing it in a separate file, I want to put it inside my exe.

James Brown

unread,
Jun 3, 2003, 1:04:46 PM6/3/03
to
It is not enough to do LockResource and "load" the DLL from the resource
section,
because of the nature of PE files. You need to do several more steps:

1. Store DLL (a PE file) as resource
2. Lock the resource (i.e. get pointer to PE header)
3. Allocate (using VirtualAlloc) enough memory as indicated in
NtHeader.OptionalHeader.SizeOfImage
4. Try to allocate this at location ntheader.OptionalHeader.ImageBase - if
this fails, then just allocate
anywhere in your address space.
5. Copy (from resource into newly allocated space) the DLL's PE header (+
section tables), and
each section 1-by-1 into the correct places (as described in the PE
header)
6. Fix the new PE header to indicate where the module has been loaded
7. Perform full Base-Relocations - you need to find the base-relocations
section by looking
in the data-directory of the PE header.
8. Fixup the DLL's import table, by looping through it calling
LoadLibrary/GetProcAddress as
appropriate
9. Call the DLL's entry-point with DLL_PROCESS_ATTACH - the signature
of the DLL entrypoint is BOOL __stdcall DllEntry(PVOID base, DWORD
dwReason, PVOID reserved)

10. (Optionally) add the loaded module into the linked-list of inside the
PEB. Not required.

That's all you need to do to load a DLL - you can load pretty much any
system DLL as well using this
technique. It's not too much work, but you must be familiar with the PE
format (portable executable)
in order to get it to work.

Cheers,
James
--
www.catch22.org.uk
Free Win32 Software, Source Code and Tutorials

"George Wilkin" <g...@dontspam.com> wrote in message

news:bbiihi$rm9$1...@nsnmpen2-gest.nuria.telefonica-data.net...

George Wilkin

unread,
Jun 3, 2003, 6:05:59 PM6/3/03
to
Uhm.. actually, what I have been doing is dumping PE image from binary
resource to disk, then calling LoadLibrary on it, and surely it worked, but
I wanted to avoid creating any files on user's PC. And about the technique
described:

Steps 4 to 9 not very clear to me (I am not familiar with PE file format), I
suppose it consists of walking the image in memory and modifying values at
certain places.. may be there are some examples on the Internet, may be you
can point me on where I must search for it.. Thank you.
Another approach: as LoadLibrary() already does all this work, may be there
is a way to "deceive" LoadLibrary() function and make it read the image from
memory, instead of reading from disk (IMHO, one of the first things
LoadLibrary() does is reading entirely DLL file into memory, so maybe I can
intercept its ReadFile() call???..), and then let it work normally (because
relocations and stuff is done in memory, I suppose)
Thank you!

"James Brown" <PLEASEDONTSPA...@virgin.net>

Barry S. Kyker

unread,
Jun 4, 2003, 12:42:51 AM6/4/03
to
"George Wilkin" <g...@dontspam.com> wrote in message
news:bbj64a$hbf$1...@nsnmpen2-gest.nuria.telefonica-data.net...

You could write a program that can unpack files appended to the
end of the EXE. The user runs the exe with the cargo files,
the program unpacks the cargo files and then executes the
main program.

This is what I do for an update utilty used to upgrade our
customer's software.

--
Best regards,

Barry S. Kyker

Pertinax

unread,
Jun 13, 2003, 2:06:35 AM6/13/03
to
I would stick with your current method.

It has served me well. I also cleanup any files I extract from myself when
my program closes.

Pertinax

"George Wilkin" <g...@dontspam.com> wrote in message

news:bbj64a$hbf$1...@nsnmpen2-gest.nuria.telefonica-data.net...

0 new messages