Click here to Skip to main content
Licence CPOL
First Posted 26 Dec 2006
Views 15,280
Downloads 113
Bookmarked 14 times

CHandleT - A HANDLE wrapper for WTL

By | 19 Sep 2011 | Article
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:

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

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



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionMore versatile PinmemberRichard GILL9:48 26 Apr '12  
SuggestionA couple of suggested changes PinmemberDave Lowndes6:00 18 Sep '11  
GeneralRe: A couple of suggested changes Pinmemberisemenov22:47 18 Sep '11  

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

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

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