view VBoxFrontend/VBoxEventListener.cs @ 7:a7650e26195f tip

Added support for generating Com Callable Wrappers
author Ivo Smits
date Fri, 06 May 2011 08:12:43 +0200
parents e1ec7bf71313
children
line wrap: on
line source
???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);
		}
	}
}