using delphi XE I am trying to execute an exe file multiple times with different parameters but i will need to close/restart each one separately for various reasons.
so i thought if i start that example.exe and get its pid
i will be able to kill it later using that unique pid value.
see if i simply execute the example.exe THEN try to get the PID of that process using process name or the process file path it will end up giving me wrong result because there are like 4 processes with that name.
any suggestions or ideas ? my question might seem similar to some others but i need to return the pid value so keep that in mind
Use the Win32 API CreateProcess()
function. It outputs a PROCESS_INFORMATION
struct that contains the IDs and handles of the launched process and its main thread. You can use the process handle to wait for the process to exit.
To terminate the process, you can pass the process handle to TerminateProcess()
.
Or, you can be more civil and:
EnumWindows()
or EnumThreadWindows()
, posting a WM_CLOSE
message to each one.And/Or:
WM_QUIT
message to the main thread.If that does not work, then use TerminateProcess()
as a last resort.