Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 30th, 2005, 5:12 PM   #1
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
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
lepricaun is offline   Reply With Quote
Old Jan 30th, 2005, 5:21 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#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.
__________________

tempest is offline   Reply With Quote
Old Jan 31st, 2005, 1:48 AM   #3
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
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
lepricaun is offline   Reply With Quote
Old Jan 31st, 2005, 3:37 AM   #4
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
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 &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Jan 31st, 2005, 3:02 PM   #5
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
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 ENDP

this 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
lepricaun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC