OpenEvent的使用

原创  vista系统,OpenEvent函数调用,返回NULL调查 收藏

一个app,一个service.service端使用CreateEvent创建一个Event,app程序使用OpenEvent来获得被service创建的Event的句柄,然后 SetEvent函数等使Event处于由信号状态。再然后service捕捉到处于信号状态的Event,做相应的处理。
可是,在OpenEvent时,返回NULL。GetLastError()值为2,The system cannot find the file specified.
对于xp/nt系统,就没有问题。vista就返回NULL。
原因在于:
  xp系统,调用OpenEvent时没有指定Global\ 的话,通常作为 session 0来运行,而vista不是。
  所以,必须明确指出是Global Event。
 例:
CreateEvent( NULL, FALSE, FALSE, "Global\\CSAPP" );
OpenEvent( EVENT_ALL_ACCESS, FALSE, "Global\\CSAPP" );
同理:Mutex,Semaphore,Waitable timer,Job等也会有同样问题。详细参考msdn.
参考文献:
1. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=980340&SiteID=1
->  
   
   
   
   
Hi Everyone:
i have a application and a service(which run on LocalSystem),the service will a Create a gloable Event, and App will Open this Event. It seems ok on NT/XP, but on vista, when App Open this Event, it will return handle NULL. this is why? Thanks
answer:The corresponding error code (from GetLastError() right after the failing OpenEvent) would be most useful in confirming this but:
If it's 5, it's likely that the event is not ACLed properly so that it can be opened with the access required by the application (which probaly ran as admin on XP, and not anymore in Vista).
If it's 2, it's likely that the application doesn't specify Global\ when calling OpenEvent (on XP, applications often run in session 0, and on Vista, they never do).
2. msdn

OpenEvent

Opens an existing named event object.
HANDLE WINAPI OpenEvent(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCTSTR lpName
);

Parameters

dwDesiredAccess
[in] 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.
bInheritHandle
[in] If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.
lpName
[in] Pointer to a null-terminated string that names 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 name space. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Namespaces .
Windows XP Home Edition:  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.
Windows 2000:  If Terminal Services is not running, the "Global\" and "Local\" prefixes are ignored. The remainder of the name can contain any character except the backslash character.
Windows NT:  The name can contain any character except the backslash character.
Windows Me/98/95:  The name can contain any character except the backslash character. The empty string ("") is a valid object name.

Return Value

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 .

Remarks

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.

Requirements

Client Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header
Declared in Winbase.h; include Windows.h.
Library
Use Kernel32.lib.
DLL Requires Kernel32.dll.
Unicode
Implemented as OpenEventW (Unicode) and OpenEventA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.

你可能感兴趣的:(职场,休闲,OpenEvent)