Load a DLL From a Resource Directly From Memory in Delphi Applications

Load a DLL From a Resource Directly From Memory in Delphi Applications

Article idea by Mark E. Moss

The article how to store a DLL inside a Delphi program exe file as a resource explains how to ship a DLL with your Delphi application executable file as a resource.

Dynamic link libraries contain sharable code or resources, they provide the ability for multiple applications to share a single copy of a routine (or resource) they have in common.

Using resource (.RES) files, you can embed (and use) sound files, video clips, animations and more generally any kind of binary files in a Delphi executable.

Loading DLLs From Memory

if a DLL stored in a RES can be used without first saving it on the file system (hard disk)

According to the article Loading a DLL from memory by Joachim Bauch, this is possible.

Here's how Joachim looks at the issue: The default windows API functions to load external libraries into a program (LoadLibrary, LoadLibraryEx) only work with files on the filesystem. It's therefore impossible to load a DLL from memory. But sometimes, you need exactly this functionality (e.g. you don't want to distribute a lot of files or want to make disassembling harder). Common workarounds for this problems are to write the DLL into a temporary file first and import it from there. When the program terminates, the temporary file gets deleted.

The code in the mentioned article is C++, the next step was to convert it to Delphi. Luckily, this has already been done by Martin Offenwanger (the author of DSPlayer).

Memory Module by Martin Offenwanger is an extended Delphi (and also Lazarus) compatible version of Joachim Bauch's C++ Memory Module 0.0.1. The zip package includes the complete Delphi source code of the MemoyModule (BTMemoryModule.pas). Furthermore there's a Delphi and sample included to demonstrate how to use it.

Loading DLLs From Resources From Memory

If a demo DLL is stored as a resource using the RC file:

ms : TMemoryStream;
rs : TResourceStream;
if 0 FindResource(hInstance, 'DemoDLL', RT_RCDATA) then
rs := TResourceStream.Create(hInstance, 'DemoDLL', RT_RCDATA);
ms := TMemoryStream.Create;
ms.Position := 0;
m_DllDataSize := ms.Size;
mp_DllData := GetMemory(m_DllDataSize);
ms.Read(mp_DllData^, m_DllDataSize);
btMM: PBTMemoryModule;
btMM := BTMemoryLoadLibary(mp_DllData, m_DllDataSize);
if btMM = nil then Abort;
@m_TestCallstd := BTMemoryGetProcAddress(btMM, 'TestCallstd');
if @m_TestCallstd = nil then Abort;
m_TestCallstd('This is a Dll Memory call!');
Showmessage('An error occoured while loading the dll: ' + BTMemoryGetLastError);
if Assigned(btMM) then BTMemoryFreeLibrary(btMM);
  1. Have / Create a DLL
  2. Store the DLL in a RES file
  3. Have BTMemoryModule implementation.
  4. Grab the DLL from the resource and load it directly into memory.
  5. Use BTMemoryModule methods to execute procedure from the DLL in memory.

BTMemoryLoadLibary in Delphi 2009, 2010,…

"The linked BTMemoryModule.pas does not work with Delphi 2009 (and I would assume Delphi 2010 also).
I found a similar version of the BTMemoryModule.pas file a while ago, and made changes so it works with (at least) Delphi 2006, 2007 and 2009. My updated BTMemoryModule.pas, and a sample project, are at BTMemoryLoadLibary for Delphi >= 2009"