C++ 复制手柄,然后关闭原来的手柄

C++ 复制手柄,然后关闭原来的手柄,c++,winapi,C++,Winapi,我正在开发以下课程: class Handle { public: inline Handle() { handle = INVALID_HANDLE_VALUE; } inline Handle(HANDLE handle) { this->handle = copyHandle(handle); } inline Handle(const Handle& rhs) {

我正在开发以下课程:

class Handle
{
public:
    inline Handle()
    {
        handle = INVALID_HANDLE_VALUE;
    }
    inline Handle(HANDLE handle)
    {
        this->handle = copyHandle(handle);
    }
    inline Handle(const Handle& rhs)
    {
        this->handle = copyHandle(rhs.handle);
    }
    inline bool isValid()
    {
        return handle != INVALID_HANDLE_VALUE;
    }
    inline HANDLE getNativeHandle()
    {
        return copyHandle(this->handle);
    }
    inline void close()
    {
        if(handle != INVALID_HANDLE_VALUE)
        {
            CloseHandle(handle);
            handle = INVALID_HANDLE_VALUE;
        }

    }
    inline virtual ~Handle()
    {
        if(handle != INVALID_HANDLE_VALUE)
            CloseHandle(handle);
    }
protected:
    HANDLE handle;
    HANDLE copyHandle(HANDLE copyable);
};
.cpp文件:

HANDLE Handle::copyHandle(HANDLE copyable)
{
    HANDLE ret;
    HANDLE current = GetCurrentProcess();
    if(copyable == INVALID_HANDLE_VALUE)
        ret = copyable;

    else if(DuplicateHandle(current, copyable, current, &ret, 0, TRUE , DUPLICATE_SAME_ACCESS) == 0)
        {
            if(GetLastError() == ERROR_ACCESS_DENIED)
                throw SecurityException("The handle duplication was denied!");
            else
                throw InvalidHandleException("The handle could not be duplicated!");
        }

    return ret;
}
该类似乎正常工作,但复制句柄,然后关闭原始句柄,然后复制新句柄将引发异常或Windows错误代码6,这是“无效句柄值”

目前,我认为关闭原始手柄也会导致副本完全销毁,并使我以后无法使用它们

Handle test = CreateMutex(NULL, FALSE, NULL);
Handle copy = test;
test.close();
std::cout << copy.getNativeHandle() << std::endl; // throws an exception, but uses the same function as above
return 0;
handletest=CreateMutex(NULL、FALSE、NULL);
处理副本=测试;
test.close();

std::cout您还需要为
Handle
定义赋值运算符。我怀疑实际崩溃的代码是这样的:

Handle test = CreateMutex(NULL, FALSE, NULL);
Handle copy;
copy= test; // assigned instead of using copy constructor
test.close();
std::cout << copy.getNativeHandle() << std::endl;
return 0;
handletest=CreateMutex(NULL、FALSE、NULL);
处理复印件;
复制=测试;//指定而不是使用复制构造函数
test.close();

std::cout尝试以下实现:

class Handle
{
public:
    Handle(HANDLE ahandle = INVALID_HANDLE_VALUE)
    {
        handle = ahandle; // <- take ownership of the original, not a copy
    }

    Handle(const Handle& src)     
    {
        handle = src.duplicate(); // <-- take ownership of a copy
    }

    ~Handle()
    {
        close();
    }

    void close()
    {
        if (handle != INVALID_HANDLE_VALUE)
        {
            CloseHandle(handle);
            handle = INVALID_HANDLE_VALUE;
        }
    }

    HANDLE getNativeHandle() const
    {
        return handle;
    }

    bool isValid() const
    {
        return (handle != INVALID_HANDLE_VALUE);
    }

    HANDLE duplicate()
    {
        if (handle == INVALID_HANDLE_VALUE)
            return handle;

        HANDLE ret, current = GetCurrentProcess();
        if (!DuplicateHandle(current, handle, current, &ret, 0, TRUE, DUPLICATE_SAME_ACCESS))
        {
            if (GetLastError() == ERROR_ACCESS_DENIED)
                throw SecurityException("The handle duplication was denied!");
            else
                throw InvalidHandleException("The handle could not be duplicated!");
        }

        return ret;
    }

    Handle& operator=(HANDLE &rhs)
    {
        close();
        handle = rhs; // <-- take ownership of the original, not a copy
        return *this;
    }

    Handle& operator=(const Handle &rhs)
    {
        close();
        handle = rhs.duplicate(); // <-- take ownership of a copy
        return *this;
    }

protected:
    HANDLE handle;
};

旁白:
inline
隐含在类主体内定义的成员函数上。你不需要明确地陈述,大多数程序员都不需要。好吧,我改变了,谢谢你的帮助!两条注释:您没有定义复制赋值运算符,因此可以在不复制包含的句柄的情况下复制类。另外,您的意思是在
getNativeHandle
上复制句柄吗?这意味着类的所有客户端在每次检索时都需要手动关闭返回的句柄。它不会让你的类让客户端的生活变得更轻松。你的测试代码被破坏了,test.close()无法编译。当我修好它的时候,却找不到重做的机会。在施工人员中复制把手是一种有问题的做法,容易导致泄漏。请确保断言CloseHandle()的返回值。@jgpt:您尚未定义复制赋值运算符,因此编译器将为您生成一个直接赋值为
handle
的运算符。我现在添加了:`handle&operator=(const handle&other){this->close();this->handle=copyHandle(other.handle);return*this;}`有没有办法区分来自GetCurrentProcess的句柄和标准无效句柄?你是说
DuplicateHandle()
而不是
GetCurrentProcess()
GetCurrentProcess()
返回一个伪句柄。顺便说一句,没有办法查询句柄以知道它是否重复。无论哪种方式,这两种类型的句柄都不会设置为无效的句柄值。
struct InvalidHandleTrait
{
    static const HANDLE InvalidValue = INVALID_HANDLE_VALUE;
};

struct NullHandleTrait
{
    static const HANDLE InvalidValue = NULL;
};

struct CloseHandleTrait
{
    static bool close(HANDLE handle)
    {
        return CloseHandle(handle);
    }
};

template< typename HandleTrait = InvalidHandleTrait, typename CloseTrait = CloseHandleTrait >
class Handle
{
public:
    Handle(HANDLE ahandle = HandleTrait::InvalidValue)
    {
        handle = ahandle; // <- take ownership of the original, not a copy
    }

    Handle(const Handle& src)     
    {
        handle = src.duplicate(); // <-- take ownership of a copy
    }

    ~Handle()
    {
        close();
    }

    void close()
    {
        if (handle != HandleTrait::InvalidValue)
        {
            CloseTrait::close(handle);
            handle = HandleTrait::InvalidValue;
        }
    }

    HANDLE getNativeHandle() const
    {
        return handle;
    }

    bool isValid() const
    {
        return (handle != HandleTrait::InvalidValue);
    }

    HANDLE duplicate()
    {
        if (handle == HandleTrait::InvalidValue)
            return handle;

        HANDLE ret, current = GetCurrentProcess();
        if (!DuplicateHandle(current, handle, current, &ret, 0, TRUE, DUPLICATE_SAME_ACCESS))
        {
            if (GetLastError() == ERROR_ACCESS_DENIED)
                throw SecurityException("The handle duplication was denied!");
            else
                throw InvalidHandleException("The handle could not be duplicated!");
        }

        return ret;
    }

    Handle& operator=(HANDLE &rhs)
    {
        close();
        handle = rhs; // <-- take ownership of the original, not a copy
        return *this;
    }

    Handle& operator=(const Handle &rhs)
    {
        close();
        handle = rhs.duplicate(); // <-- take ownership of a copy
        return *this;
    }

protected:
    HANDLE handle;
};