Question

i use delphi XE3 i call cmd and open ffmpeg in a thread and pass commands to it

i would like to run 5 thread or more at same time

procedure RunFF.Execute;
var

  SecAtrrs: TSecurityAttributes;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  pCommandLine: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir,result: string;
  Handle: Boolean;
begin
  Result := '';
  workdir :='C:\Users\M\Desktop\ffmpeg\bin';
  with SecAtrrs do begin
    nLength := SizeOf(SecAtrrs);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SecAtrrs, 0);
  try
    with StartupInfo do
    begin
      FillChar(StartupInfo, SizeOf(StartupInfo), 0);
      cb := SizeOf(StartupInfo);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
 //   WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + cmd2),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), StartupInfo, ProcessInfo);

    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, pCommandLine, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            pCommandLine[BytesRead] := #0;
            Result := Result + pCommandLine;
            form3.Memo1.Text := result;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
      finally
        CloseHandle(ProcessInfo.hThread);
        CloseHandle(ProcessInfo.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;


end;

how do i get the PiD of the ffmpeg or cmd so i can kill it !! because when i close my application cmd and ffmpeg keep running ..

Was it helpful?

Solution

The PROCESS_INFO struct that is populated by CreateProcess contains the PID of the new process.

Note that this will not be the PID for the ffmpeg process. It will be the PID of the cmd process. There seems little point using cmd here. You may as well call ffmpeg directly. Then you already have the PID you need in order to kill the process.

I'm not sure how you want to go about killing the process. If you are going to call TerminateProcess, then you need a process handle rather than a PID.

Note that CreateProcess returns a BOOL and not a handle. This variable naming error I have seen before and I expect that somewhere there is some sample code which made the mistake and then everybody that uses it repeats it. Sigh.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top