You forgot /C :
std::string cmdLine("cmd /C ipconfig /all");
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have the following code:
int main(int argc, char* argv[])
{
std::string cmdLine("cmd ipconfig /all");
std::cout << "Begin command ...\n";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
BOOL bSuccess = ::CreateProcess(NULL,
CA2W(cmdLine.c_str()),
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&si,
&pi);
if (! bSuccess)
return 0;
std::cout << "Wait command ...\n";
WaitForSingleObject(pi.hProcess, INFINITE);
::CloseHandle(pi.hThread);
// Close the process handle as soon as it is no longer needed.
::CloseHandle(pi.hProcess);
std::cout << "Command ended.\n";
return 0;
}
So, if I run this app, from command line window:
So, looks like never reach Command ended point, seems like WaitForSingleObject is block the program flow. If I remove WaitForSingleObject call, the app will show it will be reach end of the program:
But, I need to know when the my command, in this test case, ipconf /all is done, so, I really need this WaitForSingleObject call. I have took this code from here: https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes
You forgot /C :
std::string cmdLine("cmd /C ipconfig /all");
Yes, it goes now, when I removed cmd word from command. Thanks.