Click here to Skip to main content
Click here to Skip to main content

CHandleT - A HANDLE wrapper for WTL

By , 19 Sep 2011
 

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:

///////////////////////////////////////////////////////////////////////////////

// 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)

About the Author

isemenov
Russian Federation Russian Federation
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionMore versatilememberRichard GILL26 Apr '12 - 9:48 
SuggestionA couple of suggested changesmemberDave Lowndes18 Sep '11 - 6:00 
GeneralRe: A couple of suggested changesmemberisemenov18 Sep '11 - 22:47 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 19 Sep 2011
Article Copyright 2006 by isemenov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid