2011-12-23 49 views
1
关闭原始一个

我开发了以下类:复制手柄和事后

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 Errorcode 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; 

是否有重复的手柄,它不依赖于原来存在的可能性?

+0

旁白:'inline'是在成员函数隐含_defined_类体中。你不需要明确地陈述它,大多数程序员不需要。 – 2011-12-23 21:05:39

+0

好的,我改变了这一点,感谢您的帮助! – jgpt 2011-12-23 21:07:41

+1

两条评论:你没有定义一个复制赋值操作符,所以可以在不复制包含的句柄的情况下复制你的类,你的意思是复制'getNativeHandle'上的句柄?这意味着您的类的所有客户端在每次检索时都需要手动关闭返回的句柄。它不会让您的班级为客户让生活变得更加轻松。 – 2011-12-23 21:10:43

回答

0

您还需要为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; 

如果没有赋值运算符,则不会正确复制句柄。

+0

我现在增加了:'Handle&operator =(const Handle&other) { this-> close() ); this-> handle = copyHandle(other.handle); return * this; } – jgpt 2011-12-23 21:40:40

2

试试这个实施:

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; 
}; 

在一个侧面说明,一些API函数使用NULL代替INVALID_HANDLE_VALUE,有的不使用CloseHandle()。你应该考虑考虑这些差异。我建议更新Handle类使用C++模板,这样你就可以专注于每个实例的基础上的行为,如:

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; 
}; 
+0

有没有办法区分GetCurrentProcess中的Handle和标准的无效句柄? – jgpt 2011-12-23 22:40:22

+0

你的意思是'DuplicateHandle()'而不是'GetCurrentProcess()'? 'GetCurrentProcess()'返回一个伪句柄。 AFAIK,没有办法查询句柄,以知道它是否重复。无论哪种方式,这两种类型的句柄都不会被设置为“INVALID_HANDLE_VALUE”。 – 2011-12-24 16:24:35