1
我有一些事件句柄,我将它们添加到列表中。我想知道是否可以存储这些手柄,关闭本地手柄,然后再使用存储手柄。关闭手柄/存储手柄
例子:
std::map<std::string, HANDLE> Events;
DWORD OpenSingleEvent(std::string EventName, bool InheritHandle, DWORD dwDesiredAccess, DWORD dwMilliseconds)
{
Handle hEvent = OpenEvent(dwDesiredAccess, InheritHandle, EventName.c_str()); //Local Handle.
if (hEvent)
{
DeleteSingleEvent(EventName); //Delete the correct/old handle in the map.
Events.insert(EventName, hEvent); //Add this new handle to the map.
DWORD Result = WaitForSingleObject(hEvent, dwMilliseconds);
CloseHandle(hEvent); //Close THIS handle. Not the one in my Map.
return Result;
}
CloseHandle(hEvent); //Close this handle.
return WAIT_FAILED;
}
请问上面的工作?如果没有,是否有另一种方法来做到这一点?它用于共享内存通信,所以我不能复制句柄,因为我只有客户端PID而不是服务器。
也可以有人解释什么InheritHandle做?我使用的函数是OpenEvent,它具有该参数,但我不确定它的作用。
我也应该指出,在你的问题中,即使事件是NULL,你也调用'CloseHandle'。这不会造成任何伤害,但毫无意义。 – Benj
Handles的列表代表事件列表,我想我可以关闭本地的,然后使用ResetEvent和列表中的那个,等等。 这是一种无用的关闭它,我猜。我只是不喜欢把手柄打开的想法。还有什么继承手柄呢? – Brandon
@CantChooseUsernames一旦你调用了'CloseHandle',就没有事件再打开'ResetEvent'了。物体已经消失。您可以根据需要存储尽可能多的'HANDLE'副本,但是在内核中只有一个对象。 – Benj