News:

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

Terminating a Process

Started by shadow, August 12, 2005, 09:18:26 AM

Previous topic - Next topic

shadow

Hi everyone!  :toothy

I am trying to terminate a process... 

This is what I have:

The process is started by the program started by
invoke ShellExecute,hWnd,NULL,addr MainBuffer1,Parameters,NULL,SW_SHOWDEFAULT

I know the name of the process and where the file that started it is.  Is there any way I can get the process ID to terminate the process in Windows 2000?  :eek 

thanks yet again!

hutch--

Use CreateProcess() and save the process ID and you can do this.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

diablo2oo2

i wrote a procedure for that long time ago...

FindProcessByName.asm
include \masm32\include\comctl32.inc
include \masm32\include\comdlg32.inc
include \masm32\include\masm32.inc
include \masm32\include\ole32.inc
include \masm32\macros\macros.asm

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\shell32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\ole32.lib
includelib \masm32\lib\masm32.lib

FindProcessByName PROTO :DWORD,:DWORD
.const
PROCESS_HANDLE equ 0
PROCESS_ID equ 1

.code
align 4
FindProcessByName proc uses ebx ecx edx esi edi _exename:dword,_returntype:dword
LOCAL Process :PROCESSENTRY32

lea esi,Process
assume esi:ptr PROCESSENTRY32
mov [esi].dwSize, sizeof PROCESSENTRY32
        invoke CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,0
        mov edi,eax
        invoke Process32First,edi,esi
        .while eax!=FALSE
        lea eax,[esi].szExeFile
        invoke lstrcmpi,eax,_exename
        .if eax==0
        ;---found process---
        mov eax,[esi].th32ProcessID
        jmp @return
        .endif
        invoke  Process32Next,edi,esi
        .endw
        @return:
        assume esi:nothing
        push eax
        invoke CloseHandle,edi
        pop eax
        .if _returntype==PROCESS_HANDLE
        invoke OpenProcess,PROCESS_ALL_ACCESS,0,eax ;return hProcess
        .endif
ret
FindProcessByName endp

end


Usage:

invoke FindProcessByName,chr$("test.exe"),PROCESS_HANDLE ;or PROCESS_ID
.if eax!=0
invoke TerminateProcess,eax,0
.endif

shadow

Here's what I did for creatprocess:


RunProgram PROTO :DWORD,:DWORD,:DWORD

.data
pt db "Proxomitron.exe",0
ie db "IEXPLORE.EXE",0
ff db "firefox.exe",0
.data?
handle1 dd ?
handle2 dd ?
handle3 dd ?
handle4 dd ?

pi PROCESS_INFORMATION <>
su STARTUPINFO <>


.code
;...
.if eax==IDC_BTN23
invoke RunProgram,addr pt,NULL,addr handle1
invoke RunProgram,addr ff,NULL,addr handle2

.elseif eax==IDC_BTN24
invoke RunProgram,addr pt,NULL,addr handle3
invoke RunProgram,addr ie,NULL,addr handle4
.elseif eax==IDC_BTN25
invoke TerminateProcess,handle1,0
invoke TerminateProcess,handle2,0
invoke TerminateProcess,handle3,0
invoke TerminateProcess,handle4,0
;...
RunProgram PROC StringName:DWORD,Parameters:DWORD,Handle:DWORD

invoke GetPrivateProfileString,addr Paths,StringName,NULL,addr MainBuffer1,SIZEOF MainBuffer1,addr SettingsPath
invoke CreateProcess,addr MainBuffer1,NULL,NULL,NULL,FALSE,NULL,NULL,NULL,addr su,addr pi
invoke lstrcpy,Handle,addr pi.hProcess

ret
RunProgram endp



:cheekygreen: It works!  Thanks guys!  I might try diablo2oo2's way... it looks interesting.   :wink