Ok, I know that this is a common question already adressed in other threads but please let me add some context:
I'm working on a server-client apllication which provides a CLI, I need two instances of this application working first instance acts as server and second instance acts as backup server; both servers are connected through a TCP sockets. In certain situations the role of server and backup server can swap, this can happen writting a swap instruction on the CLI.
I'm connecting to the CLI using PuTTY. The PuTTY instance is opened inside the application with a call to CreateProcess
:
STARTUPINFO si = { sizeof(STARTUPINFO) };
// ...
process_information = PROCESS_INFORMATION();
BOOL success = CreateProcess
(
NULL,
command, // putty.exe param1 param2
NULL,
NULL,
TRUE,
DETACHED_PROCESS,
NULL,
static_cast<LPCWSTR>(workingDirectory),
&si,
&process_information
);
Doing this call the PuTTY process appears as child of my application process both on task manager and Process Explorer.
But I have a problem with this approach: for some unknown reason if the PuTTY console is open when the server becomes backup server it maintains the TCP listener open and after swapping again it creates a new listener on the same port, then the backup server connects to the previous TCP listener which is not managed by the application anymore.
Here is the catch:
- If I close the PuTTY console the zombie TCP listener closes and then the connection is stablished with the correct listener.
- If I open the PuTTY console manually (instead of calling
CreateProcess
from inside my application) the TCP listener closes propperly and swapping server roles works perfectly.
Until I figure out why opening the PuTTY instance from CreateProccess
makes my TCP connections behave this way I want to open the PuTTY as if it was NOT opened from within my application.
What I've tried so far:
- Create a batch file that calls PuTTY and
CreateProcess
the batch file (failed). - Create a job with
CreateJobObject
and assign the PuTTY process to that job (failed).
The reason behind the #1 try is that I've read that you can create a process (A) that creates a second process (B) and then finishing the first process (A) leaves it orpah, but I wasn't able to make the batch file run at all.
Any help is appreciated, thanks for your time.
CodePudding user response:
ShellExecute, ShellExecuteEx should do what you want, they don't create parent-child relationships between precesses.