繁体   English   中英

explorer.exe 作为 windows 中的父进程

[英]explorer.exe as the parent process in windows

我正在使用以下命令在cmd杀死一些任务:

taskkill /f /im software* /t

它完成工作并使用该IMAGENAME所有任务; 但是,看看它给了我什么,我看到了一些有趣的东西。 往下看:

 SUCCESS: The process with PID 14712 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 12184 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 16344 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 6816 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 10656 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 14912 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 11908 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 9068 (child process of PID 10060) has been terminated.

因此,正如预期的那样,所有进程都附加到一个集中任务PID = 9068 但是,该进程也是PID = 10060的子进程,它是explorer.exePID 这项工作没有GUI 所以我很惊讶地看到它是explorer的子进程。

那么,这里有一个问题: windows 中的explorer.exe下会有什么样的进程?

更新:

问题:如果我想避免杀死(使用taskkill )任何直接是explorer.exe子进程的进程,我将如何处理?

这是我到目前为止。 首先,我们需要获取explorer.exepid

TASKLIST /NH /FI  "IMAGENAME eq explorer.exe"

然后,我正在考虑编写一个函数,该函数将pid杀死,检查其父进程,如果父 pid 是explorer.exe ,则终止该进程,如果不是,则终止该进程。 这应该全部发生在同一个函数中,以避免处理注释中提到的动态 pid。

我可以使用以下方法找到每个pid的父进程:

wmic process get processid,parentprocessid,executablepath|find "process id goes here")

感谢您对编写函数的帮助。

父进程与子进程是图形、控制台还是服务应用程序无关。 如果 Explorer 是父进程,则仅意味着 Explorer 调用CreateProcess或另一个进程调用CreateProcessCreateProcessAsUser并将 Explorer 的句柄替换为子PROC_THREAD_ATTRIBUTE_PARENT_PROCESSPROC_THREAD_ATTRIBUTE_PARENT_PROCESS

例如,如果用户双击资源管理器中的 .py 脚本图标,shell 会查找文件关联(例如使用IQueryAssociations接口的内部实现)并以脚本为参数执行相应的程序,并使用命令像"C:\\Windows\\py.exe" "path\\to\\script.py" 如果直接执行,Explorer 会将此命令行作为参数调用CreateProcess 如果它“以管理员身份运行”,Explorer 将请求发送到应用程序信息服务,该服务通过CreateProcessAsUser创建提升的进程,通过上述进程创建属性将 Explorer 设置为父进程。

以下.bat脚本可以帮助理解父/子进程及其行为......

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
echo "%~1%~2%~3" | findstr /I "\? \/h \-h">NUL 2>&1
if %errorlevel% EQU 0 goto :usage 

set "_explorerName=explorer.exe"
set "_NameToKill=%~1"
set "_Terminate=%~2"
set "_KillForce=%~3"

if NOT defined _NameToKill if not [%1]==[] (
  rem debugging 
  set "_NameToKill=CMD.EXE"
  rem debugging: start a child process
  start "" /MIN notepad.exe
)

for /F "tokens=1,2 delims=," %%G in ('
    TASKLIST /NH /FI "IMAGENAME eq %_explorerName%"  /FO CSV
') do (
  set "_explorerPID=%%~H"
)
ECHO checking '%_NameToKill%*' as child of %_explorerName% ^(PID=%_explorerPID%^)  
for /F "skip=1 tokens=1-3" %%G in ('
    wmic process WHERE "Name like '%_NameToKill%%%' and ParentProcessId=%_explorerPID%" get Name^,ParentProcessId^,ProcessId^,Status
') do (
  if NOT "%%H"=="" (
    echo %%G
    for /F "skip=1 tokens=1-3" %%g in ('
        2^>NUL wmic process WHERE "ParentProcessId=%%I" get Name^,ParentProcessId^,ProcessId^,Status
    ') do (
      if NOT "%%h"=="" (
        echo    ProcessId=%%i, ParentProcessId=%%h, %%g child of %%G ^(%%I^)
        if defined _Terminate (
          if /I "%%~g"=="conhost.exe" (
            rem debugging
            taskkill /PID %%i /T
          ) else (
            taskkill /PID %%i %_Terminate% %_KillForce%
          )
          echo(
        )
      )
    )
    echo    ParentProcessId=%%H, ProcessId=%%I, %%G child of %_explorerName% ^(%_explorerPID%^)
    if defined _Terminate (
      taskkill /PID %%I /T
      echo(- 
    )
  )
)
if [%1]==[] goto :usage
:endlocal
ENDLOCAL
goto :eof

:usage
  echo USAGE:
  echo %~nx0 -? ^| /? ^| /h ^| -h           this help message
  echo %~nx0                             list all childs of %_explorerName%
  echo %~nx0 ^<processName^> [/T] [/F]     list all childs of a process
  echo %~nx0 ""            [/T] [/F]     a particular test case for cmd.exe
  echo(
  echo Optional parameters /T and /F ^(or -T and -F^): see taskkill /? 
goto :endlocal

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM