Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to save system function output into a variable in c++

#include <windows.h>
#include <atlstr.h>
//
// Execute a command and get the results. (Only standard output)
//
CStringA ExecCmd(
    const wchar_t* cmd              // [in] command to execute
)
{
    CStringA strResult;
    HANDLE hPipeRead, hPipeWrite;

    SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES)};
    saAttr.bInheritHandle = TRUE; // Pipe handles are inherited by child process.
    saAttr.lpSecurityDescriptor = NULL;

    // Create a pipe to get results from child's stdout.
    if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
        return strResult;

    STARTUPINFOW si = {sizeof(STARTUPINFOW)};
    si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.hStdOutput  = hPipeWrite;
    si.hStdError   = hPipeWrite;
    si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing.
                              // Requires STARTF_USESHOWWINDOW in dwFlags.

    PROCESS_INFORMATION pi = { 0 };

    BOOL fSuccess = CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    if (! fSuccess)
    {
        CloseHandle(hPipeWrite);
        CloseHandle(hPipeRead);
        return strResult;
    }

    bool bProcessEnded = false;
    for (; !bProcessEnded ;)
    {
        // Give some timeslice (50 ms), so we won't waste 100% CPU.
        bProcessEnded = WaitForSingleObject( pi.hProcess, 50) == WAIT_OBJECT_0;

        // Even if process exited - we continue reading, if
        // there is some data available over pipe.
        for (;;)
        {
            char buf[1024];
            DWORD dwRead = 0;
            DWORD dwAvail = 0;

            if (!::PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwAvail, NULL))
                break;

            if (!dwAvail) // No data available, return
                break;

            if (!::ReadFile(hPipeRead, buf, min(sizeof(buf) - 1, dwAvail), &dwRead, NULL) || !dwRead)
                // Error, the child process might ended
                break;

            buf[dwRead] = 0;
            strResult += buf;
        }
    } //for

    CloseHandle(hPipeWrite);
    CloseHandle(hPipeRead);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return strResult;
} //ExecCmd
Comment

PREVIOUS NEXT
Code Example
Cpp :: convert into acsii c++ 
Cpp :: how to draw a rectangle with diagonals and axes of symmetry in c ++ in the console? 
Cpp :: Print value of data in c++ 
Cpp :: bnchch 
Cpp :: how to run the code 
Cpp :: ue4 c++ add tag 
Cpp :: a suprise... c++ 
Cpp :: qt get wireless interface name 
Cpp :: input many numbers to int c++ 
Cpp :: dfs in tree using adjacency list 
Cpp :: using of and || c++ 
Cpp :: DMA c/c++ 
Cpp :: how to find the sum of elements in a stack in cpp 
Cpp :: float to byte array and back c++ with memcpy command 
Cpp :: c++ program to convert time in seconds to hours minutes and seconds 
Cpp :: avl tree c++ 
Cpp :: nodeafternode 
Cpp :: initialise a vector c++ 
Cpp :: all in one c++ 
Cpp :: convert GLFWwindow* to IntPtr 
Cpp :: 130 divided by -10 
Cpp :: compilling c++ and c by console 
Cpp :: time optimisation c++ 
Cpp :: vector of vector definaion in c++ 
Cpp :: 12 to december in c++ code 
Cpp :: Configuring an c++ OpenCV project with Cmake 
Cpp :: const char * to std::wstring 
Cpp :: c++ const shared_ptr 
Cpp :: sieve of eratosthenes c++ 
Cpp :: sort c++ array 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =