Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

OpenEvent (kernel32)
 
.
Summary

C# Signature:

[DllImport("Kernel32.dll", SetLastError=true)]
static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

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

Sample Code:

  // 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));

Alternative Managed API:

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.

Documentation
OpenEvent on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions