[Solved] CreateProcess causing problems


First, fgets will get a string with charactor ‘\n’ when size of inserted string <(255-1). So, let’s set the \n to \0:

fgets(cmd, 255, stdin);
cmd[strlen(cmd) - 1] = '\0';
CreateProcess(cmd, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

Second,

more instances of cmd to popup in the command line.

If what you mean is like:

enter image description here

This is because the input focus of the cmd process and the current process alternately appears in the same console, not always creating a new instance.

If you CreateProcess with CREATE_NEW_CONSOLE:

CreateProcess(cmd, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

You will see that only a new cmd console has been created.
enter image description here

solved CreateProcess causing problems