Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to execute a command in the command line prompt. This command is sent from c++. If i am trying to execute 3 command, only 2 is executed and the third is not returned to c++.
Here is what i am trying to execute:
wmic diskdrive get serialnumber& wmic cpu get ProcessorId& wmic os get SerialNumber& echo %username%


Here is the code snippet of catching the return from CMD my prog:
cmd.append("2>&1");

    std::string data;
    FILE * stream;
    const int char_length= 256;
    char buffer[char_length];

    stream = popen(cmd.c_str(), "r");
    if (stream)
    {
        while (!feof(stream))
        if (fgets(buffer, char_length, stream) != NULL)
            data.append(buffer);
        pclose(stream);
     }
    return data;


The returned code contain information about the first 3 command. I thought that there is some issue about the last command but when trying to swap the command the last command was not also caught by the programme.

What I have tried:

I have walked around the problem by making the last command a useless one (that i do not need), i made it:
C++
echo

std::string cmd("wmic diskdrive get serialnumber& wmic cpu get ProcessorId& echo %username%& wmic os get SerialNumber& echo");


i think there must be a solution for this, thanks in advance for help
Posted
Updated 16-Apr-18 0:52am

The line that creates the problem is
cmd.append("2>&1");
You forgot to insert a space in front of the output redirection:
cmd.append(" 2>&1");

Without the space the last command is
echo %username%2
and the output is redirected to stdout. But that does not work because the input is already from stdout so that the redirection fails with an error message on stderr which is not catched by your code now.

Try it at a command prompt to see the error message:
echo %username%2>&1
The handle could not be duplicated during redirection of handle 1.
 
Share this answer
 
Comments
Ahmed AE 16-Apr-18 7:54am    
Thank you, i have another 2 question please.
1. During the execution of that code the CMD is flashing, is there any way to stop it from flashing?
2. How to catch the stderr as you stated?
Jochen Arndt 16-Apr-18 8:10am    
See solution 1.

Otherwise you have to use CreateProcess(), set the wShowWindow member of the STARTUPINFO struct to SW_HIDE, and add STARTF_USESHOWWINDOW to the dwFlags member. Or use the CREATE_NO_WINDOW flag. To read the output you have to create a pipe as described at https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx.
Jochen Arndt 16-Apr-18 8:14am    
Sorry, forgot about the second question.

stderr is catched when using the corrected code. That is what "2>&1" does:
It redirects stderr (2) to stdout(1) so that both output streams are read by your code. But in the wrong command the "2" is just a character (printed with the echo statement) so that the redirection is not working.
Why are you even shelling out to wmic to run the queries? Run the queries directly yourself. WMI C++ Application Examples (Windows)[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900