unsigned SysRunCommandPipe( const char *cmd, int *readpipe ) { int rc; HANDLE pipe_input; HANDLE pipe_output; HANDLE pipe_input_dup; SECURITY_ATTRIBUTES sa; sa.nLength = sizeof( sa ); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; if( !CreatePipe( &pipe_input, &pipe_output, &sa, 0 ) ) { return( GetLastError() ); } SetStdHandle( STD_OUTPUT_HANDLE, pipe_output ); SetStdHandle( STD_ERROR_HANDLE, pipe_output ); DuplicateHandle( GetCurrentProcess(), pipe_input, GetCurrentProcess(), &pipe_input_dup, 0, FALSE, DUPLICATE_SAME_ACCESS ); CloseHandle( pipe_input ); rc = RunChildProcessCmdl( cmd ); CloseHandle( pipe_output ); *readpipe = _hdopen( ( int ) pipe_input_dup, O_RDONLY ); return( rc ); }
int SysRunCommand( const char *cmd ) { DWORD rc; DWORD bytes_read; char buff[256 + 1]; HANDLE readpipe; PROCESS_INFORMATION pinfo; memset( &pinfo, 0, sizeof( pinfo ) ); readpipe = INVALID_HANDLE_VALUE; rc = RunChildProcessCmdl( cmd, &pinfo, &readpipe ); if( rc != 0 ) { if( readpipe != INVALID_HANDLE_VALUE ) { CloseHandle( readpipe ); } return( rc ); } if( readpipe != INVALID_HANDLE_VALUE ) { for( ;; ) { char *dst; DWORD i; ReadFile( readpipe, buff, sizeof( buff ) - 1, &bytes_read, NULL ); if( bytes_read == 0 ) break; dst = buff; for( i = 0; i < bytes_read; ++i ) { if( buff[i] != '\r' ) { *dst++ = buff[i]; } } *dst = '\0'; Log( Quiet, "%s", buff ); } CloseHandle( readpipe ); } WaitForSingleObject( pinfo.hProcess, INFINITE ); GetExitCodeProcess( pinfo.hProcess, &rc ); CloseHandle( pinfo.hProcess ); CloseHandle( pinfo.hThread ); return( rc ); }