www.pudn.com > acdx.rar > Pipe.h
#if !defined(Pipe_H) #define Pipe_H #include "SecurityAttributes.h" #include#include using namespace std; /////////////////////////////////////////////////////////////////////////////////// // Pipe //##ModelId=424BB64203C0 class Pipe { public: //##ModelId=424BB64203D0 bool _ownWrite; // shows if close read on destroy //##ModelId=424BB64203E0 HANDLE _hWrite; // write handle //##ModelId=424BB64203E1 bool _ownRead; // shows if close read on destroy //##ModelId=424BB6430007 HANDLE _hRead; // read handle //##ModelId=424BB6430008 Pipe () : _hWrite(0), _hRead(0), _ownWrite(true), _ownRead(true) {} //##ModelId=424BB6430009 virtual ~Pipe () { release(); } // create/release //##ModelId=424BB6430017 bool create ( bool inherit = true ) { // Set up the security attributes struct. SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = (BOOL) inherit; // create the pipe if (!CreatePipe(&_hRead,&_hWrite,&sa,0)) return false; else return true; } //##ModelId=424BB6430019 bool create ( HANDLE hRead, HANDLE hWrite ) { if ( hRead == 0 || hWrite == 0 ) return false; // get read handle _hRead = hRead; _ownRead = true; // get write handle _hWrite = hWrite; _ownWrite = true; return true; } //##ModelId=424BB6430027 void release () { close(); } // dupicate handle //##ModelId=424BB6430028 HANDLE duplicateRead ( bool inherit, bool sameAccess ) { HANDLE hProcess = GetCurrentProcess(); return duplicate( _hRead, hProcess, inherit, sameAccess ); } //##ModelId=424BB6430036 HANDLE duplicateWrite ( bool inherit, bool sameAccess ) { HANDLE hProcess = GetCurrentProcess(); return duplicate( _hWrite, hProcess, inherit, sameAccess ); } //##ModelId=424BB6430039 static HANDLE duplicate ( HANDLE hPipe, HANDLE hProcess, bool inherit, bool sameAccess ) { // handle to duplicated pipe HANDLE hDupPipe; // get options DWORD dwOptions = DUPLICATE_CLOSE_SOURCE; if ( sameAccess ) dwOptions = DUPLICATE_SAME_ACCESS; // get process if ( hProcess == 0 ) hProcess = GetCurrentProcess(); // Create a duplicate of the output write handle for the std error // write handle. This is necessary in case the child application // closes one of its std output handles. BOOL success = DuplicateHandle( hProcess, hPipe, hProcess, &hDupPipe, 0, (BOOL) inherit, dwOptions ); if ( success ) return hDupPipe; else return 0; } // attach to handle //##ModelId=424BB643004A void attachRead ( HANDLE hRead ) { // if already exists stop if ( _hRead == 0 ) return; // get read handle _hRead = hRead; _ownRead = false; } // attach to handle //##ModelId=424BB6430056 void attachWrite ( HANDLE hWrite ) { // if already exists stop if ( _hWrite == 0 ) return; // get write handle _hWrite = hWrite; _ownWrite = false; } // get handles //##ModelId=424BB6430058 HANDLE getWrite () { return _hWrite; } //##ModelId=424BB6430065 HANDLE getRead () { return _hRead; } // get handles //##ModelId=424BB6430066 void close () { closeRead(); closeWrite(); } //##ModelId=424BB6430067 void closeWrite () { if ( _hWrite = 0 ) return; CloseHandle(_hWrite); _hWrite = 0; } //##ModelId=424BB6430075 void closeRead () { if ( _hRead = 0 ) return; CloseHandle(_hRead); _hRead = 0; } // read/write pipe //##ModelId=424BB6430076 long read ( char * readBuf, long readSz ) { // if no read handle stop if ( _hRead == 0 ) return 0; // read from pipe DWORD nBytesRead; BOOL success = ReadFile( _hRead,readBuf,readSz,&nBytesRead,NULL ); if ( success ) return nBytesRead; else { DWORD error = GetLastError(); return 0; } } //##ModelId=424BB6430086 long write ( char * writeBuf, long writeSz ) { // if no write handle stop if ( _hWrite == 0 ) return 0; // read from pipe DWORD nBytesWritten; BOOL success = WriteFile( _hWrite,writeBuf,writeSz,&nBytesWritten,NULL ); if ( success ) { return nBytesWritten; } else { DWORD error = GetLastError(); return 0; } } // write //##ModelId=424BB6430095 Pipe & operator << ( string & str ) { // get length long bytesToWrite = str.size() + 1; // write buffer long bytesWritten = write( (LPTSTR) str.c_str(), bytesToWrite ); return *this; } //##ModelId=424BB6430097 Pipe & operator << ( LPCTSTR str ) { // get length long bytesToWrite = strlen(str); // write buffer long bytesWritten = write( (LPTSTR) str, bytesToWrite ); return *this; } //##ModelId=424BB64300A4 Pipe & operator << ( LPTSTR str ) { // get length long bytesToWrite = strlen(str); // write buffer long bytesWritten = write( str, bytesToWrite ); return *this; } // read //##ModelId=424BB64300A6 Pipe & operator >> ( string & str ) { // read from pipe char buffer[1024]; long bytesRead = read( buffer, 1024 ); buffer[bytesRead] = '\0'; // if got something read it if ( bytesRead > 0 ) str = buffer; return *this; } }; #endif