首页 > 解决方案 > 如何使用 nim 语言在 Windows 上处理 taskkilll 命令?

问题描述

有没有办法处理Windows在没有选项的进程上使用WM_CLOSE时发送的事件?ffmpeg 并非旨在捕获此事件,我想编写一个可以管理 ffmpeg 进程并告诉它在收到事件时正常关闭的程序。我对 Windows API 非常陌生,所以我不知道从哪里开始。taskkill/FnimWM_CLOSEnim

这是一个显示 ffmpeg https://imgur.com/a/JxGjiiX问题的 gif

如果我使用该选项强行终止 ffmpeg 进程,/F它将在编码/录制期间以文件损坏告终。

这是我用来测试此功能的代码:

import os, times, strutils


let t = epochTime()

proc handler() {.noconv.} =
  echo "Program has run for ", formatFloat(epochTime() - t, precision = 0), " seconds."
  quit(0)

setControlCHook(handler)

while true:
  sleep 500

这不会捕获WM_CLOSEtaskkill.

我怎样才能让我的nim程序捕捉到WM_CLOSE事件?

标签: windowswinapinim-lang

解决方案


问题

如果没有与进程关联的窗口句柄,则在没有标志的情况下使用它时无法捕获发送的WM_CLOSE事件。taskkill/F

向没有窗口的进程发送 WM_CLOSE 消息

taskkill,当不带标志使用时,向与您调用时使用的 pid 关联的窗口句柄/F发送WM_CLOSE或消息WM_QUITtaskkill

taskkill 和 taskkill /f 的区别

请注意,我在 Windows 10 上,所以我必须 catchWM_CLOSE而不是WM_QUIT

您可以通过打开命令提示符并运行 ffmpeg 自己测试问题,然后尝试使用taskkill /PID {pid}taskkill /IM ffmpeg.exe. ffmpeg 将继续编码/记录并taskkill告诉你它成功地终止了进程。

ffmpeg.exe -rtbufsize 150M -f gdigrab -framerate 30 -offset_x 448 -offset_y 240 -video_size 1024x600 -draw_mouse 1 -show_region 1 -i desktop -r 30 -preset ultrafast -tune zerolatency -movflags +faststart output.mp4

这是一个尝试关闭 ffmpeg 进程的演示taskkill。的默认行为taskkill是未处理的,因为 ffmpeg 不会创建可以将WM_CLOSE事件发送给它的窗口句柄。

https://imgur.com/a/JxGjiiX

^ 我使用我的自定义 ffmpeg 包装器来捕捉WM_CLOSE事件并优雅地关闭 ffmpeg 以记录此剪辑。

解决方案

将窗口与 Windows 上的任何可执行文件关联的一种方法是使用 start 命令:

start "Your window title" program.exe -option1 -option2

然后您可以使用taskkill发送WM_CLOSE事件,该事件可以被拦截,但 ffmpeg 实际上并没有捕获该事件,因为它最初并不是为了捕获它而设计的。

为了解决这个问题nim,您可以使用 3 个附加模块的组合来创建窗口句柄、启动/监视进程、拦截WM_CLOSE事件并将“q”写入 ffmpeg 进程的stdout.

尼姆

温尼姆

宁皮

在其他版本的 Windows 中taskkill发送WM_QUIT事件,因此您可能也需要处理它。

wNim用;创建窗口句柄 winim仅用于访问用于事件处理程序的WM_CLOSE常量。wNim

import os
import wnim
import winim
import nimpy

# Put python3.8.dll into following folder.  It is required by nimpy
# C:/Users/username/AppData/Local/Microsoft/WindowsApps

let app = App()
let frame = Frame(title="ffmpeg", size=(400, 300)) # Size doesn't matter because we never show the frame.

使用nimpy加载python3.8.dll文件,以便您可以使用python 中的模块。(比内置的多处理库恕我直言更易于使用)subprocessnim

# This is used to check if ffmpeg has been launched
var running_process = false  

# The process must be a PyObject, not a Process object from nim
var the_proc: PyObject  

# Get reference to the subprocess module
let subprocess = pyImport("subprocess")  

# Get reference to python builtin modules
let py = pyBuiltinsModule()  

# Get a reference to the subprocess PIPE
let pipe = subprocess.PIPE  

创建事件处理程序。这些将处理应用程序的首次启动和WM_CLOSEWindows 中的事件。

# We catch the WM_MOVE event and start the process.
# We force the event to trigger when we center 
# the frame and start the app.
frame.connect(WM_MOVE) do (event: wEvent): 
  # Make sure we only start 1 process.
  if running_process == false:
    running_process = true
    
    # Create command and start the process.
    var command_str: string = paramStr(1)
    for i in 2..paramCount():
      command_str = command_str & " " & paramStr(i)
    
    # Use python subprocess module to execute command and set the stdin to the pipe.
    the_proc = subprocess.Popen(command_str, stdin=pipe)

# When WM_CLOSE is triggered from taskkill, write the utf-8 encoded "q"
# to the ffmpeg process
frame.connect(WM_CLOSE) do (event: wEvent):
  var command = "q"
  # Make sure objects are all safe to use
  # They should by python objects. Call the 
  # standard library to create the python objects.
  var pycommand = py.str.encode(py.str(command), "utf-8")

  # With a python process you call `communicate` on 
  # the process when you want it to wait to complete.
  # Since ffmpeg just needs the "q" character we send
  # the utf-8 encoded "q" with the `input` keyword
  discard the_proc.communicate(input=pycommand)
  sleep(1000)

  # Get rid of process and quit.
  discard the_proc.terminate()
  quit(0)

然后所有需要做的就是使框架居中并启动应用程序。

frame.center()
app.mainLoop()

请记住,这使用了 python 标准库,因此您需要将 python dll 放入以下文件夹nimpy以获取访问权限。

C:/Users/username/AppData/Local/Microsoft/WindowsApps

如果您碰巧遇到相同的问题,这里有一个存储库,其中包含您需要的一切:

https://github.com/Wykleph/ffmpeg_wrapper


推荐阅读