Page 1 of 1

Kill Process in Windows OS

Posted: Wed Jan 12, 2011 3:10 pm
by churchken
Hi, All,

What is the correct code to kill another Windows application with a LiveCode handler?

Should this work?

on MouseUp
kill process "anotherApp.exe"
end MouseUP

Thanks,

Re: Kill Process in Windows OS

Posted: Wed Jan 12, 2011 3:25 pm
by Klaus
Hi Ken,

I am no expert, but "kill" is definitively no LiveCode reserved word, it sounds more like a "shell" syntax.
So this won't work, I'm afraid.

Best

Klaus

Re: Kill Process in Windows OS

Posted: Wed Jan 12, 2011 3:41 pm
by kray
No, that won't work AFAIK because "kill" only works to kill processes that were launched with "open process" from inside LiveCode. I use a VBScript to do this, based on (usually) the process ID of the executable:

Code: Select all

Dim tResult
Set ProcessSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Process")
tResult = "Error: Process not found"
For each Process in ProcessSet
	If Process.ExecutablePath <> "" Then
		If LCase(Process.Caption)=[[pProcName]] Then
			Process.Terminate (Process.ProcessID)
			tResult = ""
			Exit For
		End If
	End If
Next
result = tResult
Where [[pProcName]] is replaced by the lowercase name of the executable you're trying to match ("calc.exe", "winword.exe", etc.). I store it in a stack custom property (like "uKillProcScript"), and then call it from LC like this:

Code: Select all

on KillProcess pProcName
    put toLower(pProcName) into pProcName  -- just in case
    put the uKillProcScript of this stack into tScript
    put merge(tScript) into tScript  -- replaces the placeholder with the incoming param pProcName
    do tScript as "VBScript"
    if the result <> "" then
        answer "You got an error: " && the result
    end if
end KillProcess pProcName
Funky, but it works...

Re: Kill Process in Windows OS

Posted: Wed Jan 12, 2011 4:15 pm
by churchken
Klaus & Ken -- many thanks for your reply.

Re: Kill Process in Windows OS

Posted: Wed Jan 12, 2011 4:28 pm
by churchken
Klaus & Ken

From a "mouseUP" handler, the following single line of code works in WIndows XP to "kill" another running application:


put shell("taskkill /im myApp.exe") into myresult


The shell command needed the " /im " switch to finally work correctly. The application name may include uppercase characters as long as the name is found among the process list of the task manager.

Thanks for your help.