Win32: Attempt #1 at bugfixing the stdout/stderr redirection; Switched from CreateNamedPipe to CreatePipe.

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2014 96395faa-99c1-11dd-bbfe-3dabce05a288
This commit is contained in:
Jake.Stine 2009-10-16 16:15:40 +00:00
parent 66e9e8c085
commit 5c930fcb55
1 changed files with 18 additions and 34 deletions

View File

@ -72,38 +72,23 @@ namespace Exception
using namespace Threading; using namespace Threading;
static __forceinline void CreatePipe( HANDLE& ph_Pipe, HANDLE& ph_File ) static __forceinline void __CreatePipe( HANDLE& ph_ReadPipe, HANDLE& ph_WritePipe )
{ {
// Create a threadsafe unique name for the Pipe
static int s32_Counter = 0;
wxString s_PipeName;
s_PipeName.Printf( L"\\\\.\\pipe\\pcsxPipe%X_%X_%X_%X",
GetCurrentProcessId(), GetCurrentThreadId(), GetTickCount(), ++s32_Counter);
SECURITY_ATTRIBUTES k_Secur; SECURITY_ATTRIBUTES k_Secur;
k_Secur.nLength = sizeof(SECURITY_ATTRIBUTES); k_Secur.nLength = sizeof(SECURITY_ATTRIBUTES);
k_Secur.lpSecurityDescriptor = 0; k_Secur.lpSecurityDescriptor = 0;
k_Secur.bInheritHandle = TRUE; k_Secur.bInheritHandle = TRUE;
ph_Pipe = CreateNamedPipe(s_PipeName, PIPE_ACCESS_DUPLEX, 0, 1, 2048, 2048, 0, &k_Secur); if( 0 == CreatePipe( &ph_ReadPipe, &ph_WritePipe, &k_Secur, 2048 ) )
throw Exception::Win32Error( "CreatePipe failed." );
if (ph_Pipe == INVALID_HANDLE_VALUE) if (!ConnectNamedPipe(ph_ReadPipe, NULL))
throw Exception::Win32Error( "CreateNamedPipe failed." );
ph_File = CreateFile(s_PipeName, GENERIC_READ|GENERIC_WRITE, 0, &k_Secur, OPEN_EXISTING, 0, NULL);
if (ph_File == INVALID_HANDLE_VALUE)
throw Exception::Win32Error( "CreateFile to pipe failed." );
if (!ConnectNamedPipe(ph_Pipe, NULL))
{ {
if (GetLastError() != ERROR_PIPE_CONNECTED) if (GetLastError() != ERROR_PIPE_CONNECTED)
throw Exception::Win32Error( "ConnectNamedPipe failed." ); throw Exception::Win32Error( "ConnectNamedPipe failed." );
} }
SetHandleInformation(ph_Pipe, HANDLE_FLAG_INHERIT, 0); SetHandleInformation(ph_WritePipe, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation(ph_File, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
} }
// Reads from the Pipe and appends the read data to ps_Data // Reads from the Pipe and appends the read data to ps_Data
@ -199,8 +184,8 @@ class WinPipeRedirection : public PipeRedirectionBase
DeclareNoncopyableObject( WinPipeRedirection ); DeclareNoncopyableObject( WinPipeRedirection );
protected: protected:
HANDLE m_pipe; HANDLE m_readpipe;
HANDLE m_file; HANDLE m_writepipe;
int m_crtFile; int m_crtFile;
FILE* m_fp; FILE* m_fp;
@ -214,19 +199,19 @@ public:
}; };
WinPipeRedirection::WinPipeRedirection( FILE* stdstream ) : WinPipeRedirection::WinPipeRedirection( FILE* stdstream ) :
m_pipe(INVALID_HANDLE_VALUE) m_readpipe(INVALID_HANDLE_VALUE)
, m_file(INVALID_HANDLE_VALUE) , m_writepipe(INVALID_HANDLE_VALUE)
, m_crtFile(-1) , m_crtFile(-1)
, m_fp(NULL) , m_fp(NULL)
, m_Thread( m_pipe, (stdstream == stderr) ? Color_Red : Color_Black ) , m_Thread( m_readpipe, (stdstream == stderr) ? Color_Red : Color_Black )
{ {
try try
{ {
pxAssert( (stdstream == stderr) || (stdstream == stdout) ); pxAssert( (stdstream == stderr) || (stdstream == stdout) );
DWORD stdhandle = ( stdstream == stderr ) ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE; DWORD stdhandle = ( stdstream == stderr ) ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE;
CreatePipe( m_pipe, m_file ); __CreatePipe( m_readpipe, m_writepipe );
if( 0 == SetStdHandle( stdhandle, m_file ) ) if( 0 == SetStdHandle( stdhandle, m_writepipe ) )
throw Exception::Win32Error( "SetStdHandle failed." ); throw Exception::Win32Error( "SetStdHandle failed." );
// In some cases GetStdHandle can fail, even when the one we just assigned above is valid. // In some cases GetStdHandle can fail, even when the one we just assigned above is valid.
@ -277,7 +262,6 @@ void WinPipeRedirection::Cleanup() throw()
m_fp = NULL; m_fp = NULL;
m_crtFile = -1; // crtFile is closed implicitly when closing m_fp m_crtFile = -1; // crtFile is closed implicitly when closing m_fp
m_file = INVALID_HANDLE_VALUE; // m_file is closed implicitly when closing crtFile
} }
if( m_crtFile != -1 ) if( m_crtFile != -1 )
@ -286,16 +270,16 @@ void WinPipeRedirection::Cleanup() throw()
m_crtFile = -1; // m_file is closed implicitly when closing crtFile m_crtFile = -1; // m_file is closed implicitly when closing crtFile
} }
if( m_file != INVALID_HANDLE_VALUE ) if( m_readpipe != INVALID_HANDLE_VALUE )
{ {
CloseHandle( m_pipe ); CloseHandle( m_readpipe );
m_file = INVALID_HANDLE_VALUE; m_readpipe = m_writepipe = INVALID_HANDLE_VALUE;
} }
if( m_pipe != INVALID_HANDLE_VALUE ) if( m_writepipe != INVALID_HANDLE_VALUE )
{ {
CloseHandle( m_pipe ); CloseHandle( m_writepipe );
m_pipe = INVALID_HANDLE_VALUE; m_readpipe = m_writepipe = INVALID_HANDLE_VALUE;
} }
} }