N
N
neosapient2016-08-18 13:57:04
C++ / C#
neosapient, 2016-08-18 13:57:04

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

and in the window that opens, execute some commands, for example
cd c:
nmake
nmake install

In general, I try to write data through pipes, but my commands are not heard
#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);
}

That is, when I call _popen(), I see the standard "D:\something>" prompt in the console.
But then my "cd c:" and "nmake" commands are ignored.
The file pointer is non-null, but pipe doesn't respond to my commands. Maybe I'm somehow not opening the pipe correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Teermit, 2016-08-18
@Teermit

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 question

Ask a Question

731 491 924 answers to any question