开发者

Modifying resource contents of a running executable

开发者 https://www.devze.com 2023-02-02 05:42 出处:网络
All, I store my application settings in a resource. When my program first loads, I read the specified resource using WinA开发者_运维问答PI. I then parse the retrieved byte data. This works flawlessl

All,

I store my application settings in a resource. When my program first loads, I read the specified resource using WinA开发者_运维问答PI. I then parse the retrieved byte data. This works flawlessly for me.

Now let's say that a user alters a setting in my application. He/she checks a checkbox control. I would like to save the updated setting to my resource. However, it seems that my call to UpdateResource will not work while my application is running. I can't modify my resource data even though it is the same size.

First, is it possible to modify a running image's resource data? Second, if that is not possible, what alternatives do I have for storing settings internally within my application?

NOTE: I must have the settings within my running executable. They cannot be on the harddrive or in the registry. Please don't even suggest that as an option.


It is 100% possible to write self-modifying code. It's just not very easy to do.

When you launch your executable file, windows maps it in memory. This essentially locks the file and prevents edits while it is running. You can, obviously, unmap your program (by using an undocumented function in ntdll). When your file is unmapped, you will be able to write changes to it.

This is kinda similar to what you want to do: http://www.johnfindlay.plus.com/lcc-win32/asm/SelDelNT.htm

Instead of deleting it, obviously, you want to make changes. The idea behind the madness is the same - you must unmap the file.


  1. Unfortunately that SelDelNT doesn't work on XP+ - it can unmap the file, but exe image handle is not 0x4 anymore - in fact stdin handle is 0x3 and stdout handle is 0x7, so its likely that exe handle was intentionally made inaccessible (-1 or something).
  2. One possibility is to rename the exe (its allowed), then copy it to the file with original name and modify it, and use MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT (or some other trick maybe) to delete it later.
  3. A more obvious version of [2] is to copy the exe to %TEMP% first and restart from there.
  4. VSS (http://msdn.microsoft.com/en-us/library/aa384645(v=VS.85).aspx) may allow this (via "restore") but its very slow and has noticeable side effects.
  5. A /SWAPRUN:NET linker/editbin option seems to be relevant.
  6. Here's a tutorial on updating resources in unlocked exe - http://www.codeproject.com/KB/DLL/Modify_UpdateResources_.aspx?msg=3307326
  7. Here's another tutorial with a list of self-deleting methods: http://www.catch22.net/tuts/selfdel The last method (creating a suspended process with random exe like explorer or cmd.exe, then hijacking it) sounds like it would also work for exe modification - you can start cmd.exe in suspended state, then copy your exe image into memory of that process and release your original exe.


Have you read the MSDN (UpdateResource Function) ? It has a list of things don't update your changes. Maybe you are attempting one those.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号