Solved

How to write a self-modifying executable

Posted on 1998-11-19
4
283 Views
Last Modified: 2008-03-04
Is it possible to write an executable which is able to change itself at runtime? Specifically, I'd like to store some customization data in the executable file itself, so that if that exe is passed to someone else, the data can be retrieved.

I'm thinking that it might be possible to modify the exe's resources somehow, but have no idea if that would work as a scheme for persistence.

The essential point to this problem is that the information MUST be stored in the executable, not in the registry or in a separate file.

If the executable cannot modify itself while it is running, can it spawn another process and close itself? Could the other process then go and make the necessary changes to the exe? Just a thought...

Thanks for your help,
Brett
0
Comment
Question by:circuit
4 Comments
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1178206
you can use BeginUpdateResource, ...,  for modifying resource but this function works only on NT 3.51+.

You can spwan another process, close the parent and then in this new process to modify resource.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178207
In windows, unlike DOS,  executables are opened read-only.  This makes this sort of thing nearly impossible to do.  There are ways around it, like using another process to do the writing, but did you ever stop to consider why they have the executable opened read-only?  The reason is that this sort of design tends to be dissasterious on a multi-processing system like windows.  You may be able to circumvent window's protections against writing to an executable, but they you run into the problems that windows is trying to prevent.  For example, what if you have two copies of your program running?  When one is closed so that another process can write to it, the EXE will still be open read only.  You won't be able to write to it.  What happens then?

I recommend you rethink your design.  Is it really that important that this information be written to the EXE?
0
 

Author Comment

by:circuit
ID: 1178208
Sorry, it is most important that I am able to perform this task on windows 95/98.  Thanks for your suggestion though.  

In response to nietod: I don't expect users to run two copies of my executable.  If they try, I can have the second one simply bring the first to top.  And yes, as I said in my post, modifying the executable is cornerstone to the idea of my project.

Thanks,
Brett
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1178209
If you must do it

(a) make a 2nd executable do the writing back to the 1st, while the 1st isn't running
(b) if you don't want to distribute 2 exe's, the 1st exe spits out the 2nd automatically.

e.g.
Program A wants to write to itself, therefore it does
(a) Program A finds where it's running (e.g. GetModuleFileName)
(b) Program A writes out program B to the same directory
(c) Program A kicks off program B ***
(d) Program A ends ***
(e) Program B waits till A has ended
(f) Program B writes to A's disk image
(g) Program B runs A
(h) program B ends ***
(i) Program A waits till B has finished ***
(j) Program A deletes program B disk image

The steps marked *** occur in parallel (c & d, AND,  h & i) hence the "wait" steps to make sure the last program terminated before modifying/deleting it's disk image.

A tip to make spit out stuff easier, is to embed binary resources into your EXE's, you can simply insert them in Visual Studio, or manually include files into your resource files.

Program A would include the binary image of a compiled program B as one of it's resources.

Program A could include additional binary resources (e.g. some data you can easily find)

Program B can then seek to this data, and write to it when working on A's disk image.



0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 64
How to gracefully close the c++ 11 thread? 3 108
Better understanding on C++ Class serialization and formats 9 79
learn programming 8 42
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question