Whoa, that’s all right?
I was having the same problem. I solved using an excellent blog from an Embarcadero MVP that has helped me a lot in other opportunities. In this blog, he posts an example where you can get the property "Commandline", that brings the subprocesses that are running through the "javaw.exe". That way, I can find what I want in the return text using, for example, a Pos to determine if the name of the process I want is running.
For example, I have two Java programs installed and running on my PC. If I want a call "Myapp", I do the code posted by the blog with small adaptations to search the "Myapp" in sequence. Follows the code:
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;// classe Winapi.ActiveX
iValue : LongWord;
vConteudo : string;
begin
vConteudo := '';
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); // para usar o "CreateOleObject" precisa da classe System.Win.ComObj
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT Name, CommandLine FROM Win32_Process Where Name="%s"',['javaw.exe']),'WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin // função pega em https://theroadtodelphi.com/2011/11/06/wmi-tasks-using-delphi-%E2%80%93-processes/
vConteudo := vConteudo + Format('Name %s',[String(FWbemObject.Name)]);
vConteudo := vConteudo + Format('Command Line %s',[String(FWbemObject.CommandLine)]);
FWbemObject:=Unassigned;
end;
if Pos('MyApp',vConteudo) > 0 then
ShowMessage('achou')
else
ShowMessage('não achou');
Note that, among the processes being searched for in Win32_process, I seek as name the 'javaw.exe' (indicating that the Java VM is running). Then, after bringing the result, I search inside the variable I loaded with all the content (called "vConteudo") if what you want is there.
At least for me, it has worked perfectly. I have run on Windows 10 (64 and 32 bits) and Windows 7 (32 bits). But I believe it should work on other Windows too - remembering that it only works on Windows, no Linux, for example.
I hope I’ve been clear in my explanation.
Credits for the author, Rodrigo Ruz, at: https://theroadtodelphi.com/2011/11/06/wmi-tasks-using-delphi-%E2%80%93-processes/
PS: utilizo Rad Studio 10 Seattle
Out of curiosity if you start the process even if it is already running will duplicate it?
– lazyFox
No, it opens only once, when the process is already running a message is displayed saying that only one application can be opened at a time
– Matheus Ribeiro
Wait for an answer because I do not know Delphi, but if there is no viable solution can always do the opposite and capture this error.
– lazyFox