Check running subprocess

0

When my application starts I need to check if a third-party program is running, if it is not, I need to start it.

The problem is that the program done in Java and when it is running it runs inside the Java itself, as in the image:

HowcanIidentifyiftheprogramisrunning?IhavealreadyusedsomemethodsthatIfoundtochecktheprocessesrunningonwindowsonlythemajorityonlyidentifiestheprocess"javaw.exe" and I want to identify the Demander Synchronizer.

Note: I can not search for the title of the window, as it changes

  

I use Delphi RAD Studio XE10.2 Berlin

    
asked by anonymous 07.05.2018 / 15:06

1 answer

1

You can search using the Process32First / Process32Next from windows, doing a loop in existing processes to search for the name of the executable for example. For your case you could do a Java search first, then make a second loop in the child processes (comparing the parent process ID) to see if your application is running.

Example:

uses
  TlHelp32, System.Generics.Collections;


function RetornaPID(exeFileName: string): integer;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := -1;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  while Integer(ContinueLoop) <> 0 do
  begin
    if (UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =  UpperCase(ExeFileName))
    or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName)) then
    begin
      Result := FProcessEntry32.th32ProcessID;
      break;
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

Function RetornaListaprocessosFilhos(aIDProcessoPai: Integer): TList<Integer>;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := TList<Integer>.Create;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, aIDProcessoPai);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  while Integer(ContinueLoop) <> 0 do
  begin
    if FProcessEntry32.th32ParentProcessID = aIDProcessoPai then
    begin
      Result.add(FProcessEntry32.th32ProcessID);
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  vPIDPai: Integer;
  vPIDFilho: Integer;
  vListaProcessosFilhos: TList<Integer>;
begin
  vPIDPai := RetornaPID('explorer.exe');
  if vPIDPai <> -1 then
  begin
    vListaProcessosFilhos := RetornaListaprocessosFilhos(vPIDPai);
    for vPIDFilho in vListaProcessosFilhos do
    begin
      ShowMessage(format('Id do processo filho: %d', [vPIDFilho]));
    end;
    FreeAndNil(vListaProcessosFilhos);
  end
  else
    ShowMessage('process não encontrado');
end;

Process32 ... Reference: DelphiTricks

    
08.05.2018 / 14:21