Reputation: 31
All,
I store my application settings in a resource. When my program first loads, I read the specified resource using WinAPI. 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.
Upvotes: 2
Views: 5363
Reputation: 76
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.
Upvotes: 6
Reputation: 2200
Upvotes: 1
Reputation: 3929
Have you read the MSDN (UpdateResource Function) ? It has a list of things don't update your changes. Maybe you are attempting one those.
Upvotes: 0