admin管理员组文章数量:1417555
I'm making an app that allows me to "control" the lifespan of an executable.
I have an app with a TStringList
that has the address of several executable files (.exe
). I would like to be able to launch them whenever I want, I already do this with ShellExecute()
, but I don't know how I could close them when they are running.
I'm making an app that allows me to "control" the lifespan of an executable.
I have an app with a TStringList
that has the address of several executable files (.exe
). I would like to be able to launch them whenever I want, I already do this with ShellExecute()
, but I don't know how I could close them when they are running.
1 Answer
Reset to default 4ShellExecute()
does not return anything that can be used to control the launched process.
If you use ShellExecuteEx()
instead, you can specify the SEE_MASK_NOCLOSEPROCESS
flag, and then grab the SHELLEXECUTEINFO.hProcess
field after launch. You can then use GetProcessId()
on that handle to get the process id.
But, it would be better to use CreateProcess()
instead, and grab the returned PROCESS_INFORMATION.dwProcessId
and PROCESS_INFORMATION.hProcess
fields.
Given the process id, you can then use EnumWindows()
and GetWindowThreadProcessId()
to find all of the top-level windows that belong to the process, and then you can send a WM_CLOSE
or WM_QUIT
message to each window. You can then use WaitForSingleObject()
, or GetExitCodeProcess()
in a loop, to monitor the process handle until the process exits or a timeout elapses.
If the process does not exit in a reasonable amount of time, then given the process handle, you can use TerminateProcess()
to brute-force it.
本文标签: delphiRun and close filesexeStack Overflow
版权声明:本文标题:delphi - Run and close files.exe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745272903a2651003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ShellExecute
it is better to useCreateProcess
! The parameterProcessInformation
can be used afterwards to check or control (close) the created processs. – DelphiCoder Commented Jan 31 at 9:29WM_CLOSE
. – AmigoJack Commented Jan 31 at 12:29