如何将写入stdout的数据保存到本地缓冲区

时间:2015-03-11 17:47:30

标签: c++ c

在MyProgram.c中我正在做CreateProcess(“GetData.exe”....);.在命令行上运行时,GetData将大约100行数据打印到stdout。当我运行MyPragrame.exe时,我希望数据不会转到stdout,而是希望它保存在缓冲区DataBuffer [100] [100]中,假设有100行,其strlen为100.请帮帮忙。感谢

MyProgram.c:

#include <Windows.h>
#include <stdio.h>

void
main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    char DataBuffer[100][100];

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    if( !CreateProcess( NULL,  // No module name (use command line)
        "Getdata.exe",  // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }
}

1 个答案:

答案 0 :(得分:1)

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspxSTARTUPINFO参数一起使用&#39;使用STARTF_USESTDHANDLES中的dwFlags,并为HANDLE提供hStdOutput

或者,更简单,您可以在命令行上重定向输出文件。例如: GetData.exe > File.txt

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

&#34;使用重定向的输入和输出创建子进程&#34;样本,适合只读取过程的标准,适合您的设置。

#include <comdef.h>

int main() {
STARTUPINFO si;
PROCESS_INFORMATION pi;
char DataBuffer[100][100];

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

SECURITY_ATTRIBUTES saAttr;

// Set the bInheritHandle flag so pipe handles are inherited. 

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;

HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;

// Create a pipe for the child process's STDOUT. 

if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
{
    printf("StdoutRd CreatePipe");
    return 0;
}

// Ensure the read handle to the pipe for STDOUT is not inherited.
if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
{
    printf("StdoutRd CreatePipe");
    return 0;
}

si.cb = sizeof(STARTUPINFO);
si.hStdError = g_hChildStd_OUT_Wr;
si.hStdOutput = g_hChildStd_OUT_Wr;
si.hStdInput = g_hChildStd_IN_Rd;
si.dwFlags |= STARTF_USESTDHANDLES;

if (!CreateProcess(NULL,  // No module name (use command line)
    "c:\\windows\\system32\\whoami.exe",  // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    TRUE,           // Set handle inheritance to TRUE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi)           // Pointer to PROCESS_INFORMATION structure
    )
{
    _com_error e(GetLastError());
    printf("CreateProcess failed (%d - %s).\n", GetLastError(), e.ErrorMessage());
    return 0;
}

DWORD bytesRead;
if (!ReadFile(g_hChildStd_OUT_Rd, DataBuffer, 100 * 100, &bytesRead, NULL))
{
    _com_error e(GetLastError());
    printf("ReadFile failed (%d - %s).\n", GetLastError(), e.ErrorMessage());
}
return 0;
}

现在,whoami.exe的输出位于DataBuffer中。请注意,此示例忽略了许多安全问题,您可能希望解决这些问题,尤其是在您未自行提供GetData.exe的情况下。