0

I have a Python script written, that is an extension to a massive C++ program written on Visual Studio 2013. The scripts works fine on the program and I can call it easily inside the project using system() command. However my new task is to get any Warnings from that script display as warnings inside the VS project. I have no shared directories, the python script requires no C++ variables from the project and the script has no return variables.

My current simplistic way of running my script :

system("py C:\\Task4\\testing_functions.py");

I tried using <python.h> library but couldn't get it to work on the pre-existing project without tampering with the configuration options and directories which is a no-go for the rest of the company. Is there a way I can perhaps export the python log into a report that Visual Studio can then read and display?

Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • 1
    Check https://stackoverflow.com/questions/16235370/execute-script-with-c#16237543 – kabanus Apr 12 '18 at 09:55
  • @kabanus going to that link, I found a better version of their answer in another link: https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix/478960#478960. Though this is neat however and I quote the first comment to the top answer in my link "Be aware that this will only grab stdout and not stderr. – kalaxy". But stderr is where the warning and errors are. Using popen and assigning the output to a string is not enough, neither to a text file because I need something C++ can pickup later and embed inside the warning and error log. – Hadi Farah Apr 12 '18 at 10:22
  • 1
    Hmm, wouldn't redirecting stderr in the `system` call to a file of your choice be enough then? – kabanus Apr 12 '18 at 10:42
  • @kabanus maybe I will do that, write stderr toa file read it as strings and manually write my user warnings with that. I am not very familiar with the C++ `system` command since `system("py C:\\Task4\\testing_functions.py > *some out file*");` writes the stdout how do I prompt the stderr only to output ? – Hadi Farah Apr 12 '18 at 11:13
  • @kabanus I found a good (enough*) solution I will post it. – Hadi Farah Apr 12 '18 at 11:48

1 Answers1

1
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>

#include <windows.h>
#include <cstdint>
#include <deque>
#include <thread>

using namespace std;

