site
stats
By Anish Pillai Anish Pillai Posted under QTP Concepts

3 different ways to close an application through Task Manager using QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

In this article, we’ll see how to use QTP to close an application through Windows Task Manager. We all know that Windows Task Manager is an application available in Windows Operating Systems which provides detailed information about all the processes/tasks currently running in your system. To manually close any process/task from the task manager, all you have to do is select the task/process in the task manager and use the End Task/End Process button to close the task. Lets see how the same can be achieved using QTP.

QTP provides 3 built-in methods using which you can close a process from Task Manager. All these 3 methods are part of SystemUtil object. These 3 methods use the Process ID, Process Name & Application Window Title to access and then close the process in Task Manager. QTP initially tries to close the process by sending WM_CLOSE message to the process window. If the process is still open after 5 seconds, QTP then terminates the process.

Method Name Description
CloseProcessByName Closes a process based on its name.
CloseProcessById Closes a process according to its Process ID (PID)
CloseProcessByWndTitle Closes a task based on the title of the window

Task Manager Process



Lets see some examples on how to use these SystemUtil methods.

1. CloseProcessByName – This method closes the process based on its image name in the task manager. It returns a long value which represents the number of instances of the application that are closed using this method.

'Open 3 notepad instances
SystemUtil.Run "notepad.exe"
SystemUtil.Run "notepad.exe"
SystemUtil.Run "notepad.exe"

'Close the notepads using the image name
iReturn = SystemUtil.CloseProcessByName("notepad.exe")
msgbox iReturn 'Returns 3 as total 3 notepads are closed


2. CloseProcessById – This method closes a process based on its Process ID and returns a) True if the process was closed successfully b) False if the process was not closed.

Dim pID_Notepad, pID_Excel

'Open notepad and excel applications
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"

'Find Process IDs of both the applications
pID_Notepad = Window("regexpwndtitle:=Notepad").GetROProperty("process id")
pID_Excel = Window("regexpwndtitle:=XLMAIN").GetROProperty("process id")

'Close both the applications
iReturn_N = SystemUtil.CloseProcessById(pID_Notepad)
iReturn_E = SystemUtil.CloseProcessById(pID_Excel)

msgbox iReturn_N 'Returns True
msgbox iReturn_E 'Returns True


3. CloseProcessByWndTitle – This method closes a process based on its window title and returns a long value that represents the number of instances of the applications that are closed.

'Open notepad and excel applications
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"

'Close all the tasks where the window title exactly matches "Untitled - Notepad"
SystemUtil.CloseProcessByWndTitle "Untitled - Notepad", False
'Close all the tasks where the window title contains the string "Excel"
SystemUtil.CloseProcessByWndTitle "Excel", True



Other than these 3 methods, SystemUtil object provides 2 more in-built methods that can be used to close an application. These methods are –

a. CloseDescendentProcesses – This method closes all the processes that are opened by QTP. It returns a long value which indicates the number of instances of the application that are closed using this method.

'Open notepad and excel applications using QTP
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"

'Close all the processes opened using QTP
iReturn = SystemUtil.CloseDescendentProcesses
msgbox iReturn


b. CloseProcessByHwnd – This method closes a process based on its window handle (The window handle can be found out using ‘hwnd’ property). This method returns a) True – if the process is closed successfully, and b) False if the process is not closed.

Dim hwnd_Notepad, hwnd_Excel

'Open notepad and excel applications
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"

'Find Handles of both the application windows
hwnd_Notepad = Window("regexpwndtitle:=Notepad").GetROProperty("hwnd")
hwnd_Excel = Window("regexpwndtitle:=XLMAIN").GetROProperty("hwnd")

'Close both the applications
iReturn_N = SystemUtil.CloseProcessByHwnd(hwnd_Notepad)
iReturn_E = SystemUtil.CloseProcessByHwnd(hwnd_Excel)

msgbox iReturn_N 'Returns True
msgbox iReturn_E 'Returns True

This was all about the SystemUtil built-in methods that can be used to close an application process. Did you like this article? Do let us know using the comments section. Happy reading…

If you enjoyed this article, you can join our blog to get new posts delivered directly in your inbox.


0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • harsh

    good article

  • alex

    pID_Excel = Window("regexpwndtitle:=XLMAIN").GetROProperty("process id")
    'Close the application

    iReturn_E = SystemUtil.CloseProcessById(pID_Excel)

    This 2 lines produces error (Cannot identify the object "[ Window ]" (of class Window).), I tried excel.exe also. Any idea?

  • vamsi krishna

    Useful info in the article.. But qtp is taking more time when I try to close excel process in task manager.

    I tried it to kill by the step
    SystemUtil.CloseProcessByName "excel.exe"

    Can you please help on this

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×