annotate VBoxFrontend/VBoxXPCOM.cs @ 2:f1deea9c97a0

Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
author Ivo Smits
date Wed, 04 May 2011 16:10:08 +0200
parents e1ec7bf71313
children
rev   line source
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
1 ???using System;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
2 using System.Runtime.InteropServices;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
3 using System.Runtime.CompilerServices;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
4 using VirtualBox;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
5
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
6 namespace ConsoleApplication1 {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
7 public class VBoxXPCOM {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
8 private delegate UInt32 pfnGetVersionDelegate();
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
9 private unsafe delegate void pfnComInitializeDelegate([MarshalAs(UnmanagedType.LPStr)] String pszVirtualBoxIID, out IntPtr ppVirtualBox, [MarshalAs(UnmanagedType.LPStr)] String pszSessionIID, out IntPtr ppSession);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
10 private delegate void pfnComUninitializeDelegate();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
11 private delegate void pfnGetEventQueueDelegate(out IntPtr eventQueue); //void (*pfnGetEventQueue)(nsIEventQueue **eventQueue);
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
12 private delegate void pfnComUnallocMem(IntPtr pv); //void (*pfnComUnallocMem)(void *pv);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
13
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
14 private struct VBOXXPCOMC {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
15 public UInt32 cb; //The size of the structure.
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
16 public UInt32 uVersion; //The structure version.
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
17 [MarshalAs(UnmanagedType.FunctionPtr)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
18 public pfnGetVersionDelegate pfnGetVersion;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
19 [MarshalAs(UnmanagedType.FunctionPtr)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
20 public pfnComInitializeDelegate pfnComInitialize;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
21 [MarshalAs(UnmanagedType.FunctionPtr)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
22 public pfnComUninitializeDelegate pfnComUninitialize;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
23 [MarshalAs(UnmanagedType.FunctionPtr)]
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
24 private pfnComUnallocMem pfnComUnallocMem;
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
25 [MarshalAs(UnmanagedType.FunctionPtr)]
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
26 private pfnComUnallocMem pfnUtf16Free; //void (*pfnUtf16Free)(PRUnichar *pwszString);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
27 [MarshalAs(UnmanagedType.FunctionPtr)]
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
28 private pfnComUnallocMem pfnUtf8Free; //void (*pfnUtf8Free)(char *pszString);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
29 [MarshalAs(UnmanagedType.FunctionPtr)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
30 private pfnComUninitializeDelegate pfnUtf16ToUtf8; //int (*pfnUtf16ToUtf8)(const PRUnichar *pwszString, char **ppszString);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
31 [MarshalAs(UnmanagedType.FunctionPtr)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
32 private pfnComUninitializeDelegate pfnUtf8ToUtf16; //int (*pfnUtf8ToUtf16)(const char *pszString, PRUnichar **ppwszString);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
33 [MarshalAs(UnmanagedType.FunctionPtr)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
34 public pfnGetEventQueueDelegate pfnGetEventQueue; //void (*pfnGetEventQueue)(nsIEventQueue **eventQueue);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
35 public UInt32 uEndVersion; //Tail version, same as uVersion.
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
36 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
37
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
38 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
39 [Guid("176afb41-00a4-11d3-9f2a-00400553eef0")]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
40 [ComImport()]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
41 public interface nsIEventQueue {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
42 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
43 void PostEvent(IntPtr aEvent); //[noscript] void postEvent (in PLEventPtr aEvent);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
44 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
45 Int32 IsOnCurrentThread(); //boolean isOnCurrentThread ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
46 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
47 void InitEvent(IntPtr aEvent, IntPtr owner, IntPtr handler, IntPtr destructor); //[noscript] void initEvent (in PLEventPtr aEvent, in voidPtr owner, in PLHandleEventProc handler, in PLDestroyEventProc destructor);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
48 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
49 void PostSynchronousEvent(IntPtr aEvent, out IntPtr aResult); //[noscript] void postSynchronousEvent (in PLEventPtr aEvent, out voidPtr aResult);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
50 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
51 Int32 PendingEvents(); //boolean pendingEvents ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
52 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
53 void ProcessPendingEvents(); //void processPendingEvents ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
54 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
55 void EventLoop(); //void eventLoop ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
56 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
57 void EventAvailable(ref Int32 aResult); //[noscript] void eventAvailable (in PRBoolRef aResult);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
58 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
59 IntPtr GetEvent(); //[noscript] PLEventPtr getEvent ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
60 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
61 void HandleEvent(IntPtr aEvent); //[noscript] void handleEvent (in PLEventPtr aEvent);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
62 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
63 IntPtr WaitForEvent(); //[noscript] PLEventPtr waitForEvent ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
64 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
65 Int32 GetEventQueueSelectFD(); //[notxpcom] PRInt32 getEventQueueSelectFD ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
66 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
67 void Init(Int32 aNative); //void init (in boolean aNative);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
68 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
69 void InitFromPRThread(IntPtr thread, Int32 aNative); //[noscript] void initFromPRThread (in PRThreadPtr thread, in boolean aNative);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
70 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
71 void InitFromPLQueue(IntPtr aQueue); //[noscript] void initFromPLQueue (in PLEventQueuePtr aQueue);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
72 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
73 void EnterMonitor(); //void enterMonitor ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
74 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
75 void ExitMonitor(); //void exitMonitor ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
76 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
77 void RevokeEvents(IntPtr owner); //[noscript] void revokeEvents (in voidPtr owner);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
78 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
79 IntPtr GetPLEventQueue(); //[noscript] PLEventQueuePtr getPLEventQueue ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
80 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
81 Int32 IsQueueNative(); //boolean isQueueNative ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
82 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
83 void StopAcceptingEvents(); //void stopAcceptingEvents ();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
84 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
85
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
86 public const UInt32 VBOX_XPCOMC_VERSION = 0x00020000U;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
87 [DllImport("/usr/lib/virtualbox/VBoxXPCOMC.so")]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
88 public static extern IntPtr VBoxGetXPCOMCFunctions(UInt32 uVersion);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
89
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
90 private VBOXXPCOMC FunctionTable;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
91 public IVirtualBox VirtualBox { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
92 public ISession Session { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
93 public nsIEventQueue EventQueue { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
94
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
95 //[STAThread]
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
96 public VBoxXPCOM() {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
97 IntPtr ftptr = VBoxGetXPCOMCFunctions(VBOX_XPCOMC_VERSION);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
98 //Console.WriteLine("Function table={0}", ftptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
99
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
100 FunctionTable = (VBOXXPCOMC)Marshal.PtrToStructure(ftptr, typeof(VBOXXPCOMC));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
101 //Console.WriteLine("Version {0}={1}={2}", VBOX_XPCOMC_VERSION, FunctionTable.uVersion, FunctionTable.uEndVersion);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
102 //Console.WriteLine("Version {0}", FunctionTable.pfnGetVersion());
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
103
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
104 IntPtr vbptr, sessptr, eqptr;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
105 FunctionTable.pfnComInitialize("D2DE270C-1D4B-4C9E-843F-BBB9B47269FF", out vbptr, "12F4DCDB-12B2-4EC1-B7CD-DDD9F6C5BF4D", out sessptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
106 FunctionTable.pfnGetEventQueue(out eqptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
107 //Console.WriteLine("VirtualBox={0} Session={1} EventQueue={2}", vbptr, sessptr, eqptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
108
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
109 VirtualBox = (IVirtualBox)Marshal.GetObjectForIUnknown(vbptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
110 Session = (ISession)Marshal.GetObjectForIUnknown(sessptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
111 EventQueue = (nsIEventQueue)Marshal.GetObjectForIUnknown(eqptr);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
112 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
113 ~VBoxXPCOM() {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
114 VirtualBox = null;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
115 Session = null;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
116 EventQueue = null;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
117 GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
2
f1deea9c97a0 Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
Ivo Smits
parents: 0
diff changeset
118 GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
0
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
119 FunctionTable.pfnComUninitialize();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
120 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
121 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
122 }