Can this program be written?
I came across this problem in one of my test papers.
It says : Write a program that deletes itself( the exe file ) when run.
Here's my first attempt and it didn't work, so I added the error-checking code.
/* This program tries to delete itself */
#include <stdio.h>
#include <conio.h>
#include <errno.h>
int main( int argc, char *argv[] )
{
printf("This program will delete itself. Press a key to continue...");
getch();
if( remove( argv[0] )) // remove() returns non-zero on error
{
printf("\nSome error has occcurred. Couldn't delete file. Error :");
switch( errno )
{
case EACCES : printf("Permission denied");
break;
default : printf("Unknown error.");
}
}
else
printf("\nFile %s deleted", argv[0] );
getch();
return 0;
}
The output is always "Permission Denied". Is there any other way to write this program?
vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
one way would be to write into autoexec.bat to delete the program, then reboot the computer.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Thanks. If its an OS issue, that's not my problem. :)
vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
Now that is a REALLY clever way, probably a bit too much. I was looking for somwthing simpler.
vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
I tried that. I doesn't work. The o/p is still the same. If the problem lies with the OS, I guess there isn't much we can do.
vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
I'd say it's platform independant. Most platforms have a lock on files that are in use, and you can't delete them:
#include <cstdlib>
#include <cstdio>
int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}
If that doesn't work, then as someone already mentioned, then it's got a lock on the file.
Maybe if you could find a renowned virus writer the question could be answered ;)
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Try this for Windows. It writes itself a Batch File and runs it. After the Exe File is deleted, the batch file also deletes itself. A reboot isnt needed here.
#include <iostream>
#include <fstream.h>
#include <windows.h>
#include <shellapi.h>
int main( int argc, char* argv[] )
{
char ExeFileName[ 260 ] = "";
char BatFileName[ 260 ] = "";
strncpy( ExeFileName, argv[ 0 ], strlen( argv[ 0 ] ) + 1);
strncpy( BatFileName, argv[ 0 ], strlen( argv[ 0 ] ) + 1 );
char* SlashPos = strrchr( BatFileName, '\\' );
strncpy( SlashPos, "\\BatFileName.bat", strlen("\\BatFileName.bat" ) + 1 );
ofstream BatFile(BatFileName);
BatFile << ":Begin" << "\n";
BatFile << "del " << ExeFileName << "\n";
BatFile << "if exist " << ExeFileName << " goto Begin" << "\n";
BatFile << "del " << BatFileName << "\n";
BatFile.close();
ShellExecute(NULL, "open", BatFileName, NULL, NULL, SW_HIDE);
return 0;
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Try this for Windows. It writes itself a Batch File and runs it. After the Exe File is deleted, the batch file also deletes itself. A reboot isnt needed here.
#include <iostream>
#include <fstream.h>
#include <windows.h>
#include <shellapi.h>
int main( int argc, char* argv[] )
{
char ExeFileName[ 260 ] = "";
char BatFileName[ 260 ] = "";
strncpy( ExeFileName, argv[ 0 ], strlen( argv[ 0 ] ) + 1);
strncpy( BatFileName, argv[ 0 ], strlen( argv[ 0 ] ) + 1 );
char* SlashPos = strrchr( BatFileName, '\\' );
strncpy( SlashPos, "\\BatFileName.bat", strlen("\\BatFileName.bat" ) + 1 );
ofstream BatFile(BatFileName);
BatFile << ":Begin" << "\n";
BatFile << "del " << ExeFileName << "\n";
BatFile << "if exist " << ExeFileName << " goto Begin" << "\n";
BatFile << "del " << BatFileName << "\n";
BatFile.close();
ShellExecute(NULL, "open", BatFileName, NULL, NULL, SW_HIDE);
return 0;
}
That's pretty clever ;)
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Thanks, but don't know much about the windows API. I'm pretty sure it works...so thanks again :)
vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
Hey, that works! Thanks a lot. But I didn't understand the ShellExecute() function. Does it run after the program terminates?
vicky_dev
Junior Poster in Training
86 posts since May 2005
Reputation Points: 28
Solved Threads: 2
The ShellExecute API is used to perform an operation on a specifed file. In this case the "open" operation. After performing the desired operation, it returns to the calling function. It does not wait till the batch file commands have finished running.
Executing the batch file using the system function did not work because the system function did not return to the calling function ( main )while the batch commands are running.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115