gpt4 book ai didi

c++ - 复制一个句柄然后关闭原来的句柄

转载 作者:行者123 更新时间:2023-11-30 04:30:35 26 4
gpt4 key购买 nike

我正在开发以下类(class):

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;

是否有可能复制句柄,使其不依赖于原始句柄的存在?

最佳答案

试试这个实现:

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

关于c++ - 复制一个句柄然后关闭原来的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8620587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com