Click here to Skip to main content
15,881,248 members
Articles / Mobile Apps

CHandleT - A HANDLE wrapper for WTL

Rate me:
Please Sign up or sign in to vote.
4.27/5 (10 votes)
19 Sep 2011CPOL 34K   258   15   4
A simple Win32 HANDLE type wrapper.

Introduction

Here is a bit more flexible and comfortable alternative to the ATL::CHandle class. This class is templated to support handles with different zero-values (like INVALID_HANDLE_VALUE and NULL). Here is the source code:

C++
///////////////////////////////////////////////////////////////////////////////

// CAtomT - ATOM type wrapper


template<HANDLE t_hNullValue>
class CHandleT
{
public:
    HANDLE m_hHandle;

public:
    CHandleT() : m_hHandle(t_hNullValue)
    { }

    CHandleT(const CHandleT& Handle) :
        m_hHandle(Handle.Duplicate(::GetCurrentProcess(), 
                  DUPLICATE_SAME_ACCESS, FALSE, DUPLICATE_SAME_ACCESS))
    { }
    
    explicit CHandleT(HANDLE Handle) :
        m_hHandle(Handle)
    { }

    ~CHandleT()
    {
        if (IsValid())
        {
            Close();
        }
    }

    CHandleT& operator=(const CHandleT& Handle)
    {
        if (this != &Handle)
        {
            Close();
            m_hHandle = Handle.Duplicate(::GetCurrentProcess(), 
                        DUPLICATE_SAME_ACCESS, FALSE, DUPLICATE_SAME_ACCESS);
        }

        return (*this);
    }

    operator HANDLE() const
    {
        return m_hHandle;
    }

public:
    bool IsValid() const
    {
        return (m_hHandle != t_hNullValue);
    }

    void Attach(HANDLE Handle)
    {
        ATLASSERT(!IsValid());
        m_hHandle = Handle;
    }

    HANDLE Detach()
    {
        HANDLE Handle = m_hHandle;
        m_hHandle = t_hNullValue;
        return Handle;
    }

    void Close()
    {
        if (IsValid())
        {
            ATLVERIFY(::CloseHandle(m_hHandle) != FALSE);
            m_hHandle = t_hNullValue;
        }
    }

#if (_WIN32_WINNT >= 0x0400)
    DWORD GetInformation() const
    {
        ATLASSERT(IsValid());
        DWORD dwHandleInfo = 0;
        ATLVERIFY(::GetHandleInformation(m_hHandle, &dwHandleInfo) != FALSE);
        return dwHandleInfo;
    }

    bool IsFlagInherit() const
    {
        return (GetInformation() & HANDLE_FLAG_INHERIT) != 0;
    }

    bool IsFlagProtectFromClose() const
    {
        return (GetInformation() & HANDLE_FLAG_PROTECT_FROM_CLOSE) != 0;
    }

    void SetInformation(DWORD dwMask, DWORD dwFlags)
    {
        ATLASSERT(IsValid());
        ATLVERIFY(::SetHandleInformation(m_hHandle, dwMask, dwFlags) != FALSE);
    }

    void SetFlagInherit(bool bFlagInherit)
    {
        SetInformation(HANDLE_FLAG_INHERIT, (bFlagInherit) ? HANDLE_FLAG_INHERIT : 0);
    }

    void SetFlagProtectFromClose(bool bFlagProtectFromClose)
    {
        SetInformation(HANDLE_FLAG_PROTECT_FROM_CLOSE, 
          (bFlagProtectFromClose) ? HANDLE_FLAG_PROTECT_FROM_CLOSE : 0);
    }
#endif // (_WIN32_WINNT >= 0x0400)


    HANDLE Duplicate(HANDLE hTargetProcess, DWORD dwDesiredAccess, 
                     BOOL bInheritHandle = FALSE, DWORD dwOptions = 0) const
    {
        HANDLE hNewHandle = t_hNullValue;
		if (IsValid())
		{
			ATLVERIFY(::DuplicateHandle(::GetCurrentProcess(), m_hHandle, 
			          hTargetProcess, &hNewHandle, dwDesiredAccess, 
			          bInheritHandle, dwOptions) != FALSE);
		}
        return hNewHandle;
   }
};

typedef CHandleT<NULL> CHandle;
typedef CHandleT<INVALID_HANDLE_VALUE> CFileHandle;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to get HANDLE? Pin
ab_ba27-Dec-17 23:42
ab_ba27-Dec-17 23:42 
SuggestionMore versatile Pin
Richard GILL26-Apr-12 9:48
Richard GILL26-Apr-12 9:48 
SuggestionA couple of suggested changes Pin
Dave Lowndes18-Sep-11 6:00
Dave Lowndes18-Sep-11 6:00 
GeneralRe: A couple of suggested changes Pin
isemenov18-Sep-11 22:47
isemenov18-Sep-11 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.