News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Killing all process but itself (SOLVED)

Started by travism, October 16, 2008, 07:30:29 AM

Previous topic - Next topic

travism

Im trying to kill all process but the calling process, I eventually want it to kill certain programs (ones that keep starting back up) on my other computer, So I dont want it killing itself so for now im trying to figure out a way to not do that. Heres the code I have so far but it just keeps killing itself... :(


; ************************************************************
include \masm32\include\masm32rt.inc
; ************************************************************
.data
  cLoop db 1
.data?
  szMe  dd 120  dup (?)
  szFile db 128  dup (?)
  hSnap HANDLE ?
  PE32    PROCESSENTRY32 <?>
  ME32   MODULEENTRY32   <?>
.code
start:
; ************************************************************
; Create first snapshot then loop
; ************************************************************
         push 0
         push TH32CS_SNAPPROCESS
         call CreateToolhelp32Snapshot

         mov hSnap,eax
         mov [PE32.dwSize],sizeof PE32
         
         push offset PE32
         push hSnap
         call Process32First

; ************************************************************
; Loop through all processes and not kill us
; ************************************************************
         .WHILE cLoop
         mov [ME32.dwSize],sizeof ME32
         push offset ME32
         push hSnap
         call Module32First

; ************************************************************
; Put the exe file name at the end of the path
; ************************************************************
         mov szMe,offset ME32.szExePath
         
         push offset PE32.szExeFile
         push offset szMe
         call lstrcat

; ************************************************************
; Get the file name of us (calling program)
; ************************************************************
         push sizeof szFile
         push offset szFile
         push NULL
         call GetModuleFileName
 
; ************************************************************
; Compare the two to make sure its not us to kill
; ************************************************************
         push offset szMe
         push offset szFile
         call lstrcmpi

; ************************************************************
; If its not kill the process and loop to the next
; ************************************************************
         .IF eax != 0
             push [PE32.th32ProcessID]
             push FALSE
             push PROCESS_TERMINATE
             call OpenProcess
             
             push 0
             push eax
             call TerminateProcess
         .ENDIF
         push offset PE32
         push hSnap
         call Process32Next
         .ENDW
end start


It kills some processes alright and itself... :( Any thoughts?

travism

Heres an updated one... but this one now just dies? I have no idea why.. It raises an exception after the GetModule32First.

; ************************************************************
include \masm32\include\masm32rt.inc
; ************************************************************
.data
  cLoop db 1
.data?
  szFile db 128  dup (?)
  hSnap HANDLE ?
  szMe  LPCTSTR ?
 
  szFilePart LPTSTR ?
  szBuffer    LPTSTR   ?
 
  PE32    PROCESSENTRY32 <?>
  ME32   MODULEENTRY32   <?>
.code
start:
; ************************************************************
; Create first snapshot then loop
; ************************************************************
         push 0
         push TH32CS_SNAPPROCESS
         call CreateToolhelp32Snapshot

         mov hSnap,eax
         mov [PE32.dwSize],sizeof PE32
         
         push offset PE32
         push hSnap
         call Process32First

; ************************************************************
; Loop through all processes and not kill us
; ************************************************************
         .WHILE cLoop
; ************************************************************
; Put the exe file name at the end of the path
; ************************************************************

         mov [ME32.dwSize],sizeof ME32

         push sizeof ME32
         push hSnap
         Call Module32First

         push offset ME32.szExePath
         push offset szBuffer
         call lstrcpy

         push offset PE32.szExeFile
         push offset szBuffer
         call lstrcat

; ************************************************************
; Get the file name of us (calling program)
; ************************************************************
         push sizeof szFile
         push offset szFile
         push NULL
         call GetModuleFileName

; ************************************************************
; Compare the two to make sure its not us to kill
; ************************************************************
         push offset szBuffer
         push offset szFile
         call lstrcmpi

; ************************************************************
; If its not kill the process and loop to the next
; ************************************************************
         .IF eax != 0
             push [PE32.th32ProcessID]
             push FALSE
             push PROCESS_TERMINATE
             call OpenProcess
             
             push 0
             push eax
             call TerminateProcess
         .ENDIF
         push offset PE32
         push hSnap
         call Process32Next
         .ENDW
end start

travism

I actually got it working now.. Heres the working code for anyone searching.

; ************************************************************
include \masm32\include\masm32rt.inc
; ************************************************************
.data
  cLoop db 1
.data?
  szFile     db 128  dup (?)
  szPath   dd 256  dup (?)
 
  hSnap HANDLE ?
  PE32    PROCESSENTRY32 <?>
.code
start:
; ************************************************************
; Get the file name of us (calling program)
; ************************************************************
         push sizeof szFile
         push offset szFile
         push NULL
         call GetModuleFileName

         push offset szPath
         push offset szFile
         call NameFromPath
; ************************************************************
; Create first snapshot then loop
; ************************************************************
         push 0
         push TH32CS_SNAPPROCESS
         call CreateToolhelp32Snapshot

         mov hSnap,eax
         mov [PE32.dwSize],sizeof PE32
         
         push offset PE32
         push hSnap
         call Process32First
; ************************************************************
; Loop through all processes and not kill us
; ************************************************************
         .WHILE cLoop
; ************************************************************
; Compare the two to make sure its not us to kill
; ************************************************************
         push offset PE32.szExeFile
         push offset szPath
         call lstrcmpi
; ************************************************************
; If its not kill the process and loop to the next
; ************************************************************
         .IF eax != 0
             push [PE32.th32ProcessID]
             push FALSE
             push PROCESS_TERMINATE
             call OpenProcess
             
             push 0
             push eax
             call TerminateProcess
         .ENDIF
         push offset PE32
         push hSnap
         call Process32Next
         .ENDW
end start

ic2

#3
Not bad for a new member  :U   For user land apps including office 2007 running on XP Pro SP3 it did the job.  It even let office clean up those temp files it creates.  Not many people notice, but, even that will fail at times when you simply turn off Word Excel or Access, leaving those temp files still on disk.

As far as apps like Window Security Alert and Window update, they simply recreate them self  like fungus.  I don't like them or need them because I rather give that CPU power to other  programs created by other peoples,  shared or commercial.   That's what a real OS is suppose to be all about.


Other than that, Wow  and Congratulation!

PS:...  but it may not be The End. You still may need to do some work with your file.  Killing itself may not be a bad idea.  Open up Taskman to see any left-overs as you test and go from there.

travism

Quote from: ic2 on October 17, 2008, 02:51:02 AM
Not bad for a new member  :U   For user land apps including office 2007 running on XP Pro SP3 it did the job.  It even let office clean up those temp files it creates.  Not many people notice, but, even that will fail at times when you simply turn off Word Excel or Access, leaving those temp files still on disk.

As far as apps like Window Security Alert and Window update, they simply recreate them self  like fungus.  I don't like them or need them because I rather give that CPU power to other  programs created by other peoples,  shared or commercial.   That's what a real OS is suppose to be all about.


Other than that, Wow  and Congratulation!

PS:...  but it may not be The End. You still may need to do some work with your file.  Killing itself may not be a bad idea.  Open up Taskman to see any left-overs as you test and go from there.

Wow thanks! I actually have code now that just compares the certain apps I want and kills them :) works fine that way! but yeah thanks again!