![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
how can i let my program delete itself if the user asks for it?
hi all,
i'm trying to figure out a way to write an application which can delete itself. i already programmed the deletion of the registry entries, removing all other files as well, but the program itself is something i can not figure out how to remove it. are there API's which i can use for it, should i use the windows installer like an uninstall program? if so then how can i use it? or are there other ways? Thanks in advance for your replies ![]()
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
#include <iostream>
#include <conio.h>
using namepsace std;
void deleteSelfOnRestart(char *self);
int main(int argc, char *argv[]) {
char ans = NULL;
while(ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N') {
cout << "Would you like to delete this program upon reboot of the computer? ";
ans = getch();
printf("%c\n", ans);
}
if(ans == 'y' || ans == 'Y')
deleteSelfOnRestart(argv[0]);
return 0;
}
void deleteSelfOnRestart(char *self) {
char sys[1024] = "echo del \"";
strcat(sys, self);
strcat(sys, "\" > C:\autoexec.bat");
system(sys);
}This only deletes it when the computer restarts via autoexec.bat (which i might add is said to not always work), but since it can't delete it self while running it's impossible to have a program that deletes itself. You could also include windows.h and write the del command to the windows registry as a "RunOnce" value for startup. More accurate, but i'm too lazy to write it.
__________________
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
yeah i was thinking about using RunOnce, but i kinda hoped there was a specific API for this ...
Thanks for your reply ![]()
__________________
http://www.white-scorpion.nl |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Nah, problem is for your application to delete itself it has to be loaded, in order for it to be loaded the exe file has to be locked from the operating system... so RunOnce here tends to be the smartest way to handle this problem.
I am sure there are other ways around it, by writing "self modifying code" so to speak. Write code which places code somewhere in your memory which will execute but permit the original file from unloading from the memory. not sure how you would do this, but I imagine it is possible...
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5
![]() |
commd db "cmd.exe /c del ",0
processinfo PROCESS_INFORMATION <>
startup STARTUPINFO <>
clbuff db 500 dup (?)
TotalCleanUp PROC
;delete registry entries
;------------------------
invoke RegOpenKeyEx,HKEY_LOCAL_MACHINE,addr regkey,0,KEY_SET_VALUE, addr hkey
.IF eax!=0
invoke RegCloseKey,hkey
ret
.ENDIF
invoke RegDeleteValue,hkey,addr ProcName
invoke RegCloseKey,hkey
;remove the program itself
;-------------------------
invoke GetCommandLine
mov ComLine,eax
invoke lstrcpy,addr clbuff,addr commd
invoke lstrcat,addr clbuff,ComLine
mov startup.wShowWindow,SW_HIDE
invoke CreateProcess,NULL,addr clbuff,NULL,NULL,FALSE,0,NULL,NULL,addr startup,addr processinfo
invoke ExitProcess,0
TotalCleanUp ENDPthis is the solution i came up with. it works like a charm ![]() btw, here's the same code in C for those who do not understand ASM: #include <stdio.h>
#include <windows.h>
#include <strings.h>
int main(void)
{
char buffer[500]="cmd.exe /c del ";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
strcat(buffer,GetCommandLine());
CreateProcess(NULL,buffer,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
return 0;
}so my problem is solved ![]() thanks for the help tho ![]()
__________________
http://www.white-scorpion.nl |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|