c++ deleting current program
Hello there. I'm working on a program that can close and delete itself. Is this possible without writing and executing a .bat? I'm working in C++, btw
Not without mallware-like behavior. The program's image is kept open during it's execution so you can't delete it directly.
What you can do:
1. write a bat which deletes the program and then deletes itself (bat files can do that)
2. Use MoveFileEx which deletes files in use on the next reboot
3. Inject yourself into another process and delete your program form there (complicated and dangerous)
For 1 check this link:
http://www.codeproject.com/KB/threads/HowToDeleteCurrentProcess.aspx
What you can do:
1. write a bat which deletes the program and then deletes itself (bat files can do that)
2. Use MoveFileEx which deletes files in use on the next reboot
3. Inject yourself into another process and delete your program form there (complicated and dangerous)
For 1 check this link:
http://www.codeproject.com/KB/threads/HowToDeleteCurrentProcess.aspx
Depends on your operating system. On unix-likes, you may delete open files (which is quite handy for testing purposes, as you can keep open an instance of your application while compiling; especially cool when comparing different rendering algorithms).
To give an example/inspiration of how it could work on a unix-like:
To give an example/inspiration of how it could work on a unix-like:
#include <cstdio>#include <iostream>int main (int argc, char *argv[]) { const bool b = (0 == std::remove (argv[0])); std::cout << "self-delete success: " << (b?"yes":"no") << '\n';}
Quote:Original post by Dizzy_exe
Not without mallware-like behavior. The program's image is kept open during it's execution so you can't delete it directly.
What you can do:
1. write a bat which deletes the program and then deletes itself (bat files can do that)
2. Use MoveFileEx which deletes files in use on the next reboot
3. Inject yourself into another process and delete your program form there (complicated and dangerous)
For 1 check this link:
http://www.codeproject.com/KB/threads/HowToDeleteCurrentProcess.aspx
hmm...I guess I'll stick to MoveFileEx...maybe try the .bat thing too.
Quote:Original post by phresnelDepends on your operating system. On unix-likes, you may delete open files (which is quite handy for testing purposes, as you can keep open an instance of your application while compiling; especially cool when comparing different rendering algorithms).
well, I'm using windows, so don't think this will work....
I have no idea why you would want to do this but i seems like this is not as obscure as I thought since I just ran across some code that will do this in Windows:
An tricky little piece of code that will destroy itself!
An tricky little piece of code that will destroy itself!
Hey ppl! Well, I tried using a .bat file and that works just fine.
MoveFileEx on the other hand doesn't work at all.
Here's the code:
cout is 1, so the function doesn't fail, but the file doesn't get deleted on reboot. Any ideas why?
this can be useful when making an updater and stuff like that.
MoveFileEx on the other hand doesn't work at all.
Here's the code:
char cheese1[MAX_PATH];GetModuleFileNameA(NULL,cheese1,sizeof(cheese1));cout<<MoveFileExA(cheese1,NULL,MOVEFILE_DELAY_UNTIL_REBOOT);
cout is 1, so the function doesn't fail, but the file doesn't get deleted on reboot. Any ideas why?
Quote:Original post by daviangel
I have no idea why you would want to do this but i seems like this is not as obscure as I thought since I just ran across some code that will do this in Windows:
An tricky little piece of code that will destroy itself!
this can be useful when making an updater and stuff like that.
Did you check the registry after you called MoveFileEx? It saves the pending operation in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations
It also only works when you're a member of the administrators group (because you need to write to HKLM).
It also only works when you're a member of the administrators group (because you need to write to HKLM).
You could use ProcessMonitor from Sysinternals and try to find out why the registry isn't modified.
Also, are you running Vista or Windows 7? Applications do not run by default in Vista/Win7 with admin privileges even if the user is an Administrator.
Also, are you running Vista or Windows 7? Applications do not run by default in Vista/Win7 with admin privileges even if the user is an Administrator.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement