Closed Thread
Results 1 to 2 of 2

Thread: windows process and thread wrapping

  1. #1
    julmuri's Avatar
    julmuri is offline Programmer
    Join Date
    Dec 2008
    Location
    127.0.0.1
    Posts
    139
    Rep Power
    12

    windows process and thread wrapping

    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 ?

    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;
    }
    In particular, I am looking for options to set process / thread creation flags, suspend && resume em as I please.
    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: windows process and thread wrapping

    I think there are things like this in the Boost library.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Get Current Active Windows Process ID
    By mnirahd in forum C and C++
    Replies: 1
    Last Post: 01-18-2011, 12:15 PM
  2. Replies: 3
    Last Post: 12-09-2008, 12:18 AM
  3. Significance of System Process in Windows Xp
    By gamiR in forum Computer Software/OS
    Replies: 8
    Last Post: 09-09-2008, 09:08 AM
  4. Interrupt windows thread to get received string message
    By yonghan in forum Visual Basic Programming
    Replies: 2
    Last Post: 08-17-2008, 03:32 PM
  5. Wrapping Text
    By Ronin in forum HTML Programming
    Replies: 2
    Last Post: 12-04-2006, 09:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts