I am wrapping windows c funcs in cpp classes, like process, thread among other things.
I am not lookin for portability on this project, I need functionality.
So my question is, has somethin like this been done before ?
In particular, I am looking for options to set process / thread creation flags, suspend && resume em as I please.Code:// // hpp #ifndef EVELATOR_PROCESS_HANDLE_HPP #define EVELATOR_PROCESS_HANDLE_HPP #if defined ( _MSC_VER ) && ( _MSC_VER >= 1020 ) # pragma once #endif // _MSC_VER && _MSC_VER >= 1020 class ProcessHandle { public: typedef DWORD Id; typedef HANDLE Native; enum ProcessRights { ProcessCreateProcess = PROCESS_CREATE_PROCESS, ProcessCreateThread = PROCESS_CREATE_THREAD, ProcessDuplicateHandle = PROCESS_DUP_HANDLE, ProcessSetInformation = PROCESS_SET_INFORMATION, ProcessSetQuota = PROCESS_SET_QUOTA, ProcessSuspendResume = PROCESS_SUSPEND_RESUME, ProcessTerminate = PROCESS_TERMINATE, ProcessMemoryOperation = PROCESS_VM_OPERATION, ProcessMemoryRead = PROCESS_VM_READ, ProcessMemoryWrite = PROCESS_VM_WRITE, ProcessSynchronize = SYNCHRONIZE, ProcessDelete = DELETE, ProcessAllAccess = ProcessCreateProcess | ProcessCreateThread | ProcessDuplicateHandle | ProcessSetInformation | ProcessSetQuota | ProcessSuspendResume | ProcessTerminate | ProcessMemoryOperation | ProcessMemoryRead | ProcessMemoryWrite | ProcessSynchronize | ProcessDelete }; // enum ProcessRights enum ProcessWait { ProcessWaitInfinite = INFINITE }; // enum ProcessWait public: /* default constructor *@param: none */ ProcessHandle( void ); /* constructor *@param id: process id *@param rights: process access rights */ explicit ProcessHandle( const Id id, ProcessRights rights = ProcessAllAccess ); /* constructor *@param handle: existing handle */ explicit ProcessHandle( Native handle ); /* copy constructor *@param other: existing instance */ ProcessHandle( const ProcessHandle& other ); /* assignment operator *@param other: existing instance *@return: copied instance */ ProcessHandle& operator=( const ProcessHandle& other ); /* destructor */ ~ProcessHandle( void ); /* open process *@param id: process id *@param rights: process access rights *@return: true on success, false otherwise */ bool open( const Id id, ProcessRights rights = ProcessAllAccess ); /* open process *@param handle: existing handle *@return: true on success, false otherwise */ bool open( Native handle ); /* open process *@param other: existing instance *@return: true on success, false otherwise */ bool open( const ProcessHandle& other ); /* kill process *@param code: exit code *@return: true on success, false otherwise *@note: this will kill the actual process */ bool kill( unsigned int code = 0 ); /* wait for process to finish *@param time: time in ms. to wait *@return: true on success, false otherwise *@note: this will block the thread */ bool wait( unsigned int time = ProcessWaitInfinite ); /* close process *@param: none *@return: true on success, false otherwise *@note: this will only close the process handle */ bool close( void ); /* is process open *@param: none *@return: true on success, false otherwise */ bool isOpen( void ) const; /* process id accessor *@param: none *@return: process id on success, 0 otherwise */ const Id id( void ) const; /* process handle accessor *@param: none *@return: native handle *@note: as this breaks encapsulation it should not be used. it is only there for convenience for time being */ Native handle( void ) const; private: Native handle_; }; // class ProcessHandle /* is process open *@param: none *@return: true on success, false otherwise */ inline bool ProcessHandle::isOpen( void ) const { return handle_ != 0 && handle_ != INVALID_HANDLE_VALUE; } /* process id accessor *@param: none *@return: process id on success, 0 otherwise */ inline const ProcessHandle::Id ProcessHandle::id( void ) const { return ::GetProcessId( handle_ ); } /* process handle accessor *@param: none *@return native: handle *@note: as this breaks encapsulation it should not be used. it is only there for convenience for time being */ inline ProcessHandle::Native ProcessHandle::handle( void ) const { return handle_; } #endif // EVELATOR_PROCESS_HANDLE_HPP // // cpp #include "Globals.hpp" #include "ProcessHandle.hpp" /* default constructor *@param: none */ ProcessHandle::ProcessHandle( void ) : handle_( 0 ) { } /* constructor *@param id: process id *@param rights: process access rights */ ProcessHandle::ProcessHandle( const Id id, ProcessRights rights ) : handle_( 0 ) { this->open( id, rights ); } /* constructor *@param handle: existing handle */ ProcessHandle::ProcessHandle( Native handle ) : handle_( 0 ) { this->open( handle ); } /* copy constructor *@param other: existing instance */ ProcessHandle::ProcessHandle( const ProcessHandle& other ) : handle_( 0 ) { this->open( other.handle_ ); } /* assignment operator *@param other: existing instance *@return: copied instance */ ProcessHandle& ProcessHandle::operator=( const ProcessHandle& other ) { if ( this != &other ) { this->open( other.handle_ ); } // if return *this; } /* destructor */ ProcessHandle::~ProcessHandle( void ) { this->close(); } /* open process *@param id: process id *@param rights: process access rights *@return: true on success, false otherwise */ bool ProcessHandle::open( const Id id, ProcessRights rights ) { Native native = ::OpenProcess( rights, 0, id ); if ( 0 == native ) { return false; } // if if ( this->isOpen() ) { if ( !this->close() ) { ::CloseHandle( native ); return false; } // if } // if handle_ = native; return true; } /* open process *@param handle: existing handle *@return: true on success, false otherwise */ bool ProcessHandle::open( Native handle ) { Native native = 0; int result = ::DuplicateHandle( ::GetCurrentProcess(), handle, ::GetCurrentProcess(), &native, 0, 0, DUPLICATE_SAME_ACCESS ); if ( 0 == result ) { return false; } // if if ( this->isOpen() ) { if ( !this->close() ) { ::CloseHandle( native ); return false; } // if } // if handle_ = native; return true; } /* open process *@param other: existing instance *@return: true on success, false otherwise */ bool ProcessHandle::open( const ProcessHandle& other ) { return this->open( other.handle_ ); } /* kill process *@param code: exit code *@return: true on success, false otherwise *@note: this will kill the actual process */ bool ProcessHandle::kill( unsigned int code ) { int result = ::TerminateProcess( handle_, code ); if ( 0 == result ) { return false; } // if this->close(); return true; } /* wait for process to finish *@param time: time in ms. to wait *@return: true on success, false otherwise *@note: this will block the thread */ bool ProcessHandle::wait( unsigned int time ) { return WAIT_OBJECT_0 == ::WaitForSingleObject( handle_, static_cast< unsigned long >( time )); } /* close process *@param: none *@return: true on success, false otherwise *@note: this will only close the process handle */ bool ProcessHandle::close( void ) { int result = ::CloseHandle( handle_ ); if ( 0 == result ) { return false; } // if handle_ = 0; return true; }
So, as I said, has something like this already been done, or am I just wasting my time ?
I would appreciate the help.
*@note: I know the code is not exception friendly,
like that the assignment operator should throw on !open, so you do not need to tell me that.
I think there are things like this in the Boost library.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks