VB icon

Have your Exe Delete Itself

Email
Submitted on: 1/4/2015 9:59:00 AM
By: PlanetCPP-Chris(Hound)/Micheal(xix) (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++
Views: 950
 
     Whether your making a setup application, an uninstaller, or for whatever reason, your probably going to be faced with the problem of self deletion. How can you delete your own EXE thus leaving no traces of your program on the computer? You can't use another program that you've written, ..then how do you delete that EXE? It turns out that we can rely on good ol' windows to help us commit Hari Kari, but the technique is different on NT based machines. First I'll show you the code for 9x/ME machines. For those systems you need to add an entry into wininit.ini, this is a initialization file that windows will use on boot. This file resides in your windows directory but might not be present on your computer since it gets renamed to WININIT.BAK after each use. This doesn't concern us since the WritePrivateProfileString function will create whatever is missing, that is the function used to write data to ini files. What you add is the string: NUL=yourexe.exe With the path of course, into the [RENAME] section . After this is called the file will be deleted next time you reboot your machine. Here is the code:
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Have your Exe Delete Itself
// Description:Whether your making a setup application, an uninstaller, or for whatever reason, 
your probably going to be faced with the problem of self deletion. How can you delete
your own EXE thus leaving no traces of your program on the computer? You can't use another
program that you've written, ..then how do you delete that EXE?
	It turns out that we can rely on good ol' windows to help us commit Hari Kari, but the
technique is different on NT based machines. First I'll show you the code for 9x/ME machines.
For those systems you need to add an entry into wininit.ini, this is a initialization file that
windows will use on boot. This file resides in your windows directory but might not be present
on your computer since it gets renamed to WININIT.BAK after each use. This doesn't concern us
since the WritePrivateProfileString function will create whatever is missing, that is the function
used to write data to ini files. What you add is the string:
NUL=yourexe.exe
With the path of course, into the [RENAME] section . After this is called the file will be deleted
next time you reboot your machine. Here is the code:
// By: PlanetCPP-Chris(Hound)/Micheal(xix) (from psc cd)
//**************************************

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
	char shortpath[MAX_PATH] = {""},longpath[MAX_PATH] = {""};
	HMODULE modhandle;
	modhandle = GetModuleHandle(NULL);	//Get module handle of our program
	GetModuleFileName(modhandle,longpath,MAX_PATH);	//Get long path and filename
	//You cannot use the long path and filename, you have to convert them to short
	GetShortPathName(longpath,shortpath,MAX_PATH);	//Convert to short path version
	WritePrivateProfileString("RENAME","NUL",shortpath,"WININIT.INI");	//add to ini file
	cout<<"i've lived a good life, but my time is done here ;o(\n";	//say goodbye
	cout<<"Goodbye... (reboot now to finish me off)\n";
return 0;
}
 /*
If you thought this couldn't get any easier...your wrong, on NT machines it's a simple
one-liner, all you need to use is the MoveFileEx function. This function has a flag called 
MOVEFILE_DELAY_UNTIL_REBOOT, this adds the action into the registry key
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ControlSession Manager\PendingFileRenameOperations
and performs them the next time you reboot. All you need to do is tell it to move your file to NULL.
MoveFileEx(MyExe, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
And that's it! Use the path and filename of your program for MyExe of course.
*/


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.