打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Delphi从应用程序资源中直接执行dll(不用先把dll存到磁盘上)
Delphi从应用程序资源中直接执行dll(不用先把dll存到磁盘上)
标签:dlldelphi磁盘resourcesfilertf
2011-11-08 08:38 1202人阅读 (0)
分类:
delphi开发(64)
目录
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
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 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: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 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;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, 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
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
0
0
上一篇EUpdateError -2147217900
下一篇ASPxTreeList 未将对象引用设置到对象的实例
我的同类文章
delphi开发(64)
·Delphi 10 Seattle不支持intel atom?2016-02-04阅读307
·Delphi的dll如何向C#传出string(2)2013-05-23阅读794
·Delphi XE3初体验(一)2012-11-13阅读2086
·通过挂钩API函数给加了保护的应用程序内嵌补丁2012-05-09阅读599
·文本翻译API及Web Service2012-03-10阅读560
·c#调用delphi编写的SOAP Web Service(2)2012-03-02阅读841
·delphi 10 seattle "Automatic Reference Counting"问题2016-02-01阅读153
·delphi7在win7 64bit系统下的问题2013-01-10阅读1145
·DCOM在windows 2008 Server的配置2012-08-10阅读2183
·Delphi各个版本的编译指令2012-04-06阅读991
·windows7下不产生virtualstore目录的方法2012-03-05阅读2866
更多文章
参考知识库
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Python4Delphi安装
Mechanical Sympathy: Memory Barriers/Fences
Delphi的"Invalid pointer operation"异常的解决办法 - D...
Using a Doc/View exported from a dynamically loaded DLL (SDI)
Delphi 中使用 ActiveX DLL的方法
D2007在win7下bordbk105N.dll 莫名其妙的问题。
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服