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