// ----------------------------------------------------------------------------------
// Use:>
// CString resultContext;
// ExecuteCmdEx( (LPTSTR)(LPCTSTR)CString("net help"), resultContext);
// ----------------------------------------------------------------------------------
BOOL ExecuteCmdEx(LPTSTR cmdline, CString & outputResult)
{
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE hInput,hOutput;
if ( ! CreatePipe( & hInput, & hOutput, & sa, 0 ))
return FALSE;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( & si, sizeof (si) );
ZeroMemory( & pi, sizeof (pi) );
si.cb = sizeof (si);
si.hStdError = hOutput;
si.hStdOutput = hOutput;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if ( ! CreateProcess(NULL,cmdline,NULL,NULL,TRUE,NULL,NULL,NULL, & si, & pi)){
CloseHandle(hInput);
CloseHandle(hOutput);
return FALSE;
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
DWORD rByte = 4095 ;
char outputBuffer[ 4096 ];
while (rByte == 4095 ){
ZeroMemory(outputBuffer, sizeof ( char ) * 4096 );
ReadFile(hInput,outputBuffer, sizeof ( char ) * 4095 , & rByte, NULL);
outputResult += outputBuffer;
}
CloseHandle(hInput);
CloseHandle(hOutput);
return TRUE;
}