程序如何改变自己的资源?
我想将一些 XML 程序首选项作为资源存储在 Delphi 应用程序中。资源将根据用户的更改进行修改。我可以毫无问题地创建 XML 并将其作为资源加载,并且可以修改将其加载到的 xmlDocument,但我不知道如何写回文档。这可能吗?我不希望最后有 2 个文件(.exe 和 .xml)。
I would like to store some XML program preferences as a resource in a Delphi application. The resource would be modified based on user changes. I have no problem creating the XML and loading it as a resource, and can modify the xmlDocument that I load it into, but I don't know how to write the document back. Is this even possible? I would prefer not to end up with 2 files in the end (.exe and .xml).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案既是肯定的,也是否定的。
是的,可以使用 Windows API 例程更新二进制文件中的资源。 此链接指向 BeginUpdateResource()< /a> 应该会让你在这个分数上走上正确的轨道。
但是,您会注意到使用 BeginUpdateResource() 的以下条件:
换句话说,应用程序不可能在运行时简单地更新其自身的资源。
您可以采用多种策略来实现您想要的目标 - 或者足够接近的策略哪个是最合适的取决于您的具体需求,
其中两种可能是:
1)将所有此类资源保存在 DLL 中(仅资源 DLL - 不包含实际代码),仅在以下情况下打开该资源。特别是加载资源(或更新它们)。因此,当您希望将资源写回到 DLL 时,您应该能够获得所需的写锁,
或者
2) 当您需要更新资源时,将当前运行的 EXE 重命名为“myapp.OLD”之类的名称。复制它,以便您有一个当前名称为“myapp.exe”的新文件,然后您可以更新“myapp.exe”,因为它实际上正在执行“myapp.old”。
第二种方法非常混乱并且有一个错误。 “难闻的气味”但是例如,自动更新程序非常常用(或曾经)使用的技术。如果当前运行的代码要使用修改后的 EXE 中的更新资源,显然会在某个时候重新启动您的应用程序,因此它可能不适合您的需求。
The answer is both yes and no.
Yes, it is possible to update resources in a binary using Windows API routines. This link to BeginUpdateResource() should get you on the right track on that score.
However, you will note the following condition on the use of BeginUpdateResource():
In other words, it is not possible for an application to simply update it's own resources while running.
There are a number of strategies you could employ to achieve what you want - or something close enough to it as to be satisfactory. Which is most appropriate will depend on your precise needs.
Of the multitude of solutions, two might be:
1) Maintain all such resources in a DLL (resource only DLL - containing no actual code as such) which you open only when specifically loading resources (or updating them). Thus at the time you wish to write a resource back to the DLL you should be able to get the required write-lock.
or
2) When you need to update a resource rename the current running EXE to something like "myapp.OLD", copy it so that you have a new file with the current name "myapp.exe". You can then update "myapp.exe" because it is actually "myapp.old" that is executing.
This second approach is quite messy and has a "nasty smell" but is a technique that is (or used to be) quite commonly used by auto-updaters, for example. Obviously will involve a restart of your app at some point if the current running code is to make use of the updated resources in the modified EXE, so it may not be appropriate to your needs.
其他需要考虑的事情是防病毒软件可能会将活动标记为可疑。
考虑 Deltics 的答案,我认为您还可以创建一个控制台应用程序,将您的资源写回主 exe。因此,您的主 exe 将其更改保存到文件中,并提取控制台应用程序。当主应用程序终止时,它会调用控制台应用程序。控制台应用程序等待一小段时间,然后将资源文件绑定到主可执行文件,删除资源文件及其本身。控制台应用程序可以进行检查以确保资源文件已成功写入,如果没有成功,则使资源文件保持打开状态。主可执行文件可以在启动时看到资源文件并使用它而不是嵌入文件 - 作为保护措施。
所有这些都假设单个用户应用程序。
Something else to consider is that anti-virus software may flag the activity as suspicious.
Thinking about Deltics' answer I thought you could also create a console application that writes your resource back to the main exe. So your main exe saves it's changes to a file and also extracts the console app. When the main application terminates it calls the console app. The console app waits for a short period of time and then binds the resource file to the main executable, deletes the resource file and itself. The console app could do a check to make sure that the resource file was written successfully and, if not, leave the resource file open. The main executable could see the resource file upon start up and use it instead of the embedded file - as a safeguard.
All of this assumes a single user application.