程序如何更改自己的资源? [英] How can a program change its own resources?

查看:73
本文介绍了程序如何更改自己的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些XML程序首选项作为资源存储在Delphi应用程序中。资源将根据用户更改进行修改。创建XML并将其加载为资源没有问题,并且可以修改加载到其中的xmlDocument,但是我不知道如何写回该文档。这有可能吗?我不希望最后没有两个文件(.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).

推荐答案

答案都是是和否。

是的,可以使用Windows API例程更新二进制文件中的资源。 指向BeginUpdateResource()的链接应该使您在该分数上处于正确的轨道。

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.

但是,在使用BeginUpdateResource()时,您会注意到以下条件:

However, you will note the following condition on the use of BeginUpdateResource():


用于更新资源的二进制文件。应用程序必须是
,并且能够对此文件进行写访问;该文件由$引用b $ b pFileName当前无法执行。

"The binary file in which to update resources. An application must be able to obtain write-access to this file; the file referenced by pFileName cannot be currently executing."

换句话说,应用程序不可能简单地更新自己的资源

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)将所有此类资源维护在DLL(仅资源DLL-不包含此类实际代码)中,只有在专门加载资源(或更新它们)时才打开。因此,在您希望将资源写回到DLL时,您应该能够获得所需的写锁。

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.

2)当您需要更新资源时,将当前正在运行的EXE重命名为 myapp .OLD,将其复制,以便您使用当前名称为 myapp.exe的新文件。然后,您可以更新 myapp.exe,因为它实际上是正在执行的 myapp.old。

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.

第二种方法非常凌乱,并且有令人讨厌的气味但是,例如,这种技术在自动更新程序中非常(或曾经)很常用。如果当前正在运行的代码要利用修改后的EXE中的更新资源,则显然会在某个时候重启应用程序,因此可能不适合您的需求。

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.

这篇关于程序如何更改自己的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