Killing a Process Created By WinExec
Kevin E.
Loveland, CO

Feb 19, 2014
4 Posts

Killing a process created by WinExec

I'm using the internal WinExec procedure to launch a helper application required by my ATEasy test application.  The helper application is launched successfully.  

As part of the cleanup of my test application I want to terminate the helper application.  I've tried using the Win32 kernel function TerminateProcess, passing the AHandle returned from WinExec as (hopefully) the process identifier required by TerminateProcess.  Unfortunately, the helper application is not terminated.

Any ideas or suggestions?

Cyril P.
Meung Sur Loire, Loiret

Feb 19, 2014
19 Posts

Re: Killing a process created by WinExec

Hi Kevin,

as you have seen, the handle expected by win32 function is not the same the WinExec Ateasy return.

Here is my own procedure I wrote to kill a process by its name.
I pass the variable "sProcessNameToKill" to my procedure (val string)

! Get the array containing the process id's for each process object
! calculate how many process IDs were returned

! init var

for n=1 to (wNbProcess)
    ! Get a handle to the Process
    hProcess=kernel32.OpenProcess((kernel32.PROCESS_QUERY_INFORMATION OR kernel32.PROCESS_VM_READ OR kernel32.PROCESS_TERMINATE),0,adwProcessIDs[n])
    ! Iterate through each process with an ID that <> 0
    if (hProcess <> 0) then
        if (Pos(LCase(sProcessNameToKill),LCase(sProcessName),0) >= 0) then
            ! terminate the process
    ! Close the handle to the process

return bStatus

Hope this help.

Cyril P.
Meung Sur Loire, Loiret

Feb 19, 2014
19 Posts

Re: Killing a process created by WinExec

I have attached a system with the procedure and windows api.

File Attachment:

Solution Available
Paul T.
Bacau, Romania

Feb 19, 2014
39 Posts

Re: Killing a process created by WinExec

Things worked simple for me some time ago (under Windows 7). I was launching external applications with hProcess = WinExec(...), after which I was terminating them with TerminateProcess(hProcess, lExitCode).

TerminateProcess is defined within Kernel32, with hProcess - Val AHandle, and lExitCode - Var Long. lExitCode wasn't of any concern for me.

I hope it will work for you too, Kevin.

Best regards,


Please Note
You need to have a M@GIC account to participate in the Forums.
Not yet registered on our website? Click here to register today!

All content, information and opinions presented on the Marvin Test Solutions User Forums are those of the authors of the posts and messages and not Marvin Test Solutions'. All attachments and files are downloaded at your own risk. [Read More]