This will do what your looking for I didn't know if you were using c++ so I just used c conventions. You need to clean this up before you use it I I just baned it out also make sure to close the handles to the pipe or you will leak.
// RunCmd.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <strsafe.h>
int RunCmd(_TCHAR * Command,_TCHAR **OutPut);
bool HasTerminated(PROCESS_INFORMATION PI,DWORD *ExitCode);
bool HasData(HANDLE H);
void ErrorExit(LPTSTR lpszFunction);
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR * Buffer;
_TCHAR CmdLine[] = _TEXT("Outputter.exe");
RunCmd(CmdLine,&Buffer);
wprintf(_TEXT("Buffer from other pgm \n%s"),Buffer);
free(Buffer);
}
int RunCmd(_TCHAR * Command,_TCHAR ** OutPut)
{
_TCHAR * CpBUff = NULL;
STARTUPINFO SI;
memset(&SI,0,sizeof(SI));
*OutPut = NULL;
SECURITY_ATTRIBUTES SA;
SA.nLength = sizeof(SECURITY_ATTRIBUTES);
SA.lpSecurityDescriptor = NULL;
SA.bInheritHandle = true;
HANDLE ReadOutPut, WriteOutPut;
if(!CreatePipe(&ReadOutPut,&WriteOutPut,&SA,0))
{
wprintf(_TEXT("Error"));
}
SI.hStdOutput = WriteOutPut;
SI.cb = sizeof(STARTUPINFO);
SI.dwFlags = STARTF_USESTDHANDLES;
PROCESS_INFORMATION PI;
if (!CreateProcess(NULL,Command,&SA,NULL,true,CREATE_NO_WINDOW,NULL,NULL,&SI,&PI))
{
ErrorExit(TEXT("CreateProcess"));
}
Sleep(500);
DWORD ExitCode;
char Buffer[512];
_TCHAR ConvBuff[512];
int Total =0;
bool Zero;
while (!HasTerminated(PI,&ExitCode) & HasData(ReadOutPut))
{
ZeroMemory(Buffer,512*sizeof(char));
ZeroMemory(ConvBuff,512 * sizeof(_TCHAR));
DWORD NumBytesRead;
ReadFile(ReadOutPut,Buffer,512,&NumBytesRead,NULL);
Zero = Total == 0;
Total += NumBytesRead +1;
*OutPut = ((_TCHAR *) realloc(*OutPut,Total*sizeof(_TCHAR)));
if(Zero)
{
ZeroMemory(*OutPut,Total * sizeof(_TCHAR));
}
size_t ConChar;
mbstowcs_s(&ConChar,ConvBuff,strlen(Buffer)+1,Buffer,511);
StringCchCat(*OutPut,Total,ConvBuff);
}
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
return ExitCode;
}
bool HasTerminated(PROCESS_INFORMATION PI,DWORD *ExitCode)
{
return (STILL_ACTIVE == GetExitCodeProcess(PI.hProcess,ExitCode));
}
bool HasData(HANDLE H)
{
char Buffer[25];
DWORD ReadBytes,TotalBytes,TotalLeft;
PeekNamedPipe(H,Buffer,25,&ReadBytes,&TotalBytes,&TotalLeft);
return ReadBytes > 0;
}
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}