int SystemCapture(
    string         CmdLine,    //Command Line
    string         CmdRunDir,  //set to '.' for current directory
    string&        ListStdOut, //Return List of StdOut
    string&        ListStdErr, //Return List of StdErr
    uint32_t&      RetCode)    //Return Exit Code
{
    int                  Success;
    SECURITY_ATTRIBUTES  security_attributes;
    HANDLE               stdout_rd = INVALID_HANDLE_VALUE;
    HANDLE               stdout_wr = INVALID_HANDLE_VALUE;
    HANDLE               stderr_rd = INVALID_HANDLE_VALUE;
    HANDLE               stderr_wr = INVALID_HANDLE_VALUE;
    PROCESS_INFORMATION  process_info;
    STARTUPINFO          startup_info;
    thread               stdout_thread;
    thread               stderr_thread;

    security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
    security_attributes.bInheritHandle = TRUE;
    security_attributes.lpSecurityDescriptor = nullptr;

    if (!CreatePipe(&stdout_rd, &stdout_wr, &security_attributes, 0) ||
        !SetHandleInformation(stdout_rd, HANDLE_FLAG_INHERIT, 0)) {
        return -1;
    }

    if (!CreatePipe(&stderr_rd, &stderr_wr, &security_attributes, 0) ||
        !SetHandleInformation(stderr_rd, HANDLE_FLAG_INHERIT, 0)) {
        if (stdout_rd != INVALID_HANDLE_VALUE) CloseHandle(stdout_rd);
        if (stdout_wr != INVALID_HANDLE_VALUE) CloseHandle(stdout_wr);
        return -2;
    }

    ZeroMemory(&process_info, sizeof(PROCESS_INFORMATION));
    ZeroMemory(&startup_info, sizeof(STARTUPINFO));

    startup_info.cb = sizeof(STARTUPINFO);
    startup_info.hStdInput = 0;
    startup_info.hStdOutput = stdout_wr;
    startup_info.hStdError = stderr_wr;

    if (stdout_rd || stderr_rd)
        startup_info.dwFlags |= STARTF_USESTDHANDLES;

    // Make a copy because CreateProcess needs to modify string buffer
    char      CmdLineStr[MAX_PATH];
    strncpy_s(CmdLineStr, CmdLine.c_str(), MAX_PATH);
    CmdLineStr[MAX_PATH - 1] = 0;

    Success = CreateProcess(
        nullptr,
        CmdLineStr,
        nullptr,
        nullptr,
        TRUE,
        0,
        nullptr,
        CmdRunDir.c_str(),
        &startup_info,
        &process_info
        );
    CloseHandle(stdout_wr);
    CloseHandle(stderr_wr);

    if (!Success) {
        CloseHandle(process_info.hProcess);
        CloseHandle(process_info.hThread);
        CloseHandle(stdout_rd);
        CloseHandle(stderr_rd);
        return -4;
    }
    else {
        CloseHandle(process_info.hThread);
    }
    /*
    if (stdout_rd) {
        stdout_thread = thread([&]() {
            DWORD  n;
            const size_t bufsize = 1000;
            char         buffer[bufsize];
            for (;;) {
                n = 0;
                int Success = ReadFile(
                    stdout_rd,
                    buffer,
                    (DWORD)bufsize,
                    &n,
                    nullptr
                    );
                printf("STDERR: Success:%d n:%d\n", Success, (int)n);
                if (!Success || n == 0)
                    break;
                string s(buffer, n);
                printf("STDOUT:(%s)\n", s.c_str());
                ListStdOut += s;
            }
            printf("STDOUT:BREAK!\n");
        });
    }
    */
    if (stderr_rd) {
        stderr_thread = thread([&]() {
            DWORD        n;
            const size_t bufsize = 1000;
            char         buffer[bufsize];
            for (;;) {
                n = 0;
                int Success = ReadFile(
                    stderr_rd,
                    buffer,
                    (DWORD)bufsize,
                    &n,
                    nullptr
                    );
                printf("STDERR: Success:%d n:%d\n", Success, (int)n);
                if (!Success || n == 0)
                    break;
                string s(buffer, n);
                printf("STDERR:(%s)\n", s.c_str());
                ListStdOut += s;
            }
            printf("STDERR:BREAK!\n");
        });
    }

    WaitForSingleObject(process_info.hProcess, INFINITE);
    if (!GetExitCodeProcess(process_info.hProcess, (DWORD*)&RetCode))
        RetCode = -1;

    CloseHandle(process_info.hProcess);

    if (stdout_thread.joinable())
        stdout_thread.join();

    if (stderr_thread.joinable())
        stderr_thread.join();

    CloseHandle(stdout_rd);
    CloseHandle(stderr_rd);

    return 0;
}


std::string exec(const char* cmd) {
    std::array<char, 128> buffer;
    std::string result;
    std::shared_ptr<FILE> pipe(_popen(cmd, "r"), _pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
            result += buffer.data();
    }
    return result;
}

int main(int argc, char* argv[])
{
    int            rc;
    uint32_t       RetCode;
    string         ListStdOut;
    string         ListStdErr;

    cout << "STARTING.\n";

    rc = SystemCapture(
        "py C:\\Task4\\testing_functions.py",    //Command Line
        ".",                                     //CmdRunDir
        ListStdOut,                              //Return List of StdOut
        ListStdErr,                              //Return List of StdErr
        RetCode                                  //Return Exit Code
        );
    if (rc < 0) {
        cout << "ERROR: SystemCapture\n";
    }

    /*
    cout << "STDOUT:\n";
    cout << ListStdOut;


    cout << "STDERR:\n";
    cout << ListStdErr;

    cout << "Finished.\n";
    */

    cout << "Press Enter to Continue";
    cin.ignore();

    return 0;
}

This entire code will output everything including STDOUT and STDERR, I've commented out all the standard output since I only want the warnings.

Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • Do not forgot to accept your answer in a few days if no one posts anything better. – kabanus Apr 12 '18 at 11:57
  • @kabanus how do I do that, I have two question on stack overflow to which I have answered myself some time later but never accepted. – Hadi Farah Apr 12 '18 at 12:24