How to kill the active process?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
sunkara7
Junior Coder
Posts: 25
Joined: Mon May 23, 2005 1:04 pm

How to kill the active process?

Post by sunkara7 » Tue May 24, 2005 9:25 pm

I am trying to kill the process and i used the following code.

VBSTART
Sub killProcess(pgm)
set wmi = getobject("winmgmts:")
sQuery = "select * from win32_process " & "where name='" & pgm & "'"
set processes = wmi.execquery(sQuery)
for each process in processes
process.terminate
next
End Sub
VBEND
VBRun>KillProcess,vcsv02c.exe

But i opened three windows of the same. So the program is closing all the windows.
But i want to kill the active window only.
How can i kill the active process and leaving the other windows open.

thanks

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Tue May 24, 2005 9:30 pm

What do you mean by active process? Aren't they all active?
MJT Net Support
[email protected]

sunkara7
Junior Coder
Posts: 25
Joined: Mon May 23, 2005 1:04 pm

Post by sunkara7 » Wed May 25, 2005 1:18 pm

support wrote:What do you mean by active process? Aren't they all active?
They are all active. But i want only one process to be killed. How can i modify the code inorder to kill only one process. (the name of the all processes are the same).

Thanks

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Wed May 25, 2005 3:01 pm

If you only want to kill one process you will need to use the process ID not the name - since they all have the same name. Only the PID is unique. E.g:

Select * from Win32_Process Where ProcessID = 2576
MJT Net Support
[email protected]

sunkara7
Junior Coder
Posts: 25
Joined: Mon May 23, 2005 1:04 pm

Post by sunkara7 » Wed May 25, 2005 4:38 pm

support wrote:If you only want to kill one process you will need to use the process ID not the name - since they all have the same name. Only the PID is unique. E.g:

Select * from Win32_Process Where ProcessID = 2576
But we can not hard code the processid because it'll change when we open new window.

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Wed May 25, 2005 4:51 pm

Use GetWindowProcess to get the process ID of the window. See help file topic "GetWindowProcess".
MJT Net Support
[email protected]

sunkara7
Junior Coder
Posts: 25
Joined: Mon May 23, 2005 1:04 pm

Post by sunkara7 » Wed May 25, 2005 6:44 pm

support wrote:Use GetWindowProcess to get the process ID of the window. See help file topic "GetWindowProcess".
I am really bad in coding. Could you please help in the following code in order to kill the process using the processid

GetWindowProcess>Customer Care Desktop*,pid,name

VBSTART
Sub killProcess(pgm)
set wmi = getobject("winmgmts:")
sQuery = "select * from win32_process " & "where ProcessId='" & pgm & "'"
set processes = wmi.execquery(sQuery)
for each process in processes
process.terminate
next
End Sub
VBEND
VBRun>KillProcess,pid

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Thu May 26, 2005 7:54 am

That probably won't help because it will just get the PID of the first window that matches the string - so could be any of your three identical processes (assuming they all have the same window title).

What distinguishes the process you want to kill from the others? Is it the one in the foreground? The one minimized? What? If it is the one in the foreground you could use set WIN_USEHANDLE to 1 to use window handles instead of titles then use GetActiveWindow to get the foreground window handle. Then use GetWindowProcess with the window handle. Since the handle is unique this will ensure you get the ID of the correct process. Then you can use the VBScript to kill that process.

Put your VBSTART/VBEND block before anything else in the script. It's always easier that way.
MJT Net Support
[email protected]

sunkara7
Junior Coder
Posts: 25
Joined: Mon May 23, 2005 1:04 pm

Post by sunkara7 » Thu May 26, 2005 1:19 pm

support wrote:That probably won't help because it will just get the PID of the first window that matches the string - so could be any of your three identical processes (assuming they all have the same window title).

What distinguishes the process you want to kill from the others? Is it the one in the foreground? The one minimized? What? If it is the one in the foreground you could use set WIN_USEHANDLE to 1 to use window handles instead of titles then use GetActiveWindow to get the foreground window handle. Then use GetWindowProcess with the window handle. Since the handle is unique this will ensure you get the ID of the correct process. Then you can use the VBScript to kill that process.

Put your VBSTART/VBEND block before anything else in the script. It's always easier that way.
Thank You.

I've tried that one. It's working. I have one more question to ask

I need to close all child and grand children windows of the main window. I am using WF_TYPE=2 for that. But its not closing the windows at all.

WF_TYPE=2
WindowAction>3,MainWindow

Is this the way i should write the code in order to close the child windows.

Thanks

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts