I
I
InqSEO2020-07-28 09:42:35
Delphi
InqSEO, 2020-07-28 09:42:35

How to force terminate the process of another program?

Good afternoon.
I'm not a programmer, I use what I find on the internet, please don't kick too hard. In general, there is a program "A", which has the function of reloading another program "B" (Terminal S MetaTrader4, they periodically freeze and need to be restarted).
Tried to write:

PostMessage(StrToInt(handleChart) , WM_KEYDOWN, 152, 0);

He successfully closes it, but he cannot start it again, because. the terminal hangs in processes and cannot be unloaded (this does not always happen, about 1 out of 10). Until you forcibly remove the process through the dispatcher, you won't be able to start the terminal (L-logic: D).
Therefore, I decided to send a request to force close the terminal via taskkill:
WinExec(PANsiChar('taskkill /F /IM terminal.exe'), SW_HIDE);

Hurray, it works, BUT the terminal can be launched in several instances from different folders and each of them needs to be restarted at different times.
Wrote a function that finds a window by name and then determines the path to the folder. The paths to the folders of each terminal are known. They look like this:
C:\Program Files (x86)\terminal1\terminal.exe
C:\Program Files (x86)\terminal2\terminal.exe

I write the path to the code:
WinExec(PANsiChar('taskkill /F /IM '+accounts[i].MT4_Path), SW_HIDE);

accounts[i].MT4_Path - displays the path to the terminal
Alas, nothing happens. The program does not even swear, just ignore.
Registered a direct path for the test:
WinExec(PANsiChar('taskkill /F /IM C:\Program Files (x86)\terminal1\terminal.exe'), SW_HIDE);

Unfortunately, it's the same.
Where is the mistake? Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hemul GM, 2020-07-28
@InqSEO

Method for terminating a process:
vsokovikov.narod.ru/New_MSDN_API/Process_thread/fn...
Displaying a list of processes and searching for the one you need:
www.delphibasics.info/home/delphibasicssnippets/en...
You can find everything else here:
www.delphibasics. info/home/delphibasicssnippets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question