working with UCI chess engine is different, the engine itself must not be stopped.
This demo does what you want. This is the code that I used for my
UCI chess GUI.
Otherwise, there is also a demo which does what you want in
UnTerminal.
Yet another solution
here.
Otherwise, I have a unit based on Windows API which does the same thing. I can give it to you if you are interested, but it is better to have a multiplatform solution, isn't it?
P.-S. In case someone is interested, I attach it here.