c++ modify resources at runtime

2019-04-24 18:32发布

问题:

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.

回答1:

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.