If you are using system()
you cannot avoid the occasional flash of the console window. Were you to use any other program you might even see its window flash.
I won’t go into any detail about the security flaws inherent to using system()
.
The correct way to do this with the Windows API.
Even so, you are taking a sledgehammer approach. You should first signal the process to terminate gracefully. If it hasn’t done so after a second or two, only then should you crash it.
The SO question “How to gracefully terminate a process” details several options to properly ask a process to terminate.
If that fails, then you can simply kill a process using the TerminateProcess()
Windows API function (which is what taskkill /f
does). Here is a good example of how to do that: https://github.com/malcomvetter/taskkill
The relevant code has the following function:
BOOL TerminateProcess(int pid)
{
WORD dwDesiredAccess = PROCESS_TERMINATE;
BOOL bInheritHandle = FALSE;
HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, pid);
if (hProcess == NULL)
return FALSE;
BOOL result = TerminateProcess(hProcess, 1);
CloseHandle(hProcess);
return(TRUE);
}
Microsoft has a page all about Terminating a Process that you may wish to review as well.