HELP!! deleting running EXE (code included) 
Author Message
 HELP!! deleting running EXE (code included)

hi, i am trying to delete a running executable. i've changed the
characteristics field of the EXE in the Win32 PE File Header to include the
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP flag
( http://www.*-*-*.com/ ) but it still
doesnt work. any idea? thanks!!

code to change the characteristics flag:
    char buf[]="C:\\Documents and
Settings\\Administrator\\Desktop\\imghlptst\\hello\\Debug\\hello.exe";
    WORD wChar = 0;
    DWORD dwRead;
    DWORD signature_offset;
    BOOL b;
    DWORD char_offset;

    HANDLE file =
CreateFile(buf,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FI
LE_ATTRIBUTE_NORMAL,NULL);
    SetFilePointer(file,0x3c,0,FILE_BEGIN);
    b = ReadFile(file,&signature_offset,4,&dwRead,0);
    char_offset = signature_offset+4+18;
    SetFilePointer(file,char_offset,0,FILE_BEGIN);
    b = ReadFile(file,&wChar,2,&dwRead,0);
    wChar = wChar | IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
    SetFilePointer(file,char_offset,0,FILE_BEGIN);
    b = WriteFile(file,&wChar,2,&dwRead,0);

code for hello.exe:

int main(int argc, char* argv[])
{
        BOOL b = DeleteFile("hello.exe");
    if (b)
        printf("delete ok\n");
    else
        printf("delete failed\n");
    printf("err %d\n",GetLastError());
    getchar();
        return 0;

Quote:
}



Wed, 01 Oct 2003 13:20:27 GMT  
 HELP!! deleting running EXE (code included)
A really cheesy, but effective, way to do this is:

(1) Have your program use CreateFile() to create a batch file
(2) Into the batch file write the following:

    :again
    del hello.exe
    if exist hello.exe goto again

(3) Use CreateProcess() to run that batch file, low priority, hidden window
(4) Let your application close normally. When it does, *poof*, it will be
deleted.

--Richard



Thu, 02 Oct 2003 01:53:42 GMT  
 HELP!! deleting running EXE (code included)
but i want to delete the EXE while it's still running...
thanks...


Quote:
> A really cheesy, but effective, way to do this is:

> (1) Have your program use CreateFile() to create a batch file
> (2) Into the batch file write the following:

>     :again
>     del hello.exe
>     if exist hello.exe goto again

> (3) Use CreateProcess() to run that batch file, low priority, hidden
window
> (4) Let your application close normally. When it does, *poof*, it will be
> deleted.

> --Richard



Fri, 03 Oct 2003 09:14:00 GMT  
 HELP!! deleting running EXE (code included)
That, I'm afraid, would be problematic at best.  Windows protects the disk
image of a currently running executable as to avoid deletion because it uses
the disk image for its virtual memory swapping system.  I suppose that you
could do it with some low-level APIs, but I'd be interested to see if you
could find a way to do this with user mode code.

Regards,
Jackson Yee
http://jacksonyee.welcome.to/
E-mails for help will be ignored unless I had asked for them.  Please post
to the group.


Quote:
> but i want to delete the EXE while it's still running...
> thanks...



> > A really cheesy, but effective, way to do this is:

> > (1) Have your program use CreateFile() to create a batch file
> > (2) Into the batch file write the following:

> >     :again
> >     del hello.exe
> >     if exist hello.exe goto again

> > (3) Use CreateProcess() to run that batch file, low priority, hidden
> window
> > (4) Let your application close normally. When it does, *poof*, it will
be
> > deleted.

> > --Richard



Fri, 03 Oct 2003 22:58:10 GMT  
 HELP!! deleting running EXE (code included)
The best way ive seen this done is the following:
(note that this does delete the image but just after a call to ExitProcess
is made) ...

HMODULE module = {0};
 TCHAR szFileBuf[MAX_PATH] = {0};
    GetModuleFileName((HMODULE)g_hInstance, szFileBuf, sizeof( szFileBuf ));
 module = GetModuleHandle(szFileBuf);

 __asm
 {
   lea     eax, szFileBuf
   push    0
   push    0
   push    eax
   push    ExitProcess
   push    module
   push    DeleteFile
   push    UnmapViewOfFile
   ret
 }

also worth looking at:
Q & A Win32, January 1996 MSJ


Quote:
> That, I'm afraid, would be problematic at best.  Windows protects the disk
> image of a currently running executable as to avoid deletion because it
uses
> the disk image for its virtual memory swapping system.  I suppose that you
> could do it with some low-level APIs, but I'd be interested to see if you
> could find a way to do this with user mode code.

> Regards,
> Jackson Yee
> http://jacksonyee.welcome.to/
> E-mails for help will be ignored unless I had asked for them.  Please post
> to the group.



> > but i want to delete the EXE while it's still running...
> > thanks...



> > > A really cheesy, but effective, way to do this is:

> > > (1) Have your program use CreateFile() to create a batch file
> > > (2) Into the batch file write the following:

> > >     :again
> > >     del hello.exe
> > >     if exist hello.exe goto again

> > > (3) Use CreateProcess() to run that batch file, low priority, hidden
> > window
> > > (4) Let your application close normally. When it does, *poof*, it will
> be
> > > deleted.

> > > --Richard



Sat, 04 Oct 2003 04:08:35 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. deleting running exe

2. function to run help.exe from menu->help

3. Including an exe inside another exe

4. HELP: Identifying DLLs used by a program to include in a Setup.Exe program

5. Running an exe from another exe

6. Security - help - local exe won't run off network share

7. Help? Run setup.exe and Wait

8. Help:EXE runs on some comp's, not others

9. Designer Serialization - please help! (Sample code included)

10. help with printf not printing (small code included)

11. Trouble Appending Files, code included can anyone help?

12. HELP! (Code example included)

 

 
Powered by phpBB® Forum Software