Class provides processes manipulation routine. More...
#include <system/process_abstraction.h>
Public Member Functions | |
ProcessAbstraction (void) | |
void | CreateProcess (const char *Command, const char *argv[]=NULL) |
ALIAS_FUNCTION_2 (CreateProcess, aliasCreateProcess, const char *, const char **) | |
void | CreateProcess (const std::string &Command, const char *argv[]=NULL) |
void | Wait (void) |
void | TerminateProcess (void) |
virtual | ~ProcessAbstraction () |
Private Member Functions | |
ProcessAbstraction (const ProcessAbstraction &) | |
ProcessAbstraction | operator= (const ProcessAbstraction &) |
Private Attributes | |
void * | ProcessHandle |
Class provides processes manipulation routine.
Definition at line 48 of file process_abstraction.h.
nitro::ProcessAbstraction::ProcessAbstraction | ( | void | ) |
Constructor.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 28 of file process_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::ProcessAbstraction( void )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::ProcessAbstraction( void )::An error occured" ) , 0 ) ); } }
nitro::ProcessAbstraction::~ProcessAbstraction | ( | ) | [virtual] |
Destructor.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 198 of file process_abstraction.cpp.
References ProcessHandle.
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) delete ( ProcessInfo * )ProcessHandle; #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) delete ( int * )ProcessHandle; #endif } catch( ... ) { } }
nitro::ProcessAbstraction::ProcessAbstraction | ( | const ProcessAbstraction & | ) | [private] |
Copy constructor.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 215 of file process_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::ProcessAbstraction( const ProcessAbstraction & )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::ProcessAbstraction( const ProcessAbstraction & )::An error occured" ) , 0 ) ); } }
nitro::ProcessAbstraction::ALIAS_FUNCTION_2 | ( | CreateProcess | , | |
aliasCreateProcess | , | |||
const char * | , | |||
const char ** | ||||
) |
void nitro::ProcessAbstraction::CreateProcess | ( | const char * | Command, | |
const char * | argv[] = NULL | |||
) |
Function runs process.
Command | - File of the process. | |
argv | - Parameters. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 43 of file process_abstraction.cpp.
References nitro::exception::code(), DEFINE_DYNAMIC_ARRAY, nitro::FSPath::ExtractFileName(), ProcessHandle, and nitro::exception::what().
Referenced by CreateProcess().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) ProcessHandle = new ProcessInfo; ZeroMemory( &( ( ( ProcessInfo * )ProcessHandle )->si ) , sizeof( ( ( ProcessInfo * )ProcessHandle )->si ) ); ( ( ProcessInfo * )ProcessHandle )->si.cb = sizeof( ( ( ProcessInfo * )ProcessHandle )->si ); ZeroMemory( &( ( ( ProcessInfo * )ProcessHandle )->pi ) , sizeof( ( ( ProcessInfo * )ProcessHandle )->pi ) ); std::string CommandLine( Command ); for( std::size_t i( 0 ) ; argv && argv[ i ] ; i++ ) { CommandLine += " "; CommandLine += argv[ i ]; } if( !CreateProcessA( NULL , ( LPSTR )CommandLine.c_str() , NULL , NULL , FALSE , 0 , NULL , NULL , &( ( ( ProcessInfo * )ProcessHandle )->si ) , &( ( ( ProcessInfo * )ProcessHandle )->pi ) ) ) { throw( nitro::exception( std::string( "An error occured while process startup " ) + Command , 0 ) ); } #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) ProcessHandle = new int; *( ( int * )ProcessHandle ) = fork(); if( *( ( int * )ProcessHandle ) == 0 ) { std::size_t ArgCounter( 0 ); if( argv ) { for( ; argv[ ArgCounter ] != NULL ; ArgCounter++ ); } DEFINE_DYNAMIC_ARRAY( const char * , Args , ArgCounter + 2 ) std::string FileName( FSPath::ExtractFileName( std::string( Command ) ) ); Args[ 0 ] = FileName.c_str(); for( std::size_t i( 1 ) ; i < ArgCounter + 2 ; i++ ) { if( argv ) { Args[ i ] = argv[ i - 1 ]; } else { Args[ i ] = NULL; } } if( strchr( Command , '/' ) || strchr( Command , '\\' ) ) { // указан только путь к экзешнику if( execv( Command , ( char * const * )Args ) == -1 ) { throw( nitro::exception( std::string( "An error occured while process startup " ) + Command , 0 ) ); } } else { // указан только сам экзешник if( execvp( Command , ( char * const * )Args ) == -1 ) { throw( nitro::exception( std::string( "An error occured while process startup " ) + Command , 0 ) ); } } } #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::CreateProcess( const char * Command , const char * argv [] )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::CreateProcess( const char * Command , const char * argv [] )::An error occured" ) , 0 ) ); } }
void nitro::ProcessAbstraction::CreateProcess | ( | const std::string & | Command, | |
const char * | argv[] = NULL | |||
) |
Function runs process.
Command | - File of the process. | |
argv | - Parameters. |
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 126 of file process_abstraction.cpp.
References nitro::exception::code(), CreateProcess(), and nitro::exception::what().
{ try { CreateProcess( Command.c_str() , argv ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::CreateProcess( const std::string & Command , const char * argv [] /* = NULL */ )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::CreateProcess( const std::string & Command , const char * argv [] /* = NULL */ )::An error occured" ) , 0 ) ); } }
ProcessAbstraction nitro::ProcessAbstraction::operator= | ( | const ProcessAbstraction & | ) | [private] |
Assign operator.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 230 of file process_abstraction.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { return( *this ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::operator=( const ProcessAbstraction & )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::operator=( const ProcessAbstraction & )::An error occured" ) , 0 ) ); } }
void nitro::ProcessAbstraction::TerminateProcess | ( | void | ) |
Function terminates child process.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 167 of file process_abstraction.cpp.
References nitro::exception::code(), ProcessHandle, and nitro::exception::what().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) if( ::TerminateProcess( ( ( ProcessInfo * )ProcessHandle )->pi.hProcess, 0 ) == 0 ) { throw( nitro::exception( "An error occured while process termination" , 1 ) ); } CloseHandle( ( ( ProcessInfo * )ProcessHandle )->pi.hProcess ); CloseHandle( ( ( ProcessInfo * )ProcessHandle )->pi.hThread ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) if( kill( *( ( int * )ProcessHandle ) , SIGKILL ) != 0 ) { throw( nitro::exception( "An error occured while process termination" , 1 ) ); } #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::Wait( void )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::Wait( void )::An error occured" ) , 0 ) ); } }
void nitro::ProcessAbstraction::Wait | ( | void | ) |
Function blocks process until the child process will be terminated.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 142 of file process_abstraction.cpp.
References nitro::exception::code(), ProcessHandle, and nitro::exception::what().
{ try { #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) WaitForSingleObject( ( ( ProcessInfo * )ProcessHandle )->pi.hProcess, INFINITE ); CloseHandle( ( ( ProcessInfo * )ProcessHandle )->pi.hProcess ); CloseHandle( ( ( ProcessInfo * )ProcessHandle )->pi.hThread ); #endif #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM ) waitpid( *( ( int * )ProcessHandle ) , NULL , WUNTRACED ); #endif } catch( nitro::exception e ) { throw( nitro::exception( std::string( "ProcessAbstraction::Wait( void )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "ProcessAbstraction::Wait( void )::An error occured" ) , 0 ) ); } }
void* nitro::ProcessAbstraction::ProcessHandle [private] |
Process handle.
nitro::exception | Throws an exception of that type with the error description. |
Definition at line 226 of file process_abstraction.h.
Referenced by CreateProcess(), TerminateProcess(), Wait(), and ~ProcessAbstraction().