Delphi directly executes DLL from application resources (no need to save the DLL to the disk first)

Source: Internet
Author: User

Http://www.delphifeeds.com/go/f/86025? Utm_source = feedburner & utm_medium = Email & utm_campaign = feed % 3A + delphifeeds + % 28delphifeeds.com % 29

Btmemorymodule: http://code.google.com/p/memorymodule/downloads/list

Load a DLL from a resource directly from memory in Delphi applicationsuse DLL from resources (RES) without storing it on the hard-disk first

By Zarko Gajic, about.com Guide

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

Recently, 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 memory

What 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
If
0 <> 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:

  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,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

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.