diff VBoxFrontend/VBoxEventListener.cs @ 0:e1ec7bf71313

Initial commit
author Ivo Smits
date Wed, 04 May 2011 00:59:43 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VBoxFrontend/VBoxEventListener.cs	Wed May 04 00:59:43 2011 +0200
@@ -0,0 +1,66 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using VirtualBox;
+
+namespace ConsoleApplication1 {
+	public class VBoxEventArgs : EventArgs {
+		public IEvent Event { get; private set; }
+		public VBoxEventArgs(IEvent e) {
+			Event = e;
+		}
+	}
+
+	[ClassInterface(ClassInterfaceType.None)]
+	[TypeLibType(TypeLibTypeFlags.FCanCreate)]
+	[ComVisible(true)]
+	public class VBoxEventListener : IEventListener {
+		[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+		[Guid("9b6e1aee-35f3-4f4d-b5bb-ed0ecefd8538")]
+		[ComImport()]
+		interface IEventSourceXPCOM {
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			[return: MarshalAs(UnmanagedType.Interface)]
+			IEventListener CreateListener();
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			[return: MarshalAs(UnmanagedType.Interface)]
+			IEventSource CreateAggregator([MarshalAs(UnmanagedType.SafeArray)] ref System.Array psubordinates);
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			void RegisterListener([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, UInt32 pinterestinglen, IntPtr pinteresting, Int32 pactive);
+			//void RegisterListener([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, UInt32 pinterestinglen, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] VBoxEventType[] pinteresting, Int32 pactive);
+
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			void UnregisterListener([MarshalAs(UnmanagedType.Interface)] IEventListener plistener);
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			Int32 FireEvent([MarshalAs(UnmanagedType.Interface)] IEvent pevent, Int32 ptimeout);
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			[return: MarshalAs(UnmanagedType.Interface)]
+			IEvent GetEvent([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, Int32 ptimeout);
+			[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
+			void EventProcessed([MarshalAs(UnmanagedType.Interface)] IEventListener plistener, [MarshalAs(UnmanagedType.Interface)] IEvent pevent);
+		}
+
+		public EventHandler<VBoxEventArgs> HandleEvent;
+		void IEventListener.HandleEvent(IEvent aEvent) {
+			Console.WriteLine("Event: {0}", aEvent.Type);
+			if (HandleEvent != null) HandleEvent(this, new VBoxEventArgs(aEvent));
+		}
+
+		public void RegisterSource(IEventSource src, params VBoxEventType[] events) {
+			if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.WinCE) {
+				Array eventTypesArray = (Array)events;
+				src.RegisterListener(this, ref eventTypesArray, 1);
+			} else {
+				unsafe {
+					fixed (VBoxEventType* ptr = events) {
+						((IEventSourceXPCOM)src).RegisterListener(this, (uint)events.Length, (IntPtr)ptr, 1);
+					}
+				}
+			}
+		}
+
+		public void UnregisterSource(IEventSource src) {
+			src.UnregisterListener(this);
+		}
+	}
+}