Answer the question
In order to leave comments, you need to log in
How to work with the command line? send commands? receive messages?
Hello.
I want to automate sending commands for compiling programs from the console.
I need to launch a child process, which will be equivalent to launching a bat file with the content
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"" x86
cd c:
nmake
nmake install
#include <stdio.h>
#include <stdlib.h>
void foo(char *path)
{
std::string cmd_exe = "%comspec% /k \"\"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\vcvarsall.bat\"\" x86";
FILE *file = _popen(cmd_exe.data(), "wt");
if(!file)
return;
//
std::string folder = path;
std::replace(folder.begin(), folder.end(), '\\', '/');
std::stringstream cd;
cd << "cd \"" << folder << "\"";
fputs(cd.str().data(), file);
//
std::stringstream nmake;
nmake<< "nmake";
fputs(nmake.str().data(), file);
_pclose(file);
}
Answer the question
In order to leave comments, you need to log in
You can't do this with _popen, as far as I know
. Under POSIX, the solution would be to use pipe() and fork(), but under Windows, this probably won't work.
If command output is not important, you can use system("..."), but this will not always work
. A more correct option: run CMD via WinAPI via CreateProcess, an example is here: https://msdn.microsoft.com/en-us/library/windows/d...
PS C++ is probably far from the best solution for this task
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question