diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-10-23 12:07:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-23 12:07:35 (GMT) |
commit | 69d0372fc9c5a600ecdfb7dd80f852b26c6ed087 (patch) | |
tree | be1e580765835a7d8a5cf2cb46f4685639d75809 | |
parent | 58b614a327991f4baad4d2795a50027f75411450 (diff) | |
download | cpython-69d0372fc9c5a600ecdfb7dd80f852b26c6ed087.zip cpython-69d0372fc9c5a600ecdfb7dd80f852b26c6ed087.tar.gz cpython-69d0372fc9c5a600ecdfb7dd80f852b26c6ed087.tar.bz2 |
bpo-34980: P/Invoke QueryFullProcessImageName to get process names (GH-9901)
(cherry picked from commit fa5329424f4206630c34f75629fa78738db647f0)
Co-authored-by: Jeremy Kloth <jeremy.kloth@gmail.com>
-rw-r--r-- | PCbuild/pyproject.props | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/PCbuild/pyproject.props b/PCbuild/pyproject.props index 9a096bc..1602ebf 100644 --- a/PCbuild/pyproject.props +++ b/PCbuild/pyproject.props @@ -109,19 +109,34 @@ <FileName Required="true" /> </ParameterGroup> <Task> - <Code Type="Fragment" Language="cs"> + <Using Namespace="System.Diagnostics"/> + <Using Namespace="System.IO"/> + <Using Namespace="System.Runtime.InteropServices"/> + <Using Namespace="System.Text"/> + <Code Type="Method" Language="cs"> <![CDATA[ -string fullPath = System.IO.Path.GetFullPath(FileName); -Log.LogMessage("Looking for " + fullPath, MessageImportance.Normal); -foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { - try { - Log.LogMessage("Found running process: " + p.MainModule.FileName, MessageImportance.Low); - if (fullPath.Equals(System.IO.Path.GetFullPath(p.MainModule.FileName), StringComparison.OrdinalIgnoreCase)) { - Log.LogMessage("Terminating " + p.MainModule.FileName, MessageImportance.High); - p.Kill(); +[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)] +public static extern bool QueryFullProcessImageName([In]IntPtr hProcess, [In]int dwFlags, + [Out]StringBuilder lpExeName, ref int lpdwSize); +public override bool Execute() { + string fullPath = Path.GetFullPath(FileName); + Log.LogMessage("Looking for " + fullPath, MessageImportance.Normal); + foreach (Process p in Process.GetProcesses()) { + try { + int pathLength = 32768; + StringBuilder pathBuilder = new StringBuilder(pathLength); + if (QueryFullProcessImageName(p.Handle, 0, pathBuilder, ref pathLength)) { + string exeName = Path.GetFullPath(pathBuilder.ToString()); + Log.LogMessage("Found running process: " + exeName, MessageImportance.Low); + if (fullPath.Equals(exeName, StringComparison.OrdinalIgnoreCase)) { + Log.LogMessage("Terminating " + exeName, MessageImportance.High); + p.Kill(); + } + } + } catch { } - } catch { } + return true; } ]]> </Code> |