How to get the name of an Event from a handle
I have an array of Win32 event handles that I'm waiting on using WaitForMultipleObjects(). This returns the index in the array of events that triggered but what I need to know is the name of the event. I've been looking through MSDN and can't see anything to do this.
Basically I have a class that m开发者_运维问答onitors the registry through events using RegNotifyChangeKeyValue() for a defined period of time but before it starts other classes register there interest in keys and values. I then wait on a separate thread and report back the name of the keys that have been modified. The event name is the key that the event is for and I don't know until runtime how many of these there will be or what they will be called. I don't want to create one thread per key as it's not very performant.
Does anyone know how to get the event name or a better way of doing this?
You could probably do it with undocumented NT stuff, maybe NtQueryObject(handle,ObjectNameInformation,....)
Personally I wouldn't do it that way. Create your own mapping (std::map?) between event and key name and then do your own lookup when an event is signalled.
Normally the name of an event is used to call OpenEvent() to get the Handle. This way, you don't need to communicate a handle at runtime, but instead settle on a naming convention for the name of the event.
I can think of three ways to do this:
- Loop over all hard-coded event names and call OpenEvent()
- Save the handles and names in an std::map
- Create a class to store your handles and names (possibly in an std::map), and write methods to quickly get a name from a handle.
Are you deciding what actions to take based on the name of the event? An 'if else if' statement that checks the names of the handles one-by-one to determine what action to take? This kind of scenario usually leads me to consider inheritance as a potential solution. Bear with me for a bit.
What if you create a base class, say EventAction. This has a handle to an event, and a virtual member function go_go_commandos(). You derive from it for each 'event' that has an action to be taken, and implement the action in the go_go_commandos() method of each deriving class.
Now what you need is a container so you can say actionlist->GetEventAction( handle )->go_go_commandos().
Did that help at all?
精彩评论