Kill Process in Windows OS

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
churchken
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 66
Joined: Sun Apr 15, 2007 2:54 pm
Location: Albuquerque, NM USA

Kill Process in Windows OS

Post by churchken » Wed Jan 12, 2011 3:10 pm

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,
Ken

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Kill Process in Windows OS

Post by Klaus » Wed Jan 12, 2011 3:25 pm

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

kray
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 56
Joined: Sat Apr 08, 2006 5:28 pm
Location: Eau Claire, WI
Contact:

Re: Kill Process in Windows OS

Post by kray » Wed Jan 12, 2011 3:41 pm

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...
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com

churchken
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 66
Joined: Sun Apr 15, 2007 2:54 pm
Location: Albuquerque, NM USA

Re: Kill Process in Windows OS

Post by churchken » Wed Jan 12, 2011 4:15 pm

Klaus & Ken -- many thanks for your reply.
Ken

churchken
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 66
Joined: Sun Apr 15, 2007 2:54 pm
Location: Albuquerque, NM USA

Re: Kill Process in Windows OS

Post by churchken » Wed Jan 12, 2011 4:28 pm

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.
Ken

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”