News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Self Modifying Program

Started by Darrel, May 04, 2006, 06:31:32 PM

Previous topic - Next topic

Darrel

Need to make a program that modifies the application icon while it is running. What I have is a program that calculates the current phase of the moon. When I have the program run by scheduled tasks I would like it to update its icon to show the correct phase.

Thanks for your time and consideration,

Darrel

Ossa

Hi,

Why not just include several icons as resources (or even external files) - one for each discernable phase - and load the appropriate one when it is needed?

Ossa
Website (very old): ossa.the-wot.co.uk

KSS

Hi, Darrel.
And what trouble with your plan?
Create NEW icon when program need change it icon.
Look in MSDN for CreateIcon.

Darrel

Yes I can place an icon on my desktop and have my program rewrite it at certain intervals. What I would like is to put thr program on the desktop and have it rewrite itself at certain intervals. As it stands right now I don't believe windows will let me rewrite my executable while it is running.

Ossa

Ah, I see what you mean now... and the answer for a simple solution is no. With the application running, the executable cannot be open/altered/deleted. Even if you change the application icon in memory, it will not alter the EXE file icon.

There IS, however, another solution. Write 2 applications, call them A and B. A is the one that will sit on your desktop. B is the one that will alter the icon of A. Here's what happens when A is run (with a blank commandline).


  • A unpacks B from memory (it's included as raw data in the exe file of A) and saves it to disk
  • A runs B and then exits
  • B waits for A to exit and then alters the icon appropriately
  • B runs A with a commandline switch (say /fin) and then exits
  • A waits for B to exit and then deletes B and exits

Overly complex, but the only way that I can quickly think of to do what I think it is that you want to do.

Ossa
Website (very old): ossa.the-wot.co.uk

KSS

Hi, Darrel.
Now understood.

1. You need include sub-program in your EXE (Program that modify your main EXE)
2. When you need — extract sub-program to Windows temp dir and run it with param (path to your main-exe file)
3. EXIT(!!!) with main-exe file.
4. When sub-program start it wait some time (10s) for main program can exit, than sub-program must modify main-EXE and RESET windows icon cache.

Darrel

Hey,  :8)

I see the problem has been correctly explained and solutions appropriately applied. Not what I was looking for but was expected more or less. Sounds like a flaw in the operating system because it should load the file into memory and not care what I do to the file.

Have a good day,

Darrel

MichaelW

I doubt that the behavior is a "flaw", it's probably by design.

Instead of modifying the exe could you use a Shell Extension Handler, specifically an Icon Handler?

eschew obfuscation

jckl

not sure if this would work or not but maybe store the icon that is used in a file. Maybe like an ini file and then when the app is loaded you can have the app set the icon from there. Not sure if it is possible to change the icon while the app is running or not but you could try that aswell so you car changing it plus saving it for when you open the app again.

Synfire

What you are looking for is called polymorphic code and it's well documented if you search for that term (just ignore a lot of the C++/C# results which have a different meaning for polymorphism). One possible solution would be to create a remote thread upon exit, then remote thread shouldl modify the PE/EXE then exit. Unfortunately I don't feel comfort able discussing this because of it's common relation to malware, as well most Anti-Virus will flag your program as a virus if it attempts to modify itself during execution so many people won't run your application if you do this anyways.

Quote
Sounds like a flaw in the operating system because it should load the file into memory and not care what I do to the file.

Actually that is a protection mechinism that ensures other applications won't modify/delete the program while it's running.

asmrixstar

Are you trying to modify the system tray icon or the program exe icon
If its the system tray just a thought here but, you could try this method:
1. make the resource section writeable
2. modify the the icons bits directly
3. then load the original icon again with LoadIcon

Im sure ive seen this method used i at least 2 programs its messy codewise but you only need 1 default icon
It would prolly fall over if you modify the original icon though..

Darrel

"A flaw in the operating system" is a correct statement. Why? I am assuming that the operating system was designed. All programs or operating systems run according to design be the behavior intended or unintended. Anyhow the design of the operating system is implied.

My understanding which may be wrong is when a program is run, it gets loaded into RAM and runs according to what is loaded into RAM and is independent of the program on the hard drive being changed or deleted, hence a flaw in the operating system. Please correct me if my understanding on this analysis is flawed.

dsouza123

In the workshop the original Hard Disk monitor, which runs in the system tray,
switches between icons depending on disk activity.

http://www.masmforum.com/simple/index.php?PHPSESSID=9aab094f5c39baec8f29a2c3dc7ad70e&topic=4163.0

The newer versions do also.

P1

Combine several of your ideas with a Windows Trick.  Keep all icons in the .exe file, but ....

Put a .lnk on the desktop and re-write it when you want the icon to change.  Then refresh the system desktop.

'Search' will get you plenty of .lnk code to have fun with.

Regards,  P1  :8)


MichaelW

Quote
My understanding which may be wrong is when a program is run, it gets loaded into RAM and runs according to what is loaded into RAM and is independent of the program on the hard drive being changed or deleted, hence a flaw in the operating system.

A program in memory does not necessarily run independently of the executable file. For 16-bit DOS programs there were overlays, and for Win32 programs there are resources that are stored in and loaded from the executable file.

I can't really see any good reason to change the application icon while the program is running, but if you must do so, instead of resorting to malware techniques, why not try to use the methods that Microsoft provided?
eschew obfuscation