In MyProgram.c I am doing CreateProcess("GetData.exe" ....);. GetData when run on a command line prints about 100 lines of data to stdout. When I run my MyPragrame.exe I want that data not going to stdout instead I want it to be saved in the buffer DataBuffer[100][100] assuming there are 100 lines whose strlen is 100. Please help out. Thanks
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;
}
}
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx Use with your STARTUPINFO
parameter 'si', use STARTF_USESTDHANDLES
in the dwFlags
, and provide a HANDLE
for hStdOutput
.
Or, even easier, you could just redirect the output the file on the command line. Eg: GetData.exe > File.txt
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
"Creating a Child Process with Redirected Input and Output" sample, adapted to only read the stdout of the process, and fits your setup.
#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;
}
Now, the output of whoami.exe is in DataBuffer. Note that this sample disregards a lot of safety issues, which you may want to address, especially if you are not providing GetData.exe yourself.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.