Load DLL From Memory

I have an application in which I store numerous DLLs and currently save the DLLs to the temp folder then load them from there.

I would like to know if there is any way to load a DLL directly from memory. I have access to the DLL as a stream, is there any way I can load the DLL directly from there and use GetProcAddress on it, then call some methods and free it?

I'd really like to be able to avaoid saving to disk if its possible.
LVL 5
paulb1989Asked:
Who is Participating?
 
BlackTigerXCommented:
the answer would be *no*, is not possible using documented functions, however it is possible, using (of course) undocumented functions or some hacks, the problem with that is that your code can break any time with a service pack or some update
0
 
Slick812Commented:
hello  paulb1989, I do not get your reason for your methods? ? if your program has these DLLs in it's resources, then it seems like you should just add the code in the DLL's to your program, since all of the code (as the DLL) in in your program file anyway. Any reason for wanting to have the DLLs as resource and call them from a resource memory location? It may be posible to get the memory location of a DLL function (if it is static, as not in a TObject method),  However, there may be problems if your DLL has any inialization code that sets up non-static variables,
0
 
paulb1989Author Commented:
The reason is that I have a program that adds DLLs to the end of another EXE file (the one that loads them), so there may be different DLLs each time the program runs. Other DLLs may be developed and there may be any combination of DLLs attached to the EXE. I can read them into a TMemoryStream, and currently I save them to disk which I would like not to have to do...
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Slick812Commented:
OK, this seems more complex than I thought,
If a DLL has Static functions, I think you can get the memory address to use in a TResourceStream, It would be the same memory address the GetProcAddress( ) function returns, minus the hInstance of the DLL,

But I tried to tell you, there may be non-static variables used or reference data set up in the DLL during it's initialization, the initialization will NOT be executed

and there will be NO variable storage set up on the heap for the DLL global variables

this may cause big problems. .  
0
 
Slick812Commented:
You might ask how to execute (run) a DLL from memory, I have seen this question asked here at EE and other question forums (the actuall questions were for programs not DLLs), and I have yet to see a way to do this, the win system has it's program execution based on a File location, so It may be very difficult to run an executable from just a memory location
0
 
paulb1989Author Commented:
OK it seems like I'll have to continue with the method I'm using (saving to disk).
0
 
BlackTigerXCommented:
lot safer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.