c++ 在运行时修改资源
是否可以在运行时以编程方式编辑可执行文件的资源? 如果是这样,怎么办? 如果没有,是否有其他程序可以轻松地用来修改资源?
谢谢,德里克。
Is it possible to edit resources for an executable at runtime programmatically? If so, how? If not, is there another program that can easily be used to modify resources?
Thanks, Derek.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的,尽管不是特别容易。 它基本上需要编写一个资源编译器(至少对于您想要修改的资源类型)。
例如,我曾经编写过一个菜单编译器,它在运行时从数据库中取出输入(然后将结果保存回数据库)。 如果源表未更改,则使用现有资源,否则将重新构建。
对于菜单资源(我相信是对话框),棘手的一点是某些成员仅根据标志设置以及严格的对齐要求而存在。
在修改 .exe 中已存在的资源的情况下,您需要将资源数据复制到内存缓冲区(如果要添加新元素,则需要额外的空间)(通过使用 FindResource、LockResource、内存副本,然后 UnlockResource)。
进行更改后,您可以使用间接创建函数之一(即 CreateMenuIndirect)并传递缓冲区的地址。
资源 API 允许将此类缓冲区写回应用程序二进制文件,但如果您使用代码签名,这会破坏签名,因此要非常小心。 我也不知道该 API 是否适用于实际运行的程序。
Yes, it is possible, though not especially easy. It basically requires writing a resource compiler (at least for the resource types you want to modify).
For example I once wrote a menu compiler that took its input out of a database at run-time (at then saved the result back to the DB). If the source tables were unchanged then the existing resource was used, otherwise it was rebuilt.
In the case of menu resources (and I believe dialogs) the tricky bit is that certain members are only present depending on flag settings, as well as strict alignment requirements.
In the case of modifying resources already present in your .exe you would need to copy the resource data into a memory buffer (with extra space available if you are adding new elements) (by using FindResource, LockResource, a memory copy then UnlockResource).
After making the changes you then use one of the indirect create functions (i.e. CreateMenuIndirect) and pass the buffer's address.
The resource API allows for writing such a buffer back to the application binary but that would break the signature if you use code signing so be very careful. I also do not know if that API works for a program that is actually running.