OpenEvent的使用注意点

我的测试代码如下

HANDLE hTest = CreateEvent(NULL, FALSE, FALSE, "Global\\Hello"); //Handle 1
HANDLE hGetEvent1 = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Global\\Hello"); //Handle 2
HANDLE hGetEvent2 = NULL;
if (hTest != NULL)
{
CloseHandle(hTest); //Close handle 1
hTest = NULL;

hGetEvent2 = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Global\\Hello"); //Handle 2 exists, so we can get handle 3
}



HANDLE hGetEvent1 = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Global\\Hello"); //Handle 1
HANDLE hTest = CreateEvent(NULL, FALSE, FALSE, "Global\\Hello"); //Handle 2
HANDLE hGetEvent2 = NULL;
if (hTest != NULL)
{
CloseHandle(hTest); //Close handle 2
hTest = NULL;

hGetEvent2 = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Global\\Hello"); //Handle 1 & Handle 2 are invalid, so we can not get handle 3
}


总结:
[b][color=red][size=medium]1)如果CreateEvent成功,那么OpenEvent也是可以成功的,运行多次就可以获取多个handle;
2)如果CreateEvent成功后,有一个或则多个OpenEvent是成功的,那么即使关闭最原始的Event,再次呼叫OpenEvent也是可以成功的;[/size][/color][/b]


[b]OpenEvent[/b] function


Opens an existing named event object.
[b]Syntax[/b]

HANDLE WINAPI OpenEvent(
_In_ DWORD dwDesiredAccess,
_In_ BOOL bInheritHandle,
_In_ LPCTSTR lpName
);


[b]Parameters[/b]
[i][b]dwDesiredAccess [in][/b][/i]
The access to the event object. The function fails if the security descriptor of the specified object does not permit the requested access for the calling process. For a list of access rights, see Synchronization Object Security and Access Rights.

[i][b]bInheritHandle [in][/b][/i]
If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.

[i][b]lpName [in][/b][/i]
The name of the event to be opened. Name comparisons are case sensitive.
This function can open objects in a private namespace. For more information, see Object Namespaces.
Terminal Services: The name can have a "Global\" or "Local\" prefix to explicitly open an object in the global or session namespace. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Namespaces.
Note Fast user switching is implemented using Terminal Services sessions. The first user to log on uses session 0, the next user to log on uses session 1, and so on. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

[b]Return value[/b]
If the function succeeds, the return value is a handle to the event object.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.

[b]Remarks[/b]
The OpenEvent function enables multiple processes to open handles of the same event object. The function succeeds only if some process has already created the event by using the CreateEvent function. The calling process can use the returned handle in any function that requires a handle to an event object, subject to the limitations of the access specified in the dwDesiredAccess parameter.
The handle can be duplicated by using the DuplicateHandle function. Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The event object is destroyed when its last handle has been closed.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值