annotate VBoxFrontend/Program.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
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.Reflection;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
3 using System.Runtime.InteropServices;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
4 using System.Windows.Forms;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
5 using System.Threading;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
6 using VirtualBox;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
7
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
8 namespace ConsoleApplication1 {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
9 class ProgramRun {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
10 public static VBoxXPCOM VBoxXPCOM { get; private set; }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
11 Object WaitingForProgress = null;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
12
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
13 public int Run(string[] args) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
14 IVirtualBox vb;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
15 ISession sess;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
16 if (Environment.OSVersion.Platform == PlatformID.Unix) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
17 VBoxXPCOM = new VBoxXPCOM();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
18 vb = VBoxXPCOM.VirtualBox;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
19 sess = VBoxXPCOM.Session;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
20 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
21 VBoxCOMInit(out vb, out sess);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
22 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
23
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
24 Console.WriteLine("VirtualBox version={0} revision={1}", vb.Version, vb.Revision);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
25 Console.WriteLine("VirtualBox HomeFolder={0}", vb.HomeFolder);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
26
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
27 if (args.Length < 1) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
28 Console.Error.WriteLine("Usage: {0} machine-name-or-id", Assembly.GetExecutingAssembly().Location);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
29 return 1;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
30 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
31 IMachine m;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
32 m = vb.FindMachine(args[0]);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
33
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
34 Console.WriteLine("Lock machine");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
35 m.LockMachine((Session)sess, LockType.LockType_Write);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
36 try {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
37 Framebuffer fb = new Framebuffer();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
38 sess.Console.Display.SetFramebuffer(0, fb);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
39
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
40 Console.WriteLine("Start GUI thread");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
41 Display disp = new Display(sess.Console, fb);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
42
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
43 Console.WriteLine("Power up...");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
44 //MonitorAndProcessEventsUntilCompleted(sess.Console.PowerUpPaused());
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
45
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
46 try {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
47 disp.MonitorUntilCompleted(sess.Console.PowerUpPaused());
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
48
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
49 //Console.WriteLine("Resume");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
50 //sess.Console.Resume();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
51
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
52 Application.Run(disp);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
53 } finally {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
54 if (VBoxXPCOM != null) VBoxXPCOM.EventQueue.ProcessPendingEvents();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
55 switch (sess.Console.State) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
56 case MachineState.MachineState_PoweredOff:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
57 case MachineState.MachineState_Saved:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
58 case MachineState.MachineState_Aborted:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
59 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
60 default:
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
61 Console.WriteLine("Power down");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
62 WaitingForProgress = null;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
63 ThreadPool.QueueUserWorkItem(delegate(Object state) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
64 Console.WriteLine("Requesting SaveState");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
65 IProgress p = sess.Console.SaveState();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
66 Console.WriteLine("Waiting for SaveState completion");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
67 WaitingForProgress = p;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
68 VBoxWaitComplete(p);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
69 });
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
70 ProcessEventsUntilCompleted();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
71 break;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
72 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
73 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
74 } finally {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
75 Console.WriteLine("Unlock machine");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
76 sess.UnlockMachine();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
77 if (VBoxXPCOM != null) VBoxXPCOM.EventQueue.ProcessPendingEvents();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
78 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
79 return 0;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
80 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
81
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
82 void VBoxCOMInit(out IVirtualBox vb, out ISession sess) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
83 //vb = (IVirtualBox)Activator.CreateInstance(Type.GetTypeFromProgID("VirtualBox.VirtualBox"));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
84 //vb = (IVirtualBox)Activator.CreateInstance(typeof(VirtualBoxClass));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
85 //vb = (IVirtualBox)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("B1A7A4F2-47B9-4A1E-82B2-07CCD5323C3F")));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
86 vb = new VirtualBox.VirtualBoxClass();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
87 sess = new VirtualBox.SessionClass();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
88 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
89
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
90 public void VBoxWaitComplete(IProgress p) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
91 uint prev = 0;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
92 while (p.Completed == 0) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
93 if (p.Percent != prev) Console.Write("{0}% ", p.Percent);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
94 prev = p.Percent;
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
95 p.WaitForCompletion(100);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
96 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
97 if (p.ResultCode == 0) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
98 Console.WriteLine("done");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
99 } else {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
100 Console.WriteLine("Error {0} {1}", p.ErrorInfo.ResultCode, p.ErrorInfo.Text);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
101 throw new COMException(p.ErrorInfo.Text, p.ErrorInfo.ResultCode);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
102 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
103 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
104
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
105 public void MonitorAndProcessEventsUntilCompleted(IProgress p) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
106 MonitorProgressAsync(p);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
107 ProcessEventsUntilCompleted(p);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
108 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
109 public void MonitorProgressAsync(IProgress p) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
110 ThreadPool.QueueUserWorkItem(delegate(Object state) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
111 try {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
112 VBoxWaitComplete(p);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
113 } catch (Exception ex) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
114 Console.WriteLine(ex.ToString());
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
115 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
116 });
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
117 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
118 public void ProcessEventsUntilCompleted(IProgress p) {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
119 do {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
120 if (VBoxXPCOM != null) VBoxXPCOM.EventQueue.ProcessPendingEvents();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
121 Thread.Sleep(100);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
122 } while (p.Canceled == 0 && p.Completed == 0);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
123 Console.WriteLine("Event processing completed");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
124 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
125 public void ProcessEventsUntilCompleted() {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
126 Console.WriteLine("Processing COM events...");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
127 do {
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
128 if (VBoxXPCOM != null) VBoxXPCOM.EventQueue.ProcessPendingEvents();
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
129 Thread.Sleep(100);
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
130 } while (WaitingForProgress == null || ((WaitingForProgress as IProgress).Canceled == 0 && (WaitingForProgress as IProgress).Completed == 0));
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
131 Console.WriteLine("COM Event processing completed");
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
132 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
133 }
e1ec7bf71313 Initial commit
Ivo Smits
parents:
diff changeset
134 }