Delphi从应用程序资源中直接执行dll(不用先把dll存到磁盘上)
来源:互联网 发布:seo区综合查询 编辑:程序博客网 时间:2024/05/24 05:56
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 Applications
Use DLL from Resources (RES) Without Storing it on the Hard-Disk First
By Zarko Gajic, About.com Guide
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 received an email from Mark E. Moss, asking 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
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.dllto load it from the resource, the next code can be used:
varNext, when you have the DLL loaded from a resource into memory, you can call its procedures:
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;
varThat's it. Here's a quick recipe:
btMM: PBTMemoryModule;
begin
btMM := BTMemoryLoadLibary(mp_DllData, m_DllDataSize);
try
if btMM = nil then Abort;
@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;
if Assigned(btMM) then BTMemoryFreeLibrary(btMM);
end;
- 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, 2010, ...
Soon after publishing this article I've received an email from Jason Penny:"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"
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
- Access violation for function in object?
- DLL
- Pastefromclipboard
- 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
- Delphi从应用程序资源中直接执行dll(不用先把dll存到磁盘上)
- delphi从dll中调用图片资源
- MFC--把dll加载到资源中
- delphi把窗体嵌入到dll中。实现方法
- Delphi从内存加载dll,无需从资源中释放dll
- SetDll把Dll文件注入到.exe应用程序中
- vc资源中的DLL,保存到磁盘
- 在DELPHI应用程序中使用DLL
- 在DELPHI应用程序中使用DLL
- 在Delphi应用程序中使用DLL
- Delphi 资源文件 Dll
- Delphi中如何把函数代码调出主程序,写到“DLL”中并调用
- 从内存中加载DLL DELPHI版
- 【每日一发】Delphi从Dll中提取自定义的资源文件
- C#从DLL中提取资源
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
- 使用GDI绘制简单曲线
- USB的八个问题和答案
- 解决request中文乱码的问题
- 私有云vs公有云vs混合云
- OSG的优点
- Delphi从应用程序资源中直接执行dll(不用先把dll存到磁盘上)
- TagSupport类的生命周期
- 继续聊WPF——设置网格控件列标题的样式
- xstream转换Map和List
- JSP开发模式
- 《Windows高级调试》分析(二)
- struts2标签使用详解
- jsp标签
- xampp apache 无法启动