How to terminate an application from within ATEasy

Knowledge Base Article # Q200119

Read Prior Article Read Next Article
Summary This article explains how to use the Windows API to terminate an application using its executable file name (file.exe).
You can use the following Windows API functions defined in Kernel32.dll to terminate an application using the application's name: Process32First, Process32Next, OpenProcess, GetExitCodeProcess, and TerminateProcess.

First you will need to insert kernel32.dll under Libraries sub-module of the ATEasy module (System, Driver or Program). You can do this by right clicking on Libraries and selecting Insert Library Below. Select the DLL tab and enter the following file path under DLL File Name:
kernel32.dll.  Since the DLL resides in the Windows System folder, the DLL file path can be omitted.

Click Insert.

You will now need to define several functions under the procedures section of the newly inserted kernel32 library as shown below:
How to terminate an application from within ATEasy

You will also need to define a structure under the types section of the kernel32 library as shown below:
How to terminate an application from within ATEasy

And finally, will need to define the following variables in your module:
How to terminate an application from within ATEasy

Now you can enter the code required to scan a list of all open processes and terminate one based on its name. In this example, the first instance of  Wordpad.exe will be terminated.

CreateToolhelp32Snapshot(2, 0)

ProcInfo.dwSize = sizeOf(ProcInfo)

!Get first Process's information
Process32First(lHandle, ProcInfo)

!Loop until wordpad.exe is found or the end of the process list has been reached
repeat

        !Get the exe file name
        sProcName = lcase(ProcInfo.szExeFile)

        !Check if the current process is wordpad.exe
        if (sProcName = "wordpad.exe")

                !If wordpad.exe is found get its ProcessID
                lHandle = OpenProcess(0x1F0FFF,0, ProcInfo.th32ProcessID)

                   !Terminate wordpad.exe
                TerminateProcess(lHandle, -100) ! -100 is user defined exit code
                exitloop

        endif

until not Process32Next(lHandle, ProcInfo)

Article Date 10/7/2008
Keywords TerminateProcess, Kernel32, WinAPI, ATEasy, Terminate, Exit

0 ratings | 0 out of 5
Read Prior Article Read Next Article