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