asp*_*rin 13 c windows winapi pid createprocess
让我首先说明我不是来自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;
}
Run Code Online (Sandbox Code Playgroud)
我需要做的是获取该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;
}
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,发现它PID是在内部返回的PROCESS_INFORMATION,但我找不到一个显示如何获取它的例子.
操作系统:Windows 7
语言:C
IDE:开发C++
Who*_*aig 16
在您的情况下,将它作为最后一个参数传递给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;
}
Run Code Online (Sandbox Code Playgroud)
在你的发布中,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;
}
Run Code Online (Sandbox Code Playgroud)