[DllImport("Kernel32.dll", SetLastError=true)]
static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName);
None.
None.
Tip 1: Use OpenEvent to open named event and attach it to AutoResetEvent class:
AutoResetEvent are= new AutoResetEvent(false);
are.Close();
GC.ReRegisterForFinalize(are);
are.Handle= handle; // handle from OpenEvent
// taken from header files
const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
const uint SYNCHRONIZE = 0x00100000;
const uint EVENT_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|0x3);
const uint EVENT_MODIFY_STATE = 0x0002 ;
const long ERROR_FILE_NOT_FOUND = 2L;
/// <summary>
/// Security enumeration from:
/// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/synchronization_object_security_and_access_rights.asp
/// </summary>
[Flags]
public enum SyncObjectAccess : uint
{
DELETE = 0x00010000,
READ_CONTROL = 0x00020000,
WRITE_DAC = 0x00040000,
WRITE_OWNER = 0x00080000,
SYNCHRONIZE = 0x00100000,
EVENT_ALL_ACCESS = 0x001F0003,
EVENT_MODIFY_STATE = 0x00000002,
MUTEX_ALL_ACCESS = 0x001F0001,
MUTEX_MODIFY_STATE = 0x00000001,
SEMAPHORE_ALL_ACCESS = 0x001F0003,
SEMAPHORE_MODIFY_STATE = 0x00000002,
TIMER_ALL_ACCESS = 0x001F0003,
TIMER_MODIFY_STATE = 0x00000002,
TIMER_QUERY_STATE = 0x00000001
}
// open event with error handling
IntPtr handle= OpenEvent(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, false, name);
int le= Marshal.GetLastWin32Error();
if ((handle==IntPtr.Zero) && (le!=0) && (le!=ERROR_FILE_NOT_FOUND))
throw new ApplicationException(string.Format("Error in pinvoked CreateEvent: {0}", le));
CreateEvent also opens an existing (named) event...
EventWaitHandle.OpenExisting already encapsulates this API: http://msdn.microsoft.com/en-us/library/433b98s3(v=vs.80).aspx
The EventWaitHandleRights enum is also already defined for you.