Link to home
Start Free TrialLog in
Avatar of YossiBa
YossiBa

asked on

Killing exe file running process

Hi Helper,
In my vb application i use CreateProcessA API function to run an exe file.
I would like to know if this is possible to terminate the process after it allready began.
Do i have controll on the application after the exe file running process has began?
(Shell is another function for executing files that i use).
Thank you
Yossi
Avatar of Ark
Ark
Flag of Russian Federation image

If you're using CreateProcess API:

Call TerminateProcess(pi.hProcess, exitCode)
Call CloseHandle(pi.hProcess) 'if not closed yet

where pi is PROCESS_INFORMATION structure used by CreateProcess API.

If you're using Shell:
pid = Shell....
PostMessage PidToHwnd(pid), WM_CLOSE, 0, 0

Public Function PidToHwnd(ByVal target_pid As Long) As Long
  Dim test_hwnd As Long
  Dim test_pid As Long
  test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
  Do While test_hwnd <> 0
   If GetParent(test_hwnd) = 0 Then
      Call GetWindowThreadProcessId(test_hwnd, test_pid)
      If test_pid = target_pid Then
         PidToHwnd = test_hwnd
         Exit Do
      End If
   End If
   test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
  Loop
End Function

Cheers
Avatar of YossiBa
YossiBa

ASKER

Hi Ark,
Thank you for the solution, the thing is i need to know how can the user stop the exe file that was executed by the Shell function in the middle (let say if the exe file produces many report sheets that you want to interrupt).
Thanks again
Yossi
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial