comparison VBoxFrontend/VBoxEventListener.cs @ 0:e1ec7bf71313

Initial commit
author Ivo Smits
date Wed, 04 May 2011 00:59:43 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e1ec7bf71313
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Runtime.CompilerServices;
4 using VirtualBox;
5
6 namespace ConsoleApplication1 {
7 public class VBoxEventArgs : EventArgs {
8 public IEvent Event { get; private set; }
9 public VBoxEventArgs(IEvent e) {
10 Event = e;
11 }
12 }
13
14 [ClassInterface(ClassInterfaceType.None)]
15 [TypeLibType(TypeLibTypeFlags.FCanCreate)]
16 [ComVisible(true)]
17 public class VBoxEventListener : IEventListener {
18 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
19 [Guid("9b6e1aee-35f3-4f4d-b5bb-ed0ecefd8538")]
20 [ComImport()]
21 interface IEventSourceXPCOM {
22 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
23 [return: MarshalAs(UnmanagedType.Interface)]
24 IEventListener CreateListener();
25 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
26 [return: MarshalAs(UnmanagedType.Interface)]
27 IEventSource CreateAggregator([MarshalAs(UnmanagedType.SafeArray)] ref System.Array psubordinates);
28 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
29 void RegisterListener([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, UInt32 pinterestinglen, IntPtr pinteresting, Int32 pactive);
30 //void RegisterListener([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, UInt32 pinterestinglen, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] VBoxEventType[] pinteresting, Int32 pactive);
31
32 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
33 void UnregisterListener([MarshalAs(UnmanagedType.Interface)] IEventListener plistener);
34 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
35 Int32 FireEvent([MarshalAs(UnmanagedType.Interface)] IEvent pevent, Int32 ptimeout);
36 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
37 [return: MarshalAs(UnmanagedType.Interface)]
38 IEvent GetEvent([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, Int32 ptimeout);
39 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
40 void EventProcessed([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, [MarshalAs(UnmanagedType.Interface)] IEvent pevent);
41 }
42
43 public EventHandler<VBoxEventArgs> HandleEvent;
44 void IEventListener.HandleEvent(IEvent aEvent) {
45 Console.WriteLine("Event: {0}", aEvent.Type);
46 if (HandleEvent != null) HandleEvent(this, new VBoxEventArgs(aEvent));
47 }
48
49 public void RegisterSource(IEventSource src, params VBoxEventType[] events) {
50 if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.WinCE) {
51 Array eventTypesArray = (Array)events;
52 src.RegisterListener(this, ref eventTypesArray, 1);
53 } else {
54 unsafe {
55 fixed (VBoxEventType* ptr = events) {
56 ((IEventSourceXPCOM)src).RegisterListener(this, (uint)events.Length, (IntPtr)ptr, 1);
57 }
58 }
59 }
60 }
61
62 public void UnregisterSource(IEventSource src) {
63 src.UnregisterListener(this);
64 }
65 }
66 }