A quick cut and paste from my code and removal off all that you dont need.
This little piece handle the commandline in a threading way, so you could call a thread
CODE
DWORD WINAPI UserThreadProcess(WORD wShowWindow,char * Commandline, char *Cwd,DWORD TimeOut,char **Reply)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
HANDLE hPipeRead,hPipeWrite;
BOOL bPipe=FALSE;
int ProcessExitCode;
memset(&si,0,sizeof(si));
memset(&pi,0,sizeof(pi));
si.cb=sizeof(si);
si.lpTitle=CommandLine;
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow=wShowWindow;
//Create the console pipe to catch the childs output
SECURITY_ATTRIBUTES SecurityAttributes;
SecurityAttributes.nLength=sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle=TRUE;
SecurityAttributes.lpSecurityDescriptor=NULL;
bPipe=CreatePipe(&hPipeRead,&hPipeWrite,&SecurityAttributes,0);
if (bPipe){ //if created well then catch the reply
si.hStdOutput=hPipeWrite;
si.hStdError=hPipeWrite;
//No pipe
return -1;
}
DWORD StartTick=GetTickCount();
DWORD EndTick=StartTick;
BOOL bRes=CreateProcess(NULL,CommandLine,NULL,NULL,TRUE,0,NULL,Cwd,&si,&pi);
if (!bRes){
DWORD dwErr=GetLastError();
//if commandline failed
CloseHandle(hPipeWrite);
return -1;
}
ProcessExitCode=0;
BOOL bExit;
//Now in a loop read data from the pipe, first set the pipe to return immediately
DWORD nBytesRead,dwMode=PIPE_NOWAIT;
char Buffer[1024];
SetNamedPipeHandleState(hPipeRead,&dwMode,NULL,NULL);
bool bLastIsCRLF=true;
for (;;){
WaitForSingleObject(pi.hProcess,1); //switch tasks so the pipe gets filled
bExit=GetExitCodeProcess(pi.hProcess,&ProcessExitCode); //check if process finished or not
if (ReadFile(hPipeRead,&Buffer,sizeof(Buffer)-1,&nBytesRead,NULL)){
if (nBytesRead>0){
Buffer[nBytesRead]='\0'; //if needed replace \b with ' '
//Add to Reply buffer
char *n=realloc(Reply,ReplyLen+nBytesRead);
if (n){ //if not enough memory then stop receiving the pipe info into the buffer
strcat(n,Buffer);
Reply=n;
}
}
}
if (nBytesRead==0 && ProcessExitCode!=STILL_ACTIVE) break; //if process is ended then break
EndTick=GetTickCount();
if (EndTick>StartTick && EndTick-StartTick>(TimeOut*1000)) break; //<49.7 days
if (StartTick>EndTick && (~StartTick)+EndTick>(TimeOut*1000)) break; //>49.7 days
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hPipeRead);
CloseHandle(hPipeWrite);
return 0;
}
This should be more then sufficient to get you started on pipes.