Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have a process that creates a named event, using ::CreateEvent.

In my process, I want to check whether the event exists or not, but I don't want to create the event in case it doesn't exist.

How can I do it?

I can do it like this, but then the event will be created in case it doesn't exist:

C++
HANDLE hEvent;
hEvent= ::CreateEvent(NULL, FALSE, FALSE, _T("MyEvent"));
    if (::GetLastError() != ERROR_ALREADY_EXISTS)
{
       .......
    }
Posted
Updated 11-Jun-12 0:17am
v3
Comments
Mohibur Rashid 11-Jun-12 6:13am    
I just read from MSDN that it will be successful only if the event is exists. So, I don't get you when you said "but I don't want to create the event in case it doesn't exist". It was not suppose to be created ..
user_code 11-Jun-12 6:17am    
Sorry, I had a mistake. I ment to use "CreateEvent" and not "OpenEvent". "OpenEvent" doesn't work for me at all..
I edited my question.
[no name] 11-Jun-12 6:31am    
And why OpenEvent does not work?
If the Event does not exists, OpenEvent retruns NULL and GetLastError returns ERROR_FILE_NOT_FOUND.
user_code 11-Jun-12 7:17am    
Thanks. I solved it. :) OpenEvent didn't work, because each time after I use it, I have to do CloseHandle.
[no name] 11-Jun-12 7:27am    
Of course you have to close the handle when OpenEvent was successfull. But where is the problem :)
WIN Help:
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.

Anyway glad you solved it.

1 solution

You can use OpenEvent() to test for existence of named event.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684305(v=vs.85).aspx[^]

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900