复制句柄,然后关闭原来的句柄
Duplicating a handle and closing the original one afterwards
我正在开发以下类:
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;
是否有可能复制句柄,这样它就不依赖于原始句柄的存在?
试试这个实现:
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;
};
您还需要为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;
如果没有赋值操作符,就不能正确地复制句柄。
相关文章:
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 如何获取边缘窗口句柄 (HWND)?
- 枚举进程模块在有效句柄上返回无效句柄
- 在 c/c++ 中打开 PhysicalDrive 的句柄并写入 MBR 的 linux 等效是什么?
- 从运行服务的应用程序代码中提取窗口句柄
- 在读取文件后重置句柄
- 是否有像地图这样的C++结构,但我得到的不是值的键,而是值的句柄?
- Microsoft ODBC 无法创建有效的句柄
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 获取特定进程的句柄数
- C++ 创建 NdisProt 驱动程序的句柄
- 是否需要关闭来自 WinHTTP 异步的句柄?
- 句柄OK全部崩溃
- 我需要通过窗口句柄(HWND)获取文件,我该怎么办?
- 什么是 C# 等同于C++句柄
- 未知C++错误:致命错误:glibc检测到无效的stdio句柄
- 获取现有进程句柄
- GetModuleHandle() 无法检索由 "notepad.exe" 加载的"advapi32.dll"的句柄
- 更正GLSL无绑定纹理句柄中的结构布局
- 复制句柄,然后关闭原来的句柄