tags:

views:

956

answers:

2

I'd like create a kernel(aka named events) from C#.

Do I have to interop services and wrap the native CreateEvent function or is there already a .NET class that does the job?

The function that I need to run should be something like this: hEvent = CreateEvent ( NULL , false , false , "MyCSHARPEvent" );

This should notify all procs that probing forMyCSHARPEvent about the event.

If there is a need to wrap the function, how would I translate the SECURITY_ATTRIBUTES struct from C# to win32?

+7  A: 

Take a look at the EventWaitHandle class. It's supported from .Net 2.0 onwards and allows creation of named events. It also supports setting event security, depending on which constructor you use.

Stu Mackellar
+3  A: 

If you still want to use interop, you can define the function like this:

[DllImport("kernel32.dll")]
static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);

and the struct like this(you may have to mess with the Pack attribute to make it fit):

[StructLayout(LayoutKind.Sequential)]
struct SECURITY_ATTRIBUTES{
  public int length;
  public IntPtr securityDesc;
  public bool inherit;
}

Also, here's a code example of putting it all together.

scottm
But why would you?
ctacke
if your supporting .net 1.1
JoshBerke
Wow - I thought only the CF was that crippled (it still doesn't support named EventWaitHandles).
ctacke
CF 3.5 now does support EventWaitHandles.
Sam