14

让我先声明我不是C背景。我是一名 PHP 开发人员。因此,到目前为止,我编写的所有代码都是通过从其他示例中获取点点滴滴并对其进行微调以满足我的要求。所以,如果我问得太基本或明显的问题,请多多包涵。

我开始FFmpeg使用CreateProcess()通过

int startFFmpeg()
{
    snprintf(cmd, sizeof(cmd), "D:\\ffpmeg\bin\ffmpeg.exe -i D:\\video.mpg -r 10 D:\\frames");

    PROCESS_INFORMATION pi;
    STARTUPINFO si={sizeof(si)};
    si.cb = sizeof(STARTUPINFO);
    int ff = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    return ff;
}

我需要做的是获取该PID进程,然后稍后检查它是否在一段时间后仍在运行。这基本上是我正在寻找的:

int main()
{
    int ff = startFFmpeg();
    if(ff)
    {
       // great! FFmpeg is generating frames
       // then some time later
       if(<check if ffmpeg is still running, probably by checking the PID in task manager>) // <-- Need this condition
       {
            // if running, continue
       }
       else
       {
           startFFmpeg();
       }
    } 
  return 0;   
}

我做了一些研究,发现它PID在 中返回PROCESS_INFORMATION,但我找不到显示如何获取它的示例。

一些元数据

操作系统:Windows 7
语言:C
IDE:Dev C++

4

2 回答 2

17

在您的情况下,将其从作为最后一个参数传递给的PROCESS_INFORMATION结构中拉出CreateProcess()pi.dwProcessId

但是,要检查它是否仍在运行,您可能只想等待进程句柄。

static HANDLE startFFmpeg()
{
    snprintf(cmd, sizeof(cmd), "D:\\ffpmeg\bin\ffmpeg.exe -i D:\\video.mpg -r 10 D:\\frames");

    PROCESS_INFORMATION pi = {0};
    STARTUPINFO si = {0};
    si.cb = sizeof(STARTUPINFO);
    if (CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
    {
        CloseHandle(pi.hThread);
        return pi.hProcess;
    }
    return NULL;
}

在您的启动main()中,您可以执行类似...

int main()
{
    HANDLE ff = startFFmpeg();
    if(ff != NULL)
    {
        // wait with periodic checks. this is setup for
        //  half-second checks. configure as you need
        while (WAIT_TIMEOUT == WaitForSingleObject(ff, 500))
        {
            // your wait code goes here.
        }

        // close the handle no matter what else.
        CloseHandle(ff);
    }
    return 0;
}
于 2013-02-22T07:16:06.857 回答
4

您可能想使用 win32 api 功能GetProcessId()

#include <windows.h>

...

BOOL bSuccess = FALSE;
LPTSTR pszCmd = NULL;
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);

pszCmd = ... /* assign something useful */

bSuccess = CreateProcess(NULL, pszCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);

if (bSuccess)
{
  DWORD dwPid = GetProcessId(pi.hProcess);
  ...
}
else
  ... /* erorr handling */

有关详细信息,请参阅此处:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms683215%28v=vs.85%29.aspx

于 2013-02-22T07:15:48.470 回答