Http://www.delphifeeds.com/go/f/86025? Utm_source = feedburner & utm_medium = Email & utm_campaign = feed % 3A + delphifeeds + % 28delphifeeds.com % 29
Load a DLL from a resource directly from memory in Delphi applicationsuse DLL from resources (RES) without storing it on the hard-disk first
Article idea by Mark E. MossThe 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 memoryRecently, I 've got ed an email from Mark E. Moss, askingIf 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 have want to distribute a lot of files or want to make disconfiguring 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
Martin offenwanger is an extended Delphi (and also Lazarus) compatible version of Joachim bauch's c ++ memory module 0.0.1. the zip package contains des the complete Delphi source code of the memoymodule (btmemorymodule. PAS ). furthermore there's a Delphi and sample
Added to demonstrate how to use it.
Loading DLLs from resources from memoryWhat was left to implement is to grab the DLL from a res file and then call its procedures and functions.
If a demo DLL is stored as a resource using the RC file:
Demodll rcdata demodll. dll
To load it from the resource, the next code can be used:
VaR
MS: tmemorystream;
RS: tresourcestream;
Begin
If0 <> findresource (hinstance, 'demodll ', rt_rcdata)Then
Begin
RS: = tresourcestream. Create (hinstance, 'demodll ', rt_rcdata );
MS: = tmemorystream. Create;
Try
Ms. loadfromstream (RS );
Ms. Position: = 0;
M_dlldatasize: = Ms. size;
Mp_dlldata: = getmemory (m_dlldatasize );
Ms. Read (mp_dlldata ^, m_dlldatasize );
Finally
Ms. Free;
Rs. Free;
End;
End;
End;
Next, when you have the DLL loaded from a resource into memory, you can call its procedures:
VaR
Btmm: pbtmemorymodule;
Begin
Btmm: = btmemoryloadlibary (mp_dlldata, m_dlldatasize );
Try
If btmm =Nil thenAbort;
@ M_testcallstd: = btmemorygetprocaddress (btmm, 'testcallstd ');
If @ m_testcallstd = nil then abort;
M_testcallstd ('this is a dll Memory call! ');
Except
Showmessage ('an error occoured while loading the DLL: '+ btmemorygetlasterror );
End;
IfAssigned (btmm)ThenBtmemoryfreelibrary (btmm );
End;
That's it. Here's a quick recipe:
- Have/create a DLL
- Store the DLL in a res File
- Have btmemorymodule
Implementation.
- Grab the DLL from the resource and load it directly into memory.
- Use btmemorymodule methods to execute procedure from the DLL in memory.
Btmemoryloadlibary in Delphi 2009,201 0 ,...Soon after publishing this article I 've got ed an email from Jason PENNY:
"The linked btmemorymodule. PAS does not work with Delphi 2009 (and I wowould 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,200 7 and 2009. my updated btmemorymodule. PAS, and a sample project, are at btmemoryloadlibary
For Delphi & gt; = 2009 & quot"Suggested reading
- Creating and using a resource only DLL with Delphi
- Dynamic Link Libraries (DLL) and Delphi
- Delphi Memory Manager problems in dynamic libraries
(Page 1/2)
Suggested reading
New posts to the Delphi programming forums:
- Access violation
For function in object?
- DLL
- Pastefromclipboard
Related Articles
- Creating and using a resource only DLL with Delphi
- Store RTF as a resource-load resource
RTF into a trichedit in Delphi prog...
- Resource files made easy
- Inside the (Delphi) Exe-storing resource
(WAV, MP3,) into Delphi executa...
- Web site inside a Delphi exe