VirtualBox

source: vbox/trunk/doc/manual/en_US/SDKRef.xml@ 34664

Last change on this file since 34664 was 34563, checked in by vboxsync, 14 years ago

VRDPAuth -> VBoxAuth.

File size: 180.1 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
4<book>
5 <bookinfo>
6 <title>$VBOX_PRODUCT<superscript>®</superscript></title>
7
8 <subtitle>Programming Guide and Reference</subtitle>
9
10 <edition>Version $VBOX_VERSION_STRING</edition>
11
12 <corpauthor>$VBOX_VENDOR</corpauthor>
13
14 <address>http://www.virtualbox.org</address>
15
16 <copyright>
17 <year>2004-$VBOX_C_YEAR</year>
18
19 <holder>$VBOX_VENDOR</holder>
20 </copyright>
21 </bookinfo>
22
23 <chapter>
24 <title>Introduction</title>
25
26 <para>VirtualBox comes with comprehensive support for third-party
27 developers. This Software Development Kit (SDK) contains all the
28 documentation and interface files that are needed to write code that
29 interacts with VirtualBox.</para>
30
31 <sect1>
32 <title>Modularity: the building blocks of VirtualBox</title>
33
34 <para>VirtualBox is cleanly separated into several layers, which can be
35 visualized like in the picture below:</para>
36
37 <mediaobject>
38 <imageobject>
39 <imagedata align="center" fileref="images/vbox-components.png"
40 width="12cm" />
41 </imageobject>
42 </mediaobject>
43
44 <para>The orange area represents code that runs in kernel mode, the blue
45 area represents userspace code.</para>
46
47 <para>At the bottom of the stack resides the hypervisor -- the core of
48 the virtualization engine, controlling execution of the virtual machines
49 and making sure they do not conflict with each other or whatever the
50 host computer is doing otherwise.</para>
51
52 <para>On top of the hypervisor, additional internal modules provide
53 extra functionality. For example, the RDP server, which can deliver the
54 graphical output of a VM remotely to an RDP client, is a separate module
55 that is only loosely tacked into the virtual graphics device. Live
56 Migration and Resource Monitor are additional modules currently in the
57 process of being added to VirtualBox.</para>
58
59 <para>What is primarily of interest for purposes of the SDK is the API
60 layer block that sits on top of all the previously mentioned blocks.
61 This API, which we call the <emphasis role="bold">"Main API"</emphasis>,
62 exposes the entire feature set of the virtualization engine below. It is
63 completely documented in this SDK Reference -- see <xref
64 linkend="sdkref_classes" /> and <xref linkend="sdkref_enums" /> -- and
65 available to anyone who wishes to control VirtualBox programmatically.
66 We chose the name "Main API" to differentiate it from other programming
67 interfaces of VirtualBox that may be publicly accessible.</para>
68
69 <para>With the Main API, you can create, configure, start, stop and
70 delete virtual machines, retrieve performance statistics about running
71 VMs, configure the VirtualBox installation in general, and more. In
72 fact, internally, the front-end programs
73 <computeroutput>VirtualBox</computeroutput> and
74 <computeroutput>VBoxManage</computeroutput> use nothing but this API as
75 well -- there are no hidden backdoors into the virtualization engine for
76 our own front-ends. This ensures the entire Main API is both
77 well-documented and well-tested. (The same applies to
78 <computeroutput>VBoxHeadless</computeroutput>, which is not shown in the
79 image.)</para>
80 </sect1>
81
82 <sect1 id="webservice-or-com">
83 <title>Two guises of the same "Main API": the web service or
84 COM/XPCOM</title>
85
86 <para>There are several ways in which the Main API can be called by
87 other code:<orderedlist>
88 <listitem>
89 <para>VirtualBox comes with a <emphasis role="bold">web
90 service</emphasis> that maps nearly the entire Main API. The web
91 service ships in a stand-alone executable
92 (<computeroutput>vboxwebsrv</computeroutput>) that, when running,
93 acts as an HTTP server, accepts SOAP connections and processes
94 them.</para>
95
96 <para>Since the entire web service API is publicly described in a
97 web service description file (in WSDL format), you can write
98 client programs that call the web service in any language with a
99 toolkit that understands WSDL. These days, that includes most
100 programming languages that are available: Java, C++, .NET, PHP,
101 Python, Perl and probably many more.</para>
102
103 <para>All of this is explained in detail in subsequent chapters of
104 this book.</para>
105
106 <para>There are two ways in which you can write client code that
107 uses the web service:<orderedlist>
108 <listitem>
109 <para>For Java as well as Python, the SDK contains
110 easy-to-use classes that allow you to use the web service in
111 an object-oriented, straightforward manner. We shall refer
112 to this as the <emphasis role="bold">"object-oriented web
113 service (OOWS)"</emphasis>.</para>
114
115 <para>The OO bindings for Java are described in <xref
116 linkend="javaapi" />, those for Python in <xref lang=""
117 linkend="glue-python-ws" />.</para>
118 </listitem>
119
120 <listitem>
121 <para>Alternatively, you can use the web service directly,
122 without the object-oriented client layer. We shall refer to
123 this as the <emphasis role="bold">"raw web
124 service"</emphasis>.</para>
125
126 <para>You will then have neither native object orientation
127 nor full type safety, since web services are neither
128 object-oriented nor stateful. However, in this way, you can
129 write client code even in languages for which we do not ship
130 object-oriented client code; all you need is a programming
131 language with a toolkit that can parse WSDL and generate
132 client wrapper code from it.</para>
133
134 <para>We describe this further in <xref
135 linkend="raw-webservice" />, with samples for Java and
136 Perl.</para>
137 </listitem>
138 </orderedlist></para>
139 </listitem>
140
141 <listitem>
142 <para>Internally, for portability and easier maintenance, the Main
143 API is implemented using the <emphasis role="bold">Component
144 Object Model (COM),</emphasis> an interprocess mechanism for
145 software components originally introduced by Microsoft for
146 Microsoft Windows. On a Windows host, VirtualBox will use
147 Microsoft COM; on other hosts where COM is not present, it ships
148 with XPCOM, a free software implementation of COM originally
149 created by the Mozilla project for their browsers.</para>
150
151 <para>So, if you are familiar with COM and the C++ programming
152 language (or with any other programming language that can handle
153 COM/XPCOM objects, such as Java, Visual Basic or C#), then you can
154 use the COM/XPCOM API directly. VirtualBox comes with all
155 necessary files and documentation to build fully functional COM
156 applications. For an introduction, please see <xref
157 linkend="api_com" /> below.</para>
158
159 <para>The VirtualBox front-ends (the graphical user interfaces as
160 well as the command line), which are all written in C++, use
161 COM/XPCOM to call the Main API. Technically, the web service is
162 another front-end to this COM API, mapping almost all of it to
163 SOAP clients.</para>
164 </listitem>
165 </orderedlist></para>
166
167 <para>If you wonder which way to choose, here are a few
168 comparisons:<table>
169 <title>Comparison web service vs. COM/XPCOM</title>
170
171 <tgroup cols="2">
172 <tbody>
173 <row>
174 <entry><emphasis role="bold">Web service</emphasis></entry>
175
176 <entry><emphasis role="bold">COM/XPCOM</emphasis></entry>
177 </row>
178
179 <row>
180 <entry><emphasis role="bold">Pro:</emphasis> Easy to use with
181 Java and Python with the object-oriented web service;
182 extensive support even with other languages (C++, .NET, PHP,
183 Perl and others)</entry>
184
185 <entry><emphasis role="bold">Con:</emphasis> Usable from
186 languages where COM bridge available (most languages on
187 Windows platform, Python and C++ on other hosts)</entry>
188 </row>
189
190 <row>
191 <entry><emphasis role="bold">Pro:</emphasis> Client can be on
192 remote machine</entry>
193
194 <entry><emphasis role="bold">Con: </emphasis>Client must be on
195 the same host where virtual machine is executed</entry>
196 </row>
197
198 <row>
199 <entry><emphasis role="bold">Con: </emphasis>Significant
200 overhead due to XML marshalling over the wire for each method
201 call</entry>
202
203 <entry><emphasis role="bold">Pro: </emphasis>Relatively low
204 invocation overhead</entry>
205 </row>
206 </tbody>
207 </tgroup>
208 </table></para>
209
210 <para>In the following chapters, we will describe the different ways in
211 which to program VirtualBox, starting with the method that is easiest to
212 use and then increase complexity as we go along.</para>
213 </sect1>
214
215 <sect1 id="api_soap_intro">
216 <title>About web services in general</title>
217
218 <para>Web services are a particular type of programming interface.
219 Whereas, with "normal" programming, a program calls an application
220 programming interface (API) defined by another program or the operating
221 system and both sides of the interface have to agree on the calling
222 convention and, in most cases, use the same programming language, web
223 services use Internet standards such as HTTP and XML to
224 communicate.<footnote>
225 <para>In some ways, web services promise to deliver the same thing
226 as CORBA and DCOM did years ago. However, while these previous
227 technologies relied on specific binary protocols and thus proved to
228 be difficult to use between diverging platforms, web services
229 circumvent these incompatibilities by using text-only standards like
230 HTTP and XML. On the downside (and, one could say, typical of things
231 related to XML), a lot of standards are involved before a web
232 service can be implemented. Many of the standards invented around
233 XML are used one way or another. As a result, web services are slow
234 and verbose, and the details can be incredibly messy. The relevant
235 standards here are called SOAP and WSDL, where SOAP describes the
236 format of the messages that are exchanged (an XML document wrapped
237 in an HTTP header), and WSDL is an XML format that describes a
238 complete API provided by a web service. WSDL in turn uses XML Schema
239 to describe types, which is not exactly terse either. However, as
240 you will see from the samples provided in this chapter, the
241 VirtualBox web service shields you from these details and is easy to
242 use.</para>
243 </footnote></para>
244
245 <para>In order to successfully use a web service, a number of things are
246 required -- primarily, a web service accepting connections; service
247 descriptions; and then a client that connects to that web service. The
248 connections are governed by the SOAP standard, which describes how
249 messages are to be exchanged between a service and its clients; the
250 service descriptions are governed by WSDL.</para>
251
252 <para>In the case of VirtualBox, this translates into the following
253 three components:<orderedlist>
254 <listitem>
255 <para>The VirtualBox web service (the "server"): this is the
256 <computeroutput>vboxwebsrv</computeroutput> executable shipped
257 with VirtualBox. Once you start this executable (which acts as a
258 HTTP server on a specific TCP/IP port), clients can connect to the
259 web service and thus control a VirtualBox installation.</para>
260 </listitem>
261
262 <listitem>
263 <para>VirtualBox also comes with WSDL files that describe the
264 services provided by the web service. You can find these files in
265 the <computeroutput>sdk/bindings/webservice/</computeroutput>
266 directory. These files are understood by the web service toolkits
267 that are shipped with most programming languages and enable you to
268 easily access a web service even if you don't use our
269 object-oriented client layers. VirtualBox is shipped with
270 pregenerated web service glue code for several languages (Python,
271 Perl, Java).</para>
272 </listitem>
273
274 <listitem>
275 <para>A client that connects to the web service in order to
276 control the VirtualBox installation.</para>
277
278 <para>Unless you play with some of the samples shipped with
279 VirtualBox, this needs to be written by you.</para>
280 </listitem>
281 </orderedlist></para>
282 </sect1>
283
284 <sect1 id="runvboxwebsrv">
285 <title>Running the web service</title>
286
287 <para>The web service ships in an stand-alone executable,
288 <computeroutput>vboxwebsrv</computeroutput>, that, when running, acts as
289 a HTTP server, accepts SOAP connections and processes them -- remotely
290 or from the same machine.<note>
291 <para>The web service executable is not contained with the
292 VirtualBox SDK, but instead ships with the standard VirtualBox
293 binary package for your specific platform. Since the SDK contains
294 only platform-independent text files and documentation, the binaries
295 are instead shipped with the platform-specific packages.</para>
296 </note></para>
297
298 <para>The <computeroutput>vboxwebsrv</computeroutput> program, which
299 implements the web service, is a text-mode (console) program which,
300 after being started, simply runs until it is interrupted with Ctrl-C or
301 a kill command.</para>
302
303 <para>Once the web service is started, it acts as a front-end to the
304 VirtualBox installation of the user account that it is running under. In
305 other words, if the web service is run under the user account of
306 <computeroutput>user1</computeroutput>, it will see and manipulate the
307 virtual machines and other data represented by the VirtualBox data of
308 that user (e.g., on a Linux machine, under
309 <computeroutput>/home/user1/.VirtualBox</computeroutput>; see the
310 VirtualBox User Manual for details on where this data is stored).</para>
311
312 <sect2 id="vboxwebsrv-ref">
313 <title>Command line options of vboxwebsrv</title>
314
315 <para>The web service supports the following command line
316 options:</para>
317
318 <itemizedlist>
319 <listitem>
320 <para><computeroutput>--help</computeroutput> (or
321 <computeroutput>-h</computeroutput>): print a brief summary of
322 command line options.</para>
323 </listitem>
324
325 <listitem>
326 <para><computeroutput>--background</computeroutput> (or
327 <computeroutput>-b</computeroutput>): run the web service as a
328 background daemon. This option is not supported on Windows
329 hosts.</para>
330 </listitem>
331
332 <listitem>
333 <para><computeroutput>--host</computeroutput> (or
334 <computeroutput>-H</computeroutput>): This specifies the host to
335 bind to and defaults to "localhost".</para>
336 </listitem>
337
338 <listitem>
339 <para><computeroutput>--port</computeroutput> (or
340 <computeroutput>-p</computeroutput>): This specifies which port to
341 bind to on the host and defaults to 18083.</para>
342 </listitem>
343
344 <listitem>
345 <para><computeroutput>--timeout</computeroutput> (or
346 <computeroutput>-t</computeroutput>): This specifies the session
347 timeout, in seconds, and defaults to 300 (five minutes). A web
348 service client that has logged on but makes no calls to the web
349 service will automatically be disconnected after the number of
350 seconds specified here, as if it had called the
351 <computeroutput>IWebSessionManager::logoff()</computeroutput>
352 method provided by the web service itself.</para>
353
354 <para>It is normally vital that each web service client call this
355 method, as the web service can accumulate large amounts of memory
356 when running, especially if a web service client does not properly
357 release managed object references. As a result, this timeout value
358 should not be set too high, especially on machines with a high
359 load on the web service, or the web service may eventually deny
360 service.</para>
361 </listitem>
362
363 <listitem>
364 <para><computeroutput>--check-interval</computeroutput> (or
365 <computeroutput>-i</computeroutput>): This specifies the interval
366 in which the web service checks for timed-out clients, in seconds,
367 and defaults to 5. This normally does not need to be
368 changed.</para>
369 </listitem>
370
371 <listitem>
372 <para><computeroutput>--verbose</computeroutput> (or
373 <computeroutput>-v</computeroutput>): Normally, the webservice
374 outputs only brief messages to the console each time a request is
375 served. With this option, the webservice prints much more detailed
376 data about every request and the COM methods that those requests
377 are mapped to internally, which can be useful for debugging client
378 programs.</para>
379 </listitem>
380
381 <listitem>
382 <para><computeroutput>--logfile</computeroutput> (or
383 <computeroutput>-F</computeroutput>)
384 <computeroutput>&lt;file&gt;</computeroutput>: If this is
385 specified, the webservice not only prints its output to the
386 console, but also writes it to the specified file. The file is
387 created if it does not exist; if it does exist, new output is
388 appended to it. This is useful if you run the webservice
389 unattended and need to debug problems after they have
390 occurred.</para>
391 </listitem>
392 </itemizedlist>
393 </sect2>
394
395 <sect2 id="websrv_authenticate">
396 <title>Authenticating at web service logon</title>
397
398 <para>As opposed to the COM/XPCOM variant of the Main API, a client
399 that wants to use the web service must first log on by calling the
400 <computeroutput>IWebsessionManager::logon()</computeroutput> API (see
401 <xref linkend="IWebsessionManager__logon" />) that is specific to the
402 web service. Logon is necessary for the web service to be stateful;
403 internally, it maintains a session for each client that connects to
404 it.</para>
405
406 <para>The <computeroutput>IWebsessionManager::logon()</computeroutput>
407 API takes a user name and a password as arguments, which the web
408 service then passes to a customizable authentication plugin that
409 performs the actual authentication.</para>
410
411 <para>For testing purposes, it is recommended that you first disable
412 authentication with this command:<screen>VBoxManage setproperty websrvauthlibrary null</screen></para>
413
414 <para><warning>
415 <para>This will cause all logons to succeed, regardless of user
416 name or password. This should of course not be used in a
417 production environment.</para>
418 </warning>Generally, the mechanism by which clients are
419 authenticated is configurable by way of the
420 <computeroutput>VBoxManage</computeroutput> command:</para>
421
422 <para><screen>VBoxManage setproperty websrvauthlibrary default|null|&lt;library&gt;</screen></para>
423
424 <para>This way you can specify any shared object/dynamic link module
425 that conforms with the specifications for authentication modules as
426 laid out in section 9.3 of the VirtualBox User Manual; the web service
427 uses the same kind of modules as the VirtualBox RDP server.</para>
428
429 <para>By default, after installation, the web service uses the
430 VBoxAuth module that ships with VirtualBox. This module uses PAM on
431 Linux hosts to authenticate users. Any valid username/password
432 combination is accepted, it does not have to be the username and
433 password of the user running the webservice daemon. Unless
434 <computeroutput>vboxwebsrv</computeroutput> runs as root, PAM
435 authentication can fail, because sometimes the file
436 <computeroutput>/etc/shadow</computeroutput>, which is used by PAM, is
437 not readable. On most Linux distribution PAM uses a suid root helper
438 internally, so make sure you test this before deploying it. One can
439 override this behavior by setting the environment variable
440 <computeroutput>VBOX_PAM_ALLOW_INACTIVE</computeroutput> which will
441 suppress failures when unable to read the shadow password file. Please
442 use this variable carefully, and only if you fully understand what
443 you're doing.</para>
444 </sect2>
445
446 <sect2>
447 <title>Solaris host: starting the web service via SMF</title>
448
449 <para>On Solaris hosts, the VirtualBox web service daemon is
450 integrated into the SMF framework. You can change the parameters, but
451 don't have to if the defaults below already match your needs:<screen>svccfg -s svc:/application/virtualbox/webservice:default setprop config/host=localhost
452svccfg -s svc:/application/virtualbox/webservice:default setprop config/port=18083
453svccfg -s svc:/application/virtualbox/webservice:default setprop config/user=root</screen></para>
454
455 <para>If you made any change, don't forget to run the following
456 command to put the changes into effect immediately:<screen>svcadm refresh svc:/application/virtualbox/webservice:default</screen></para>
457
458 <para>If you forget the above command then the previous settings will
459 be used when enabling the service. Check the current property settings
460 with:<screen>svcprop -p config svc:/application/virtualbox/webservice:default</screen></para>
461
462 <para>When everything is configured correctly you can start the
463 VirtualBox webservice with the following command:<screen>svcadm enable svc:/application/virtualbox/webservice:default</screen></para>
464
465 <para>For more information about SMF, please refer to the Solaris
466 documentation.</para>
467 </sect2>
468 </sect1>
469 </chapter>
470
471 <chapter>
472 <title>Environment-specific notes</title>
473
474 <para>The Main API described in <xref linkend="sdkref_classes" /> and
475 <xref linkend="sdkref_enums" /> is mostly identical in all the supported
476 programming environments which have been briefly mentioned in the
477 introduction of this book. As a result, the Main API's general concepts
478 described in <xref linkend="concepts" /> are the same whether you use the
479 object-oriented web service (OOWS) for JAX-WS or a raw web service
480 connection via, say, Perl, or whether you use C++ COM bindings.</para>
481
482 <para>Some things are different depending on your environment, however.
483 These differences are explained in this chapter.</para>
484
485 <sect1 id="glue">
486 <title>Using the object-oriented web service (OOWS)</title>
487
488 <para>As explained in <xref linkend="webservice-or-com" />, VirtualBox
489 ships with client-side libraries for Java, Python and PHP that allow you
490 to use the VirtualBox web service in an intuitive, object-oriented way.
491 These libraries shield you from the client-side complications of managed
492 object references and other implementation details that come with the
493 VirtualBox web service. (If you are interested in these complications,
494 have a look at <xref linkend="raw-webservice" />).</para>
495
496 <para>We recommend that you start your experiments with the VirtualBox
497 web service by using our object-oriented client libraries for JAX-WS, a
498 web service toolkit for Java, which enables you to write code to
499 interact with VirtualBox in the simplest manner possible.</para>
500
501 <para>As "interfaces", "attributes" and "methods" are COM concepts,
502 please read the documentation in <xref linkend="sdkref_classes" /> and
503 <xref linkend="sdkref_enums" /> with the following notes in mind.</para>
504
505 <para>The OOWS bindings attempt to map the Main API as closely as
506 possible to the Java, Python and PHP languages. In other words, objects
507 are objects, interfaces become classes, and you can call methods on
508 objects as you would on local objects.</para>
509
510 <para>The main difference remains with attributes: to read an attribute,
511 call a "getXXX" method, with "XXX" being the attribute name with a
512 capitalized first letter. So when the Main API Reference says that
513 <computeroutput>IMachine</computeroutput> has a "name" attribute (see
514 <xref linkend="IMachine__name" xreflabel="IMachine::name" />), call
515 <computeroutput>getName()</computeroutput> on an IMachine object to
516 obtain a machine's name. Unless the attribute is marked as read-only in
517 the documentation, there will also be a corresponding "set"
518 method.</para>
519
520 <sect2 id="glue-jax-ws">
521 <title>The object-oriented web service for JAX-WS</title>
522
523 <para>JAX-WS is a powerful toolkit by Sun Microsystems to build both
524 server and client code with Java. It is part of Java 6 (JDK 1.6), but
525 can also be obtained separately for Java 5 (JDK 1.5). The VirtualBox
526 SDK comes with precompiled OOWS bindings working with both Java 5 and
527 6.</para>
528
529 <para>The following sections explain how to get the JAX-WS sample code
530 running and explain a few common practices when using the JAX-WS
531 object-oriented web service.</para>
532
533 <sect3>
534 <title>Preparations</title>
535
536 <para>Since JAX-WS is already integrated into Java 6, no additional
537 preparations are needed for Java 6.</para>
538
539 <para>If you are using Java 5 (JDK 1.5.x), you will first need to
540 download and install an external JAX-WS implementation, as Java 5
541 does not support JAX-WS out of the box; for example, you can
542 download one from here: <ulink
543 url="https://jax-ws.dev.java.net/2.1.4/JAXWS2.1.4-20080502.jar">https://jax-ws.dev.java.net/2.1.4/JAXWS2.1.4-20080502.jar</ulink>.
544 Then perform the installation (<computeroutput>java -jar
545 JAXWS2.1.4-20080502.jar</computeroutput>).</para>
546 </sect3>
547
548 <sect3>
549 <title>Getting started: running the sample code</title>
550
551 <para>To run the OOWS for JAX-WS samples that we ship with the SDK,
552 perform the following steps: <orderedlist>
553 <listitem>
554 <para>Open a terminal and change to the directory where the
555 JAX-WS samples reside.<footnote>
556 <para>In
557 <computeroutput>sdk/bindings/webservice/java/jax-ws/samples/</computeroutput>.</para>
558 </footnote> Examine the header of
559 <computeroutput>Makefile</computeroutput> to see if the
560 supplied variables (Java compiler, Java executable) and a few
561 other details match your system settings.</para>
562 </listitem>
563
564 <listitem>
565 <para>To start the VirtualBox web service, open a second
566 terminal and change to the directory where the VirtualBox
567 executables are located. Then type:<screen>./vboxwebsrv -v</screen></para>
568
569 <para>The web service now waits for connections and will run
570 until you press Ctrl+C in this second terminal. The -v
571 argument causes it to log all connections to the terminal.
572 (See <xref linkend="runvboxwebsrv" os="" /> for details on how
573 to run the web service.)</para>
574 </listitem>
575
576 <listitem>
577 <para>Back in the first terminal and still in the samples
578 directory, to start a simple client example just type:<screen>make run16</screen></para>
579
580 <para>if you're on a Java 6 system; on a Java 5 system, run
581 <computeroutput>make run15</computeroutput> instead.</para>
582
583 <para>This should work on all Unix-like systems such as Linux
584 and Solaris. For Windows systems, use commands similar to what
585 is used in the Makefile.</para>
586
587 <para>This will compile the
588 <computeroutput>clienttest.java</computeroutput> code on the
589 first call and then execute the resulting
590 <computeroutput>clienttest</computeroutput> class to show the
591 locally installed VMs (see below).</para>
592 </listitem>
593 </orderedlist></para>
594
595 <para>The <computeroutput>clienttest</computeroutput> sample
596 imitates a few typical command line tasks that
597 <computeroutput>VBoxManage</computeroutput>, VirtualBox's regular
598 command-line front-end, would provide (see the VirtualBox User
599 Manual for details). In particular, you can run:<itemizedlist>
600 <listitem>
601 <para><computeroutput>java clienttest show
602 vms</computeroutput>: show the virtual machines that are
603 registered locally.</para>
604 </listitem>
605
606 <listitem>
607 <para><computeroutput>java clienttest list
608 hostinfo</computeroutput>: show various information about the
609 host this VirtualBox installation runs on.</para>
610 </listitem>
611
612 <listitem>
613 <para><computeroutput>java clienttest startvm
614 &lt;vmname|uuid&gt;</computeroutput>: start the given virtual
615 machine.</para>
616 </listitem>
617 </itemizedlist></para>
618
619 <para>The <computeroutput>clienttest.java</computeroutput> sample
620 code illustrates common basic practices how to use the VirtualBox
621 OOWS for JAX-WS, which we will explain in more detail in the
622 following chapters.</para>
623 </sect3>
624
625 <sect3>
626 <title>Logging on to the web service</title>
627
628 <para>Before a web service client can do anything useful, two
629 objects need to be created, as can be seen in the
630 <computeroutput>clienttest</computeroutput> constructor:<orderedlist>
631 <listitem>
632 <para>An instance of <xref linkend="IWebsessionManager"
633 xreflabel="IWebsessionManager" />, which is an interface
634 provided by the web service to manage "web sessions" -- that
635 is, stateful connections to the web service with persistent
636 objects upon which methods can be invoked.</para>
637
638 <para>In the OOWS for JAX-WS, the IWebsessionManager class
639 must be constructed explicitly, and a URL must be provided in
640 the constructor that specifies where the web service (the
641 server) awaits connections. The code in
642 <computeroutput>clienttest.java</computeroutput> connects to
643 "http://localhost:18083/", which is the default.</para>
644
645 <para>The port number, by default 18083, must match the port
646 number given to the
647 <computeroutput>vboxwebsrv</computeroutput> command line; see
648 <xref linkend="vboxwebsrv-ref" />.</para>
649 </listitem>
650
651 <listitem>
652 <para>After that, the code calls <xref
653 linkend="IWebsessionManager__logon"
654 xreflabel="IWebsessionManager::logon()" />, which is the first
655 call that actually communicates with the server. This
656 authenticates the client with the web service and returns an
657 instance of <xref linkend="IVirtualBox"
658 xreflabel="IVirtualBox" />, the most fundamental interface of
659 the VirtualBox web service, from which all other functionality
660 can be derived.</para>
661
662 <para>If logon doesn't work, please take another look at <xref
663 linkend="websrv_authenticate" />.</para>
664 </listitem>
665 </orderedlist></para>
666 </sect3>
667
668 <sect3>
669 <title>Object management</title>
670
671 <para>The current OOWS for JAX-WS has certain memory management
672 related limitations. When you no longer need an object, call its
673 <xref linkend="IManagedObjectRef__release"
674 xreflabel="IManagedObjectRef::release()" /> method explicitly, which
675 frees appropriate managed reference, as is required by the raw
676 webservice; see <xref linkend="managed-object-references" /> for
677 details. This limitation may be reconsidered in a future version of
678 the VirtualBox SDK.</para>
679 </sect3>
680 </sect2>
681
682 <sect2 id="glue-python-ws">
683 <title>The object-oriented web service for Python</title>
684
685 <para>VirtualBox comes with two flavors of a Python API: one for web
686 service, discussed here, and one for the COM/XPCOM API discussed in
687 <xref linkend="pycom" />. The client code is mostly similar, except
688 for the initialization part, so it is up to the application developer
689 to choose the appropriate technology. Moreover, a common Python glue
690 layer exists, abstracting out concrete platform access details, see
691 <xref linkend="glue-python" />.</para>
692
693 <para>As indicated in <xref linkend="webservice-or-com" />, the
694 COM/XPCOM API gives better performance without the SOAP overhead, and
695 does not require a web server to be running. On the other hand, the
696 COM/XPCOM Python API requires a suitable Python bridge for your Python
697 installation (VirtualBox ships the most important ones for each
698 platform<footnote>
699 <para>On On Mac OS X only the Python versions bundled with the OS
700 are officially supported. This means Python 2.3 for 10.4, Python
701 2.5 for 10.5 and Python 2.5 and 2.6 for 10.6.</para>
702 </footnote>), and you cannot connect to VirtualBox remotely. On
703 Windows, you can use the Main API from Python if the Win32 extensions
704 package for Python<footnote>
705 <para>See <ulink
706 url="http://sourceforge.net/project/showfiles.php?group_id=78018">http://sourceforge.net/project/showfiles.php?group_id=78018</ulink>.</para>
707 </footnote> is installed.</para>
708
709 <para>The VirtualBox OOWS for Python relies on the Python ZSI SOAP
710 implementation (see <ulink
711 url="http://pywebsvcs.sourceforge.net/zsi.html">http://pywebsvcs.sourceforge.net/zsi.html</ulink>),
712 which you will need to install locally before trying the examples.
713 Most Linux distributions come with package for ZSI, such as
714 <computeroutput>python-zsi</computeroutput> in Ubuntu.</para>
715
716 <para>To get started, open a terminal and change to the
717 <computeroutput>bindings/glue/python/sample</computeroutput>
718 directory, which contains an example of a simple interactive shell
719 able to control a VirtualBox instance. The shell is written using the
720 API layer, thereby hiding different implementation details, so it is
721 actually an example of code share among XPCOM, MSCOM and web services.
722 If you are interested in how to interact with the webservices layer
723 directly, have a look at
724 <computeroutput>install/vboxapi/__init__.py</computeroutput> which
725 contains the glue layer for all target platforms (i.e. XPCOM, MSCOM
726 and web services).</para>
727
728 <para>To start the shell, perform the following commands: <screen>/opt/VirtualBox/vboxwebsrv -t 0
729 # start webservice with object autocollection disabled
730export VBOX_PROGRAM_PATH=/opt/VirtualBox
731 # your VirtualBox installation directory
732export VBOX_SDK_PATH=/home/youruser/vbox-sdk
733 # where you've extracted the SDK
734./vboxshell.py -w </screen>See <xref linkend="vboxshell" /> for more
735 details on the shell's functionality. For you, as a VirtualBox
736 application developer, the vboxshell sample could be interesting as an
737 example of how to write code targeting both local and remote cases
738 (COM/XPCOM and SOAP). The common part of the shell is the same -- the
739 only difference is how it interacts with the invocation layer. You can
740 use the <computeroutput>connect</computeroutput> shell command to
741 connect to remote VirtualBox servers; in this case you can skip
742 starting the local webserver.</para>
743 </sect2>
744
745 <sect2>
746 <title>The object-oriented web service for PHP</title>
747
748 <para>VirtualBox also comes with object-oriented web service (OOWS)
749 wrappers for PHP5. These wrappers rely on the PHP SOAP
750 Extension<footnote>
751 <para>See <ulink url="???">http://www.php.net/soap</ulink>.</para>
752 </footnote>, which can be installed by configuring PHP with
753 <computeroutput>--enable-soap</computeroutput>.</para>
754 </sect2>
755 </sect1>
756
757 <sect1 id="raw-webservice">
758 <title>Using the raw web service with any language</title>
759
760 <para>The following examples show you how to use the raw web service,
761 without the object-oriented client-side code that was described in the
762 previous chapter.</para>
763
764 <para>Generally, when reading the documentation in <xref
765 linkend="sdkref_classes" /> and <xref linkend="sdkref_enums" />, due to
766 the limitations of SOAP and WSDL lined out in <xref
767 linkend="rawws-conventions" />, please have the following notes in
768 mind:</para>
769
770 <para><orderedlist>
771 <listitem>
772 <para>Any COM method call becomes a <emphasis role="bold">plain
773 function call</emphasis> in the raw web service, with the object
774 as an additional first parameter (before the "real" parameters
775 listed in the documentation). So when the documentation says that
776 the <computeroutput>IVirtualBox</computeroutput> interface
777 supports the <computeroutput>createMachine()</computeroutput>
778 method (see <xref linkend="IVirtualBox__createMachine"
779 xreflabel="IVirtualBox::createMachine()" />), the web service
780 operation is
781 <computeroutput>IVirtualBox_createMachine(...)</computeroutput>,
782 and a managed object reference to an
783 <computeroutput>IVirtualBox</computeroutput> object must be passed
784 as the first argument.</para>
785 </listitem>
786
787 <listitem>
788 <para>For <emphasis role="bold">attributes</emphasis> in
789 interfaces, there will be at least one "get" function; there will
790 also be a "set" function, unless the attribute is "readonly". The
791 attribute name will be appended to the "get" or "set" prefix, with
792 a capitalized first letter. So, the "version" readonly attribute
793 of the <computeroutput>IVirtualBox</computeroutput> interface can
794 be retrieved by calling
795 <computeroutput>IVirtualBox_getVersion(vbox)</computeroutput>,
796 with <computeroutput>vbox</computeroutput> being the VirtualBox
797 object.</para>
798 </listitem>
799
800 <listitem>
801 <para>Whenever the API documentation says that a method (or an
802 attribute getter) returns an <emphasis
803 role="bold">object</emphasis>, it will returned a managed object
804 reference in the web service instead. As said above, managed
805 object references should be released if the web service client
806 does not log off again immediately!</para>
807 </listitem>
808 </orderedlist></para>
809
810 <para></para>
811
812 <sect2 id="webservice-java-sample">
813 <title>Raw web service example for Java with Axis</title>
814
815 <para>Axis is an older web service toolkit created by the Apache
816 foundation. If your distribution does not have it installed, you can
817 get a binary from <ulink
818 url="http://www.apache.org">http://www.apache.org</ulink>. The
819 following examples assume that you have Axis 1.4 installed.</para>
820
821 <para>The VirtualBox SDK ships with an example for Axis that, again,
822 is called <computeroutput>clienttest.java</computeroutput> and that
823 imitates a few of the commands of
824 <computeroutput>VBoxManage</computeroutput> over the wire.</para>
825
826 <para>Then perform the following steps:<orderedlist>
827 <listitem>
828 <para>Create a working directory somewhere. Under your
829 VirtualBox installation directory, find the
830 <computeroutput>sdk/webservice/samples/java/axis/</computeroutput>
831 directory and copy the file
832 <computeroutput>clienttest.java</computeroutput> to your working
833 directory.</para>
834 </listitem>
835
836 <listitem>
837 <para>Open a terminal in your working directory. Execute the
838 following command:<screen> java org.apache.axis.wsdl.WSDL2Java /path/to/vboxwebService.wsdl</screen></para>
839
840 <para>The <computeroutput>vboxwebService.wsdl</computeroutput>
841 file should be located in the
842 <computeroutput>sdk/webservice/</computeroutput>
843 directory.</para>
844
845 <para>If this fails, your Apache Axis may not be located on your
846 system classpath, and you may have to adjust the CLASSPATH
847 environment variable. Something like this:<screen>export CLASSPATH="/path-to-axis-1_4/lib/*":$CLASSPATH</screen></para>
848
849 <para>Use the directory where the Axis JAR files are located.
850 Mind the quotes so that your shell passes the "*" character to
851 the java executable without expanding. Alternatively, add a
852 corresponding <computeroutput>-classpath</computeroutput>
853 argument to the "java" call above.</para>
854
855 <para>If the command executes successfully, you should see an
856 "org" directory with subdirectories containing Java source files
857 in your working directory. These classes represent the
858 interfaces that the VirtualBox web service offers, as described
859 by the WSDL file.</para>
860
861 <para>This is the bit that makes using web services so
862 attractive to client developers: if a language's toolkit
863 understands WSDL, it can generate large amounts of support code
864 automatically. Clients can then easily use this support code and
865 can be done with just a few lines of code.</para>
866 </listitem>
867
868 <listitem>
869 <para>Next, compile the
870 <computeroutput>clienttest.java</computeroutput> source:<screen>javac clienttest.java </screen></para>
871
872 <para>This should yield a "clienttest.class" file.</para>
873 </listitem>
874
875 <listitem>
876 <para>To start the VirtualBox web service, open a second
877 terminal and change to the directory where the VirtualBox
878 executables are located. Then type:<screen>./vboxwebsrv -v</screen></para>
879
880 <para>The web service now waits for connections and will run
881 until you press Ctrl+C in this second terminal. The -v argument
882 causes it to log all connections to the terminal. (See <xref
883 linkend="runvboxwebsrv" os="" /> for details on how to run the
884 web service.)</para>
885 </listitem>
886
887 <listitem>
888 <para>Back in the original terminal where you compiled the Java
889 source, run the resulting binary, which will then connect to the
890 web service:<screen>java clienttest</screen></para>
891
892 <para>The client sample will connect to the web service (on
893 localhost, but the code could be changed to connect remotely if
894 the web service was running on a different machine) and make a
895 number of method calls. It will output the version number of
896 your VirtualBox installation and a list of all virtual machines
897 that are currently registered (with a bit of seemingly random
898 data, which will be explained later).</para>
899 </listitem>
900 </orderedlist></para>
901 </sect2>
902
903 <sect2 id="raw-webservice-perl">
904 <title>Raw web service example for Perl</title>
905
906 <para>We also ship a small sample for Perl. It uses the SOAP::Lite
907 perl module to communicate with the VirtualBox web service.</para>
908
909 <para>The
910 <computeroutput>sdk/bindings/webservice/perl/lib/</computeroutput>
911 directory contains a pre-generated Perl module that allows for
912 communicating with the web service from Perl. You can generate such a
913 module yourself using the "stubmaker" tool that comes with SOAP::Lite,
914 but since that tool is slow as well as sometimes unreliable, we are
915 shipping a working module with the SDK for your convenience.</para>
916
917 <para>Perform the following steps:<orderedlist>
918 <listitem>
919 <para>If SOAP::Lite is not yet installed on your system, you
920 will need to install the package first. On Debian-based systems,
921 the package is called
922 <computeroutput>libsoap-lite-perl</computeroutput>; on Gentoo,
923 it's <computeroutput>dev-perl/SOAP-Lite</computeroutput>.</para>
924 </listitem>
925
926 <listitem>
927 <para>Open a terminal in the
928 <computeroutput>sdk/bindings/webservice/perl/samples/</computeroutput>
929 directory.</para>
930 </listitem>
931
932 <listitem>
933 <para>To start the VirtualBox web service, open a second
934 terminal and change to the directory where the VirtualBox
935 executables are located. Then type:<screen>./vboxwebsrv -v</screen></para>
936
937 <para>The web service now waits for connections and will run
938 until you press Ctrl+C in this second terminal. The -v argument
939 causes it to log all connections to the terminal. (See <xref
940 linkend="runvboxwebsrv" os="" /> for details on how to run the
941 web service.)</para>
942 </listitem>
943
944 <listitem>
945 <para>In the first terminal with the Perl sample, run the
946 clienttest.pl script:<screen>perl -I ../lib clienttest.pl</screen></para>
947 </listitem>
948 </orderedlist></para>
949 </sect2>
950
951 <sect2>
952 <title>Programming considerations for the raw web service</title>
953
954 <para>If you use the raw web service, you need to keep a number of
955 things in mind, or you will sooner or later run into issues that are
956 not immediately obvious. By contrast, the object-oriented client-side
957 libraries described in <xref linkend="glue" /> take care of these
958 things automatically and thus greatly simplify using the web
959 service.</para>
960
961 <sect3 id="rawws-conventions">
962 <title>Fundamental conventions</title>
963
964 <para>If you are familiar with other web services, you may find the
965 VirtualBox web service to behave a bit differently to accommodate
966 for the fact that VirtualBox web service more or less maps the
967 VirtualBox Main COM API. The following main differences had to be
968 taken care of:<itemizedlist>
969 <listitem>
970 <para>Web services, as expressed by WSDL, are not
971 object-oriented. Even worse, they are normally stateless (or,
972 in web services terminology, "loosely coupled"). Web service
973 operations are entirely procedural, and one cannot normally
974 make assumptions about the state of a web service between
975 function calls.</para>
976
977 <para>In particular, this normally means that you cannot work
978 on objects in one method call that were created by another
979 call.</para>
980 </listitem>
981
982 <listitem>
983 <para>By contrast, the VirtualBox Main API, being expressed in
984 COM, is object-oriented and works entirely on objects, which
985 are grouped into public interfaces, which in turn have
986 attributes and methods associated with them.</para>
987 </listitem>
988 </itemizedlist> For the VirtualBox web service, this results in
989 three fundamental conventions:<orderedlist>
990 <listitem>
991 <para>All <emphasis role="bold">function names</emphasis> in
992 the VirtualBox web service consist of an interface name and a
993 method name, joined together by an underscore. This is because
994 there are only functions ("operations") in WSDL, but no
995 classes, interfaces, or methods.</para>
996
997 <para>In addition, all calls to the VirtualBox web service
998 (except for logon, see below) take a <emphasis
999 role="bold">managed object reference</emphasis> as the first
1000 argument, representing the object upon which the underlying
1001 method is invoked. (Managed object references are explained in
1002 detail below; see <xref
1003 linkend="managed-object-references" />.)</para>
1004
1005 <para>So, when one would normally code, in the pseudo-code of
1006 an object-oriented language, to invoke a method upon an
1007 object:<screen>IMachine machine;
1008result = machine.getName();</screen></para>
1009
1010 <para>In the VirtualBox web service, this looks something like
1011 this (again, pseudo-code):<screen>IMachineRef machine;
1012result = IMachine_getName(machine);</screen></para>
1013 </listitem>
1014
1015 <listitem>
1016 <para>To make the web service stateful, and objects persistent
1017 between method calls, the VirtualBox web service introduces a
1018 <emphasis role="bold">session manager</emphasis> (by way of
1019 the <xref linkend="IWebsessionManager"
1020 xreflabel="IWebsessionManager" /> interface), which manages
1021 object references. Any client wishing to interact with the web
1022 service must first log on to the session manager and in turn
1023 receives a managed object reference to an object that supports
1024 the <xref linkend="IVirtualBox" xreflabel="IVirtualBox" />
1025 interface (the basic interface in the Main API).</para>
1026 </listitem>
1027 </orderedlist></para>
1028
1029 <para>In other words, as opposed to other web services, <emphasis
1030 role="bold">the VirtualBox web service is both object-oriented and
1031 stateful.</emphasis></para>
1032 </sect3>
1033
1034 <sect3>
1035 <title>Example: A typical web service client session</title>
1036
1037 <para>A typical short web service session to retrieve the version
1038 number of the VirtualBox web service (to be precise, the underlying
1039 Main API version number) looks like this:<orderedlist>
1040 <listitem>
1041 <para>A client logs on to the web service by calling <xref
1042 linkend="IWebsessionManager__logon"
1043 xreflabel="IWebsessionManager::logon()" /> with a valid user
1044 name and password. See <xref linkend="websrv_authenticate" />
1045 for details about how authentication works.</para>
1046 </listitem>
1047
1048 <listitem>
1049 <para>On the server side,
1050 <computeroutput>vboxwebsrv</computeroutput> creates a session,
1051 which persists until the client calls <xref
1052 linkend="IWebsessionManager__logoff"
1053 xreflabel="IWebsessionManager::logoff()" /> or the session
1054 times out after a configurable period of inactivity (see <xref
1055 linkend="vboxwebsrv-ref" />).</para>
1056
1057 <para>For the new session, the web service creates an instance
1058 of <xref linkend="IVirtualBox" xreflabel="IVirtualBox" />.
1059 This interface is the most central one in the Main API and
1060 allows access to all other interfaces, either through
1061 attributes or method calls. For example, IVirtualBox contains
1062 a list of all virtual machines that are currently registered
1063 (as they would be listed on the left side of the VirtualBox
1064 main program).</para>
1065
1066 <para>The web service then creates a managed object reference
1067 for this instance of IVirtualBox and returns it to the calling
1068 client, which receives it as the return value of the logon
1069 call. Something like this:</para>
1070
1071 <screen>string oVirtualBox;
1072oVirtualBox = webservice.IWebsessionManager_logon("user", "pass");</screen>
1073
1074 <para>(The managed object reference "oVirtualBox" is just a
1075 string consisting of digits and dashes. However, it is a
1076 string with a meaning and will be checked by the web service.
1077 For details, see below. As hinted above, <xref
1078 linkend="IWebsessionManager__logon"
1079 xreflabel="IWebsessionManager::logon()" /> is the
1080 <emphasis>only</emphasis> operation provided by the web
1081 service which does not take a managed object reference as the
1082 first argument!)</para>
1083 </listitem>
1084
1085 <listitem>
1086 <para>The VirtualBox Main API documentation says that the
1087 <computeroutput>IVirtualBox</computeroutput> interface has a
1088 <xref linkend="IVirtualBox__version" xreflabel="version" />
1089 attribute, which is a string. For each attribute, there is a
1090 "get" and a "set" method in COM, which maps to according
1091 operations in the web service. So, to retrieve the "version"
1092 attribute of this <computeroutput>IVirtualBox</computeroutput>
1093 object, the web service client does this:<screen>string version;
1094version = webservice.IVirtualBox_getVersion(oVirtualBox);
1095
1096print version;</screen></para>
1097
1098 <para>And it will print
1099 "$VBOX_VERSION_MAJOR.$VBOX_VERSION_MINOR.$VBOX_VERSION_BUILD".</para>
1100 </listitem>
1101
1102 <listitem>
1103 <para>The web service client calls <xref
1104 linkend="IWebsessionManager__logoff"
1105 xreflabel="IWebsessionManager::logoff()" /> with the
1106 VirtualBox managed object reference. This will clean up all
1107 allocated resources.</para>
1108 </listitem>
1109 </orderedlist></para>
1110 </sect3>
1111
1112 <sect3 id="managed-object-references">
1113 <title>Managed object references</title>
1114
1115 <para>To a web service client, a managed object reference looks like
1116 a string: two 64-bit hex numbers separated by a dash. This string,
1117 however, represents a COM object that "lives" in the web service
1118 process. The two 64-bit numbers encoded in the managed object
1119 reference represent a session ID (which is the same for all objects
1120 in the same web service session, i.e. for all objects after one
1121 logon) and a unique object ID within that session.</para>
1122
1123 <para>Managed object references are created in two
1124 situations:<orderedlist>
1125 <listitem>
1126 <para>When a client logs on, by calling <xref
1127 linkend="IWebsessionManager__logon"
1128 xreflabel="IWebsessionManager::logon()" />.</para>
1129
1130 <para>Upon logon, the websession manager creates one instance
1131 of <xref linkend="IVirtualBox" xreflabel="IVirtualBox" /> and
1132 another object of <xref linkend="ISession"
1133 xreflabel="ISession" /> representing the web service session.
1134 This can be retrieved using <xref
1135 linkend="IWebsessionManager__getSessionObject"
1136 xreflabel="IWebsessionManager::getSessionObject()" />.</para>
1137
1138 <para>(Technically, there is always only one <xref
1139 linkend="IVirtualBox" xreflabel="IVirtualBox" /> object, which
1140 is shared between all sessions and clients, as it is a COM
1141 singleton. However, each session receives its own managed
1142 object reference to it. The <xref linkend="ISession"
1143 xreflabel="ISession" /> object, however, is created and
1144 destroyed for each session.)</para>
1145 </listitem>
1146
1147 <listitem>
1148 <para>Whenever a web service clients invokes an operation
1149 whose COM implementation creates COM objects.</para>
1150
1151 <para>For example, <xref linkend="IVirtualBox__createMachine"
1152 xreflabel="IVirtualBox::createMachine()" /> creates a new
1153 instance of <xref linkend="IMachine" xreflabel="IMachine" />;
1154 the COM object returned by the COM method call is then wrapped
1155 into a managed object reference by the web server, and this
1156 reference is returned to the web service client.</para>
1157 </listitem>
1158 </orderedlist></para>
1159
1160 <para>Internally, in the web service process, each managed object
1161 reference is simply a small data structure, containing a COM pointer
1162 to the "real" COM object, the web session ID and the object ID. This
1163 structure is allocated on creation and stored efficiently in hashes,
1164 so that the web service can look up the COM object quickly whenever
1165 a web service client wishes to make a method call. The random
1166 session ID also ensures that one web service client cannot intercept
1167 the objects of another.</para>
1168
1169 <para>Managed object references are not destroyed automatically and
1170 must be released by explicitly calling <xref
1171 linkend="IManagedObjectRef__release"
1172 xreflabel="IManagedObjectRef::release()" />. This is important, as
1173 otherwise hundreds or thousands of managed object references (and
1174 corresponding COM objects, which can consume much more memory!) can
1175 pile up in the web service process and eventually cause it to deny
1176 service.</para>
1177
1178 <para>To reiterate: The underlying COM object, which the reference
1179 points to, is only freed if the managed object reference is
1180 released. It is therefore vital that web service clients properly
1181 clean up after the managed object references that are returned to
1182 them.</para>
1183
1184 <para>When a web service client calls <xref
1185 linkend="IWebsessionManager__logoff"
1186 xreflabel="IWebsessionManager::logoff()" />, all managed object
1187 references created during the session are automatically freed. For
1188 short-lived sessions that do not create a lot of objects, logging
1189 off may therefore be sufficient, although it is certainly not "best
1190 practice".</para>
1191 </sect3>
1192
1193 <sect3>
1194 <title>Some more detail about web service operation</title>
1195
1196 <sect4 id="soap">
1197 <title>SOAP messages</title>
1198
1199 <para>Whenever a client makes a call to a web service, this
1200 involves a complicated procedure internally. These calls are
1201 remote procedure calls. Each such procedure call typically
1202 consists of two "message" being passed, where each message is a
1203 plain-text HTTP request with a standard HTTP header and a special
1204 XML document following. This XML document encodes the name of the
1205 procedure to call and the argument names and values passed to
1206 it.</para>
1207
1208 <para>To give you an idea of what such a message looks like,
1209 assuming that a web service provides a procedure called
1210 "SayHello", which takes a string "name" as an argument and returns
1211 "Hello" with a space and that name appended, the request message
1212 could look like this:</para>
1213
1214 <para><screen>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
1215&lt;SOAP-ENV:Envelope
1216 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
1217 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
1218 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1219 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
1220 xmlns:test="http://test/"&gt;
1221&lt;SOAP-ENV:Body&gt;
1222 &lt;test:SayHello&gt;
1223 &lt;name&gt;Peter&lt;/name&gt;
1224 &lt;/test:SayHello&gt;
1225 &lt;/SOAP-ENV:Body&gt;
1226&lt;/SOAP-ENV:Envelope&gt;</screen>A similar message -- the "response" message
1227 -- would be sent back from the web service to the client,
1228 containing the return value "Hello Peter".</para>
1229
1230 <para>Most programming languages provide automatic support to
1231 generate such messages whenever code in that programming language
1232 makes such a request. In other words, these programming languages
1233 allow for writing something like this (in pseudo-C++ code):</para>
1234
1235 <para><screen>webServiceClass service("localhost", 18083); // server and port
1236string result = service.SayHello("Peter"); // invoke remote procedure</screen>and
1237 would, for these two pseudo-lines, automatically perform these
1238 steps:</para>
1239
1240 <para><orderedlist>
1241 <listitem>
1242 <para>prepare a connection to a web service running on port
1243 18083 of "localhost";</para>
1244 </listitem>
1245
1246 <listitem>
1247 <para>for the <computeroutput>SayHello()</computeroutput>
1248 function of the web service, generate a SOAP message like in
1249 the above example by encoding all arguments of the remote
1250 procedure call (which could involve all kinds of type
1251 conversions and complex marshalling for arrays and
1252 structures);</para>
1253 </listitem>
1254
1255 <listitem>
1256 <para>connect to the web service via HTTP and send that
1257 message;</para>
1258 </listitem>
1259
1260 <listitem>
1261 <para>wait for the web service to send a response
1262 message;</para>
1263 </listitem>
1264
1265 <listitem>
1266 <para>decode that response message and put the return value
1267 of the remote procedure into the "result" variable.</para>
1268 </listitem>
1269 </orderedlist></para>
1270 </sect4>
1271
1272 <sect4 id="wsdl">
1273 <title>Service descriptions in WSDL</title>
1274
1275 <para>In the above explanations about SOAP, it was left open how
1276 the programming language learns about how to translate function
1277 calls in its own syntax into proper SOAP messages. In other words,
1278 the programming language needs to know what operations the web
1279 service supports and what types of arguments are required for the
1280 operation's data in order to be able to properly serialize and
1281 deserialize the data to and from the web service. For example, if
1282 a web service operation expects a number in "double" floating
1283 point format for a particular parameter, the programming language
1284 cannot send to it a string instead.</para>
1285
1286 <para>For this, the Web Service Definition Language (WSDL) was
1287 invented, another XML substandard that describes exactly what
1288 operations the web service supports and, for each operation, which
1289 parameters and types are needed with each request and response
1290 message. WSDL descriptions can be incredibly verbose, and one of
1291 the few good things that can be said about this standard is that
1292 it is indeed supported by most programming languages.</para>
1293
1294 <para>So, if it is said that a programming language "supports" web
1295 services, this typically means that a programming language has
1296 support for parsing WSDL files and somehow integrating the remote
1297 procedure calls into the native language syntax -- for example,
1298 like in the Java sample shown in <xref
1299 linkend="webservice-java-sample" />.</para>
1300
1301 <para>For details about how programming languages support web
1302 services, please refer to the documentation that comes with the
1303 individual languages. Here are a few pointers:</para>
1304
1305 <orderedlist>
1306 <listitem>
1307 <para>For <emphasis role="bold">C++,</emphasis> among many
1308 others, the gSOAP toolkit is a good option. Parts of gSOAP are
1309 also used in VirtualBox to implement the VirtualBox web
1310 service.</para>
1311 </listitem>
1312
1313 <listitem>
1314 <para>For <emphasis role="bold">Java,</emphasis> there are
1315 several implementations already described in this document
1316 (see <xref linkend="glue-jax-ws" /> and <xref
1317 linkend="webservice-java-sample" />).</para>
1318 </listitem>
1319
1320 <listitem>
1321 <para><emphasis role="bold">Perl</emphasis> supports WSDL via
1322 the SOAP::Lite package. This in turn comes with a tool called
1323 <computeroutput>stubmaker.pl</computeroutput> that allows you
1324 to turn any WSDL file into a Perl package that you can import.
1325 (You can also import any WSDL file "live" by having it parsed
1326 every time the script runs, but that can take a while.) You
1327 can then code (again, assuming the above example):<screen>my $result = servicename-&gt;sayHello("Peter");</screen></para>
1328
1329 <para>A sample that uses SOAP::Lite was described in <xref
1330 linkend="raw-webservice-perl" />.</para>
1331 </listitem>
1332 </orderedlist>
1333 </sect4>
1334 </sect3>
1335 </sect2>
1336 </sect1>
1337
1338 <sect1 id="api_com">
1339 <title>Using COM/XPCOM directly</title>
1340
1341 <para>If you do not require <emphasis>remote</emphasis> procedure calls
1342 such as those offered by the VirtualBox web service, and if you know
1343 Python or C++ as well as COM, you might find it preferable to program
1344 VirtualBox's Main API directly via COM.</para>
1345
1346 <para>COM stands for "Component Object Model" and is a standard
1347 originally introduced by Microsoft in the 1990s for Microsoft Windows.
1348 It allows for organizing software in an object-oriented way and across
1349 processes; code in one process may access objects that live in another
1350 process.</para>
1351
1352 <para>COM has several advantages: it is language-neutral, meaning that
1353 even though all of VirtualBox is internally written in C++, programs
1354 written in other languages could communicate with it. COM also cleanly
1355 separates interface from implementation, so that external programs need
1356 not know anything about the messy and complicated details of VirtualBox
1357 internals.</para>
1358
1359 <para>On a Windows host, all parts of VirtualBox will use the COM
1360 functionality that is native to Windows. On other hosts (including
1361 Linux), VirtualBox comes with a built-in implementation of XPCOM, as
1362 originally created by the Mozilla project, which we have enhanced to
1363 support interprocess communication on a level comparable to Microsoft
1364 COM. Internally, VirtualBox has an abstraction layer that allows the
1365 same VirtualBox code to work both with native COM as well as our XPCOM
1366 implementation.</para>
1367
1368 <sect2 id="pycom">
1369 <title>Python COM API</title>
1370
1371 <para>On Windows, Python scripts can use COM and VirtualBox interfaces
1372 to control almost all aspects of virtual machine execution. As an
1373 example, use the following commands to instantiate the VirtualBox
1374 object and start a VM: <screen>
1375 vbox = win32com.client.Dispatch("VirtualBox.VirtualBox")
1376 session = win32com.client.Dispatch("VirtualBox.Session")
1377 mach = vbox.findMachine("uuid or name of machine to start")
1378 progress = mach.launchVMProcess(session, "gui", "")
1379 progress.waitForCompletion(-1)
1380 </screen> Also, see
1381 <computeroutput>/bindings/glue/python/samples/vboxshell.py</computeroutput>
1382 for more advanced usage scenarious. However, unless you have specific
1383 requirements, we strongly recommend to use the generic glue layer
1384 described in the next section to access MS COM objects.</para>
1385 </sect2>
1386
1387 <sect2 id="glue-python">
1388 <title>Common Python bindings layer</title>
1389
1390 <para>As different wrappers ultimately provide access to the same
1391 underlying API, and to simplify porting and development of Python
1392 application using the VirtualBox Main API, we developed a common glue
1393 layer that abstracts out most platform-specific details from the
1394 application and allows the developer to focus on application logic.
1395 The VirtualBox installer automatically sets up this glue layer for the
1396 system default Python install. See below for details on how to set up
1397 the glue layer if you want to use a different Python
1398 installation.</para>
1399
1400 <para>In this layer, the class
1401 <computeroutput>VirtualBoxManager</computeroutput> hides most
1402 platform-specific details. It can be used to access both the local
1403 (COM) and the webservice-based API. The following code can be used by
1404 an application to use the glue layer.</para>
1405
1406 <screen># This code assumes vboxapi.py from VirtualBox distribution
1407# being in PYTHONPATH, or installed system-wide
1408from vboxapi import VirtualBoxManager
1409
1410# This code initializes VirtualBox manager with default style
1411# and parameters
1412virtualBoxManager = VirtualBoxManager(None, None)
1413
1414# Alternatively, one can be more verbose, and initialize
1415# glue with webservice backend, and provide authentication
1416# information
1417virtualBoxManager = VirtualBoxManager("WEBSERVICE",
1418 {'url':'http://myhost.com::18083/',
1419 'user':'me',
1420 'password':'secret'}) </screen>
1421
1422 <para>We supply the <computeroutput>VirtualBoxManager</computeroutput>
1423 constructor with 2 arguments: style and parameters. Style defines
1424 which bindings style to use (could be "MSCOM", "XPCOM" or
1425 "WEBSERVICE"), and if set to <computeroutput>None</computeroutput>
1426 defaults to usable platform bindings (MS COM on Windows, XPCOM on
1427 other platforms). The second argument defines parameters, passed to
1428 the platform-specific module, as we do in the second example, where we
1429 pass username and password to be used to authenticate against the web
1430 service.</para>
1431
1432 <para>After obtaining the
1433 <computeroutput>VirtualBoxManager</computeroutput> instance, one can
1434 perform operations on the IVirtualBox class. For example, the
1435 following code will a start virtual machine by name or ID:</para>
1436
1437 <screen>vbox = virtualBoxManager.vbox
1438mgr = virtualBoxManager.mgr
1439print "Version is",vbox.version
1440
1441def machById(id):
1442 mach = None
1443 for m in virtualBoxManager.getArray(vbox, 'machines'):
1444 if m.name == id or mach.id == id:
1445 mach = m
1446 break
1447 return mach
1448
1449name = "Linux"
1450mach = machById(name)
1451if mach is None:
1452 print "cannot find machine",name
1453else:
1454 session = mgr.getSessionObject(vbox)
1455 # one can also start headless session with "vrdp" instead of "gui"
1456 progress = vb.openRemoteSession(session, mach.id, "gui", "")
1457 progress.waitForCompletion(-1)
1458 session.close()
1459 </screen>
1460
1461 <para>This code also shows cross-platform access to array properties
1462 (certain limitations prevent one from using
1463 <computeroutput>vbox.machines</computeroutput> to access a list of
1464 available virtual machines in case of XPCOM), and a mechanism of
1465 uniform session creation
1466 (<computeroutput>virtualBoxManager.mgr.getSessionObject()</computeroutput>).</para>
1467
1468 <para>In case you want to use the glue layer with a different Python
1469 installation, use these steps in a shell to add the necessary
1470 files:</para>
1471
1472 <screen> # cd VBOX_INSTALL_PATH/sdk/installer
1473 # PYTHON vboxapisetup.py install</screen>
1474 </sect2>
1475
1476 <sect2 id="cppcom">
1477 <title>C++ COM API</title>
1478
1479 <para>C++ is the language that VirtualBox itself is written in, so C++
1480 is the most direct way to use the Main API -- but it is not
1481 necessarily the easiest, as using COM and XPCOM has its own set of
1482 complications.</para>
1483
1484 <para>VirtualBox ships with sample programs that demonstrate how to
1485 use the Main API to implement a number of tasks on your host platform.
1486 These samples can be found in the
1487 <computeroutput>/bindings/xpcom/samples</computeroutput> directory for
1488 Linux, Mac OS X and Solaris and
1489 <computeroutput>/bindings/mscom/samples</computeroutput> for Windows.
1490 The two samples are actually different, because the one for Windows
1491 uses native COM, whereas the other uses our XPCOM implementation, as
1492 described above.</para>
1493
1494 <para>Since COM and XPCOM are conceptually very similar but vary in
1495 the implementation details, we have created a "glue" layer that
1496 shields COM client code from these differences. All VirtualBox uses is
1497 this glue layer, so the same code written once works on both Windows
1498 hosts (with native COM) as well as on other hosts (with our XPCOM
1499 implementation). It is recommended to always use this glue code
1500 instead of using the COM and XPCOM APIs directly, as it is very easy
1501 to make your code completely independent from the platform it is
1502 running on.<!-- A third sample,
1503 <computeroutput>tstVBoxAPIGlue.cpp</computeroutput>, illustrates how to
1504 use the glue layer.
1505--></para>
1506
1507 <para>In order to encapsulate platform differences between Microsoft
1508 COM and XPCOM, the following items should be kept in mind when using
1509 the glue layer:</para>
1510
1511 <para><orderedlist>
1512 <listitem>
1513 <para><emphasis role="bold">Attribute getters and
1514 setters.</emphasis> COM has the notion of "attributes" in
1515 interfaces, which roughly compare to C++ member variables in
1516 classes. The difference is that for each attribute declared in
1517 an interface, COM automatically provides a "get" method to
1518 return the attribute's value. Unless the attribute has been
1519 marked as "readonly", a "set" attribute is also provided.</para>
1520
1521 <para>To illustrate, the IVirtualBox interface has a "version"
1522 attribute, which is read-only and of the "wstring" type (the
1523 standard string type in COM). As a result, you can call the
1524 "get" method for this attribute to retrieve the version number
1525 of VirtualBox.</para>
1526
1527 <para>Unfortunately, the implementation differs between COM and
1528 XPCOM. Microsoft COM names the "get" method like this:
1529 <computeroutput>get_Attribute()</computeroutput>, whereas XPCOM
1530 uses this syntax:
1531 <computeroutput>GetAttribute()</computeroutput> (and accordingly
1532 for "set" methods). To hide these differences, the VirtualBox
1533 glue code provides the
1534 <computeroutput>COMGETTER(attrib)</computeroutput> and
1535 <computeroutput>COMSETTER(attrib)</computeroutput> macros. So,
1536 <computeroutput>COMGETTER(version)()</computeroutput> (note, two
1537 pairs of brackets) expands to
1538 <computeroutput>get_Version()</computeroutput> on Windows and
1539 <computeroutput>GetVersion()</computeroutput> on other
1540 platforms.</para>
1541 </listitem>
1542
1543 <listitem>
1544 <para><emphasis role="bold">Unicode conversions.</emphasis>
1545 While the rest of the modern world has pretty much settled on
1546 encoding strings in UTF-8, COM, unfortunately, uses UCS-16
1547 encoding. This requires a lot of conversions, in particular
1548 between the VirtualBox Main API and the Qt GUI, which, like the
1549 rest of Qt, likes to use UTF-8.</para>
1550
1551 <para>To facilitate these conversions, VirtualBox provides the
1552 <computeroutput>com::Bstr</computeroutput> and
1553 <computeroutput>com::Utf8Str</computeroutput> classes, which
1554 support all kinds of conversions back and forth.</para>
1555 </listitem>
1556
1557 <listitem>
1558 <para><emphasis role="bold">COM autopointers.</emphasis>
1559 Possibly the greatest pain of using COM -- reference counting --
1560 is alleviated by the
1561 <computeroutput>ComPtr&lt;&gt;</computeroutput> template
1562 provided by the <computeroutput>ptr.h</computeroutput> file in
1563 the glue layer.</para>
1564 </listitem>
1565 </orderedlist></para>
1566 </sect2>
1567
1568 <sect2 id="event-queue">
1569 <title>Event queue processing</title>
1570
1571 <para>Both VirtualBox client programs and frontends should
1572 periodically perform processing of the main event queue, and do that
1573 on the application's main thread. In case of a typical GUI Windows/Mac
1574 OS application this happens automatically in the GUI's dispatch loop.
1575 However, for CLI only application, the appropriate actions have to be
1576 taken. For C++ applications, the VirtualBox SDK provided glue method
1577 <screen>
1578 int EventQueue::processEventQueue(uint32_t cMsTimeout)
1579 </screen> can be used for both blocking and non-blocking operations.
1580 For the Python bindings, a common layer provides the method <screen>
1581 VirtualBoxManager.waitForEvents(ms)
1582 </screen> with similar semantics.</para>
1583
1584 <para>Things get somewhat more complicated for situations where an
1585 application using VirtualBox cannot directly control the main event
1586 loop and the main event queue is separated from the event queue of the
1587 programming librarly (for example in case of Qt on Unix platforms). In
1588 such a case, the application developer is advised to use a
1589 platform/toolkit specific event injection mechanism to force event
1590 queue checks either based on periodical timer events delivered to the
1591 main thread, or by using custom platform messages to notify the main
1592 thread when events are available. See the VBoxSDL and Qt (VirtualBox)
1593 frontends as examples.</para>
1594 </sect2>
1595
1596 <sect2 id="vbcom">
1597 <title>Visual Basic and Visual Basic Script (VBS) on Windows
1598 hosts</title>
1599
1600 <para>On Windows hosts, one can control some of the VirtualBox Main
1601 API functionality from VBS scripts, and pretty much everything from
1602 Visual Basic programs.<footnote>
1603 <para>The difference results from the way VBS treats COM
1604 safearrays, which are used to keep lists in the Main API. VBS
1605 expects every array element to be a
1606 <computeroutput>VARIANT</computeroutput>, which is too strict a
1607 limitation for any high performance API. We may lift this
1608 restriction for interface APIs in a future version, or
1609 alternatively provide conversion APIs.</para>
1610 </footnote></para>
1611
1612 <para>VBS is scripting language available in any recent Windows
1613 environment. As an example, the following VBS code will print
1614 VirtualBox version: <screen>
1615 set vb = CreateObject("VirtualBox.VirtualBox")
1616 Wscript.Echo "VirtualBox version " &amp; vb.version
1617 </screen> See
1618 <computeroutput>bindings/mscom/vbs/sample/vboxinfo.vbs</computeroutput>
1619 for the complete sample.</para>
1620
1621 <para>Visual Basic is a popular high level language capable of
1622 accessing COM objects. The following VB code will iterate over all
1623 available virtual machines:<screen>
1624 Dim vb As VirtualBox.IVirtualBox
1625
1626 vb = CreateObject("VirtualBox.VirtualBox")
1627 machines = ""
1628 For Each m In vb.Machines
1629 m = m &amp; " " &amp; m.Name
1630 Next
1631 </screen> See
1632 <computeroutput>bindings/mscom/vb/sample/vboxinfo.vb</computeroutput>
1633 for the complete sample.</para>
1634 </sect2>
1635
1636 <sect2 id="cbinding">
1637 <title>C binding to XPCOM API</title>
1638
1639 <note>
1640 <para>This section currently applies to Linux hosts only.</para>
1641 </note>
1642
1643 <para>Starting with version 2.2, VirtualBox offers a C binding for the
1644 XPCOM API.</para>
1645
1646 <para>The C binding provides a layer enabling object creation, method
1647 invocation and attribute access from C.</para>
1648
1649 <sect3 id="c-gettingstarted">
1650 <title>Getting started</title>
1651
1652 <para>The following sections describe how to use the C binding in a
1653 C program.</para>
1654
1655 <para>For Linux, a sample program is provided which demonstrates use
1656 of the C binding to initialize XPCOM, get handles for VirtualBox and
1657 Session objects, make calls to list and start virtual machines, and
1658 uninitialize resources when done. The program uses the VBoxGlue
1659 library to open the C binding layer during runtime.</para>
1660
1661 <para>The sample program
1662 <computeroutput>tstXPCOMCGlue</computeroutput> is located in the bin
1663 directory and can be run without arguments. It lists registered
1664 machines on the host along with some additional information and ask
1665 for a machine to start. The source for this program is available in
1666 <computeroutput>sdk/bindings/xpcom/cbinding/samples/</computeroutput>
1667 directory. The source for the VBoxGlue library is available in the
1668 <computeroutput>sdk/bindings/xpcom/cbinding/</computeroutput>
1669 directory.</para>
1670 </sect3>
1671
1672 <sect3 id="c-initialization">
1673 <title>XPCOM initialization</title>
1674
1675 <para>Just like in C++, XPCOM needs to be initialized before it can
1676 be used. The <computeroutput>VBoxCAPI_v2_5.h</computeroutput> header
1677 provides the interface to the C binding. Here's how to initialize
1678 XPCOM:</para>
1679
1680 <screen>#include "VBoxCAPI_v2_5.h"
1681...
1682PCVBOXXPCOM g_pVBoxFuncs = NULL;
1683IVirtualBox *vbox = NULL;
1684ISession *session = NULL;
1685
1686/*
1687 * VBoxGetXPCOMCFunctions() is the only function exported by
1688 * VBoxXPCOMC.so and the only one needed to make virtualbox
1689 * work with C. This functions gives you the pointer to the
1690 * function table (g_pVBoxFuncs).
1691 *
1692 * Once you get the function table, then how and which functions
1693 * to use is explained below.
1694 *
1695 * g_pVBoxFuncs-&gt;pfnComInitialize does all the necessary startup
1696 * action and provides us with pointers to vbox and session handles.
1697 * It should be matched by a call to g_pVBoxFuncs-&gt;pfnComUninitialize()
1698 * when done.
1699 */
1700
1701g_pVBoxFuncs = VBoxGetXPCOMCFunctions(VBOX_XPCOMC_VERSION);
1702g_pVBoxFuncs-&gt;pfnComInitialize(&amp;vbox, &amp;session);</screen>
1703
1704 <para>If either <computeroutput>vbox</computeroutput> or
1705 <computeroutput>session</computeroutput> is still
1706 <computeroutput>NULL</computeroutput>, initialization failed and the
1707 XPCOM API cannot be used.</para>
1708 </sect3>
1709
1710 <sect3 id="c-invocation">
1711 <title>XPCOM method invocation</title>
1712
1713 <para>Method invocation is straightforward. It looks pretty much
1714 like the C++ way, augmented with an extra indirection due to
1715 accessing the vtable and passing a pointer to the object as the
1716 first argument to serve as the <computeroutput>this</computeroutput>
1717 pointer.</para>
1718
1719 <para>Using the C binding, all method invocations return a numeric
1720 result code.</para>
1721
1722 <para>If an interface is specified as returning an object, a pointer
1723 to a pointer to the appropriate object must be passed as the last
1724 argument. The method will then store an object pointer in that
1725 location.</para>
1726
1727 <para>In other words, to call an object's method what you need
1728 is</para>
1729
1730 <screen>IObject *object;
1731nsresult rc;
1732...
1733/*
1734 * Calling void IObject::method(arg, ...)
1735 */
1736rc = object-&gt;vtbl-&gt;Method(object, arg, ...);
1737
1738...
1739IFoo *foo;
1740/*
1741 * Calling IFoo IObject::method(arg, ...)
1742 */
1743rc = object-&gt;vtbl-&gt;Method(object, args, ..., &amp;foo);</screen>
1744
1745 <para>As a real-world example of a method invocation, let's call
1746 <xref linkend="IVirtualBox__openRemoteSession"
1747 xreflabel="IVirtualBox::openRemoteSession" /> which returns an
1748 IProgress object. Note again that the method name is
1749 capitalized.</para>
1750
1751 <screen>IProgress *progress;
1752...
1753rc = vbox-&gt;vtbl-&gt;OpenRemoteSession(
1754 vbox, /* this */
1755 session, /* arg 1 */
1756 id, /* arg 2 */
1757 sessionType, /* arg 3 */
1758 env, /* arg 4 */
1759 &amp;progress /* Out */
1760);
1761</screen>
1762 </sect3>
1763
1764 <sect3 id="c-attributes">
1765 <title>XPCOM attribute access</title>
1766
1767 <para>A construct similar to calling non-void methods is used to
1768 access object attributes. For each attribute there exists a getter
1769 method, the name of which is composed of
1770 <computeroutput>Get</computeroutput> followed by the capitalized
1771 attribute name. Unless the attribute is read-only, an analogous
1772 <computeroutput>Set</computeroutput> method exists. Let's apply
1773 these rules to read the <xref linkend="IVirtualBox__revision"
1774 xreflabel="IVirtualBox::revision" /> attribute.</para>
1775
1776 <para>Using the <computeroutput>IVirtualBox</computeroutput> handle
1777 <computeroutput>vbox</computeroutput> obtained above, calling its
1778 <computeroutput>GetRevision</computeroutput> method looks like
1779 this:</para>
1780
1781 <screen>PRUint32 rev;
1782
1783rc = vbox-&gt;vtbl-&gt;GetRevision(vbox, &amp;rev);
1784if (NS_SUCCEEDED(rc))
1785{
1786 printf("Revision: %u\n", (unsigned)rev);
1787}
1788</screen>
1789
1790 <para>All objects with their methods and attributes are documented
1791 in <xref linkend="sdkref_classes" />.</para>
1792 </sect3>
1793
1794 <sect3 id="c-string-handling">
1795 <title>String handling</title>
1796
1797 <para>When dealing with strings you have to be aware of a string's
1798 encoding and ownership.</para>
1799
1800 <para>Internally, XPCOM uses UTF-16 encoded strings. A set of
1801 conversion functions is provided to convert other encodings to and
1802 from UTF-16. The type of a UTF-16 character is
1803 <computeroutput>PRUnichar</computeroutput>. Strings of UTF-16
1804 characters are arrays of that type. Most string handling functions
1805 take pointers to that type. Prototypes for the following conversion
1806 functions are declared in
1807 <computeroutput>VBoxCAPI_v2_5.h</computeroutput>.</para>
1808
1809 <sect4>
1810 <title>Conversion of UTF-16 to and from UTF-8</title>
1811
1812 <screen>int (*pfnUtf16ToUtf8)(const PRUnichar *pwszString, char **ppszString);
1813int (*pfnUtf8ToUtf16)(const char *pszString, PRUnichar **ppwszString);
1814</screen>
1815 </sect4>
1816
1817 <sect4>
1818 <title>Ownership</title>
1819
1820 <para>The ownership of a string determines who is responsible for
1821 releasing resources associated with the string. Whenever XPCOM
1822 creates a string, ownership is transferred to the caller. To avoid
1823 resource leaks, the caller should release resources once the
1824 string is no longer needed.</para>
1825 </sect4>
1826 </sect3>
1827
1828 <sect3 id="c-uninitialization">
1829 <title>XPCOM uninitialization</title>
1830
1831 <para>Uninitialization is performed by
1832 <computeroutput>g_pVBoxFuncs-&gt;pfnComUninitialize().</computeroutput>
1833 If your program can exit from more than one place, it is a good idea
1834 to install this function as an exit handler with Standard C's
1835 <computeroutput>atexit()</computeroutput> just after calling
1836 <computeroutput>g_pVBoxFuncs-&gt;pfnComInitialize()</computeroutput>
1837 , e.g. <screen>#include &lt;stdlib.h&gt;
1838#include &lt;stdio.h&gt;
1839
1840...
1841
1842/*
1843 * Make sure g_pVBoxFuncs-&gt;pfnComUninitialize() is called at exit, no
1844 * matter if we return from the initial call to main or call exit()
1845 * somewhere else. Note that atexit registered functions are not
1846 * called upon abnormal termination, i.e. when calling abort() or
1847 * signal(). Separate provisions must be taken for these cases.
1848 */
1849
1850if (atexit(g_pVBoxFuncs-&gt;pfnComUninitialize()) != 0) {
1851 fprintf(stderr, "failed to register g_pVBoxFuncs-&gt;pfnComUninitialize()\n");
1852 exit(EXIT_FAILURE);
1853}
1854</screen></para>
1855
1856 <para>Another idea would be to write your own <computeroutput>void
1857 myexit(int status)</computeroutput> function, calling
1858 <computeroutput>g_pVBoxFuncs-&gt;pfnComUninitialize()</computeroutput>
1859 followed by the real <computeroutput>exit()</computeroutput>, and
1860 use it instead of <computeroutput>exit()</computeroutput> throughout
1861 your program and at the end of
1862 <computeroutput>main.</computeroutput></para>
1863
1864 <para>If you expect the program to be terminated by a signal (e.g.
1865 user types CTRL-C sending SIGINT) you might want to install a signal
1866 handler setting a flag noting that a signal was sent and then
1867 calling
1868 <computeroutput>g_pVBoxFuncs-&gt;pfnComUninitialize()</computeroutput>
1869 later on (usually <emphasis>not</emphasis> from the handler itself
1870 .)</para>
1871
1872 <para>That said, if a client program forgets to call
1873 <computeroutput>g_pVBoxFuncs-&gt;pfnComUninitialize()</computeroutput>
1874 before it terminates, there is a mechanism in place which will
1875 eventually release references held by the client. You should not
1876 rely on this, however.</para>
1877 </sect3>
1878
1879 <sect3 id="c-linking">
1880 <title>Compiling and linking</title>
1881
1882 <para>A program using the C binding has to open the library during
1883 runtime using the help of glue code provided and as shown in the
1884 example <computeroutput>tstXPCOMCGlue.c</computeroutput>.
1885 Compilation and linking can be achieved, e.g., with a makefile
1886 fragment similar to</para>
1887
1888 <screen># Where is the XPCOM include directory?
1889INCS_XPCOM = -I../../include
1890# Where is the glue code directory?
1891GLUE_DIR = ..
1892GLUE_INC = -I..
1893
1894#Compile Glue Library
1895VBoxXPCOMCGlue.o: $(GLUE_DIR)/VBoxXPCOMCGlue.c
1896 $(CC) $(CFLAGS) $(INCS_XPCOM) $(GLUE_INC) -o $@ -c $&lt;
1897
1898# Compile.
1899program.o: program.c VBoxCAPI_v2_5.h
1900 $(CC) $(CFLAGS) $(INCS_XPCOM) $(GLUE_INC) -o $@ -c $&lt;
1901
1902# Link.
1903program: program.o VBoxXPCOMCGlue.o
1904 $(CC) -o $@ $^ -ldl</screen>
1905 </sect3>
1906 </sect2>
1907 </sect1>
1908 </chapter>
1909
1910 <chapter id="concepts">
1911 <title>Basic VirtualBox concepts; some examples</title>
1912
1913 <para>The following explains some basic VirtualBox concepts such as the
1914 VirtualBox object, sessions and how virtual machines are manipulated and
1915 launched using the Main API. The coding examples use a pseudo-code style
1916 closely related to the object-oriented web service (OOWS) for JAX-WS.
1917 Depending on which environment you are using, you will need to adjust the
1918 examples.</para>
1919
1920 <sect1>
1921 <title>Obtaining basic machine information. Reading attributes</title>
1922
1923 <para>Any program using the Main API will first need access to the
1924 global VirtualBox object (see <xref linkend="IVirtualBox"
1925 xreflabel="IVirtualBox" />), from which all other functionality of the
1926 API is derived. With the OOWS for JAX-WS, this is returned from the
1927 <xref linkend="IWebsessionManager__logon"
1928 xreflabel="IWebsessionManager::logon()" /> call.</para>
1929
1930 <para>To enumerate virtual machines, one would look at the "machines"
1931 array attribute in the VirtualBox object (see <xref
1932 linkend="IVirtualBox__machines" xreflabel="IVirtualBox::machines" />).
1933 This array contains all virtual machines currently registered with the
1934 host, each of them being an instance of <xref linkend="IMachine"
1935 xreflabel="IMachine" />. From each such instance, one can query
1936 additional information, such as the UUID, the name, memory, operating
1937 system and more by looking at the attributes; see the attributes list in
1938 <xref linkend="IMachine" xreflabel="IMachine documentation" />.</para>
1939
1940 <para>As mentioned in the preceding chapters, depending on your
1941 programming environment, attributes are mapped to corresponding "get"
1942 and (if the attribute is not read-only) "set" methods. So when the
1943 documentation says that IMachine has a "<xref linkend="IMachine__name"
1944 xreflabel="name" />" attribute, this means you need to code something
1945 like the following to get the machine's name:<screen>IMachine machine = ...;
1946String name = machine.getName();</screen>Boolean attribute getters can
1947 sometimes be called <computeroutput>isAttribute()</computeroutput> due
1948 to JAX-WS naming conventions.</para>
1949 </sect1>
1950
1951 <sect1>
1952 <title>Changing machine settings. Sessions</title>
1953
1954 <para>As said in the previous section, to read a machine's attribute,
1955 one invokes the corresponding "get" method. One would think that to
1956 change settings of a machine, it would suffice to call the corresponding
1957 "set" method -- for example, to set a VM's memory to 1024 MB, one would
1958 call <computeroutput>setMemorySize(1024)</computeroutput>. Try that, and
1959 you will get an error: "The machine is not mutable."</para>
1960
1961 <para>So unfortunately, things are not that easy. VirtualBox is a
1962 complicated environment in which multiple processes compete for possibly
1963 the same resources, especially machine settings. As a result, machines
1964 must be "locked" before they can either be modified or started. This is
1965 to prevent multiple processes from making conflicting changes to a
1966 machine: it should, for example, not be allowed to change the memory
1967 size of a virtual machine while it is running. (You can't add more
1968 memory to a real computer while it is running either, at least not to an
1969 ordinary PC.) Also, two processes must not change settings at the same
1970 time, or start a machine at the same time.</para>
1971
1972 <para>These requirements are implemented in the Main API by way of
1973 "sessions", in particular, the <xref linkend="ISession"
1974 xreflabel="ISession" /> interface. Each process which talks to
1975 VirtualBox needs its own instance of ISession. In the web service, you
1976 cannot create such an object, but
1977 <computeroutput>vboxwebsrv</computeroutput> creates one for you when you
1978 log on, which you can obtain by calling <xref
1979 linkend="IWebsessionManager__getSessionObject"
1980 xreflabel="IWebsessionManager::getSessionObject()" />.</para>
1981
1982 <para>This session object must then be used like a mutex semaphore in
1983 common programming environments. Before you can change machine settings,
1984 you must write-lock the machine by calling <xref
1985 linkend="IMachine__lockMachine" xreflabel="IMachine::lockMachine()" />
1986 with your process's session object.</para>
1987
1988 <para>After the machine has been locked, the <xref
1989 linkend="ISession__machine" xreflabel="ISession::machine" /> attribute
1990 contains a copy of the original IMachine object upon which the session
1991 was opened, but this copy is "mutable": you can invoke "set" methods on
1992 it.</para>
1993
1994 <para>When done making the changes to the machine, you must call <xref
1995 linkend="IMachine__saveSettings"
1996 xreflabel="IMachine::saveSettings()" />, which will copy the changes you
1997 have made from your "mutable" machine back to the real machine and write
1998 them out to the machine settings XML file. This will make your changes
1999 permanent.</para>
2000
2001 <para>Finally, it is important to always unlock the machine again, by
2002 calling <xref linkend="ISession__unlockMachine"
2003 xreflabel="ISession::unlockMachine()" />. Otherwise, when the calling
2004 process end, the machine will receive the "aborted" state, which can
2005 lead to loss of data.</para>
2006
2007 <para>So, as an example, the sequence to change a machine's memory to
2008 1024 MB is something like this:<screen>IWebsessionManager mgr ...;
2009IVirtualBox vbox = mgr.logon(user, pass);
2010...
2011IMachine machine = ...; // read-only machine
2012ISession session = mgr.getSessionObject();
2013machine.lockMachine(session, LockType.Write); // machine is now locked for writing
2014IMachine mutable = session.getMachine(); // obtain the mutable machine copy
2015mutable.setMemorySize(1024);
2016mutable.saveSettings(); // write settings to XML
2017session.unlockMachine();</screen></para>
2018 </sect1>
2019
2020 <sect1>
2021 <title>Launching virtual machines</title>
2022
2023 <para>To launch a virtual machine, you call <xref
2024 linkend="IMachine__launchVMProcess"
2025 xreflabel="IMachine::launchVMProcess()" />. In doing so, the caller
2026 instructs the VirtualBox engine to start a new process with the virtual
2027 machine in it, since to the host, each virtual machine looks like a
2028 single process, even if it has hundreds of its own processes inside.
2029 (This new VM process in turn obtains a write lock on the machine, as
2030 described above, to prevent conflicting changes from other processes;
2031 this is why opening another session will fail while the VM is
2032 running.)</para>
2033
2034 <para>Starting a machine looks something like this:<screen>IWebsessionManager mgr ...;
2035IVirtualBox vbox = mgr.logon(user, pass);
2036...
2037IMachine machine = ...; // read-only machine
2038ISession session = mgr.getSessionObject();
2039IProgress prog = machine.launchVMProcess(session,
2040 "gui", // session type
2041 ""); // possibly environment setting
2042prog.waitForCompletion(10000); // give the process 10 secs
2043if (prog.getResultCode() != 0) // check success
2044 System.out.println("Cannot launch VM!")</screen></para>
2045
2046 <para>The caller's session object can then be used as a sort of remote
2047 control to the VM process that was launched. It contains a "console"
2048 object (see <xref linkend="ISession__console"
2049 xreflabel="ISession::console" />) with which the VM can be paused,
2050 stopped, snapshotted or other things.</para>
2051 </sect1>
2052
2053 <sect1>
2054 <title>VirtualBox events</title>
2055
2056 <para>In VirtualBox, "events" provide a uniform mechanism to register
2057 for and consume specific events. A VirtualBox client can register an
2058 "event listener" (represented by the <xref linkend="IEventListener"
2059 xreflabel="IEventListener" /> interface), which will then get notified
2060 by the server when an event (represented by the <xref linkend="IEvent"
2061 xreflabel="IEvent" /> interface) happens.</para>
2062
2063 <para>The IEvent interface is an abstract parent interface for all
2064 events that can occur in VirtualBox. The actual events that the server
2065 sends out are then of one of the specific subclasses, for example <xref
2066 linkend="IMachineStateChangedEvent"
2067 xreflabel="IMachineStateChangedEvent" /> or <xref
2068 linkend="IMediumChangedEvent" xreflabel="IMediumChangedEvent" />.</para>
2069
2070 <para>As an example, the VirtualBox GUI waits for machine events and can
2071 thus update its display when the machine state changes or machine
2072 settings are modified, even if this happens in another client. This is
2073 how the GUI can automatically refresh its display even if you manipulate
2074 a machine from another client, for example, from VBoxManage.</para>
2075
2076 <para>To register an event listener to listen to events, use code like
2077 this:<screen>EventSource es = console.getEventSource();
2078IEventListener listener = es.createListener();
2079VBoxEventType aTypes[] = (VBoxEventType.OnMachineStateChanged);
2080 // list of event types to listen for
2081es.registerListener(listener, aTypes, false /* active */);
2082 // register passive listener
2083IEvent ev = es.getEvent(listener, 1000);
2084 // wait up to one second for event to happen
2085if (ev != null)
2086{
2087 // downcast to specific event interface (in this case we have only registered
2088 // for one type, otherwise IEvent::type would tell us)
2089 IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);
2090 ... // inspect and do something
2091 es.eventProcessed(listener, ev);
2092}
2093...
2094es.unregisterListener(listener); </screen></para>
2095
2096 <para>A graphical user interface would probably best start its own
2097 thread to wait for events and then process these in a loop.</para>
2098
2099 <para>The events mechanism was introduced with VirtualBox 3.3 and
2100 replaces various callback interfaces which were called for each event in
2101 the interface. The callback mechanism was not compatible with scripting
2102 languages, local Java bindings and remote web services as they do not
2103 support callbacks. The new mechanism with events and event listeners
2104 works with all of these.</para>
2105
2106 <para>To simplify developement of application using events, concept of
2107 event aggregator was introduced. Essentially it's mechanism to aggregate
2108 multiple event sources into single one, and then work with this single
2109 aggregated event source instead of original sources. As an example, one
2110 can evaluate demo recorder in VirtualBox Python shell, shipped with SDK
2111 - it records mouse and keyboard events, represented as separate event
2112 sources. Code is essentially like this:<screen>
2113 listener = console.eventSource.createListener()
2114 agg = console.eventSource.createAggregator([console.keyboard.eventSource, console.mouse.eventSource])
2115 agg.registerListener(listener, [ctx['global'].constants.VBoxEventType_Any], False)
2116 registered = True
2117 end = time.time() + dur
2118 while time.time() &lt; end:
2119 ev = agg.getEvent(listener, 1000)
2120 processEent(ev)
2121 agg.unregisterListener(listener)</screen> Without using aggregators
2122 consumer have to poll on both sources, or start multiple threads to
2123 block on those sources.</para>
2124 </sect1>
2125 </chapter>
2126
2127 <chapter id="vboxshell">
2128 <title>The VirtualBox shell</title>
2129
2130 <para>VirtualBox comes with an extensible shell, which allows you to
2131 control your virtual machines from the command line. It is also a
2132 nontrivial example of how to use the VirtualBox APIs from Python, for all
2133 three COM/XPCOM/WS styles of the API.</para>
2134
2135 <para>You can easily extend this shell with your own commands. Create a
2136 subdirectory named <computeroutput>.VirtualBox/shexts</computeroutput>
2137 below your home directory and put a Python file implementing your shell
2138 extension commands in this directory. This file must contain an array
2139 named <computeroutput>commands</computeroutput> containing your command
2140 definitions: <screen>
2141 commands = {
2142 'cmd1': ['Command cmd1 help', cmd1],
2143 'cmd2': ['Command cmd2 help', cmd2]
2144 }
2145 </screen> For example, to create a command for creating hard drive
2146 images, the following code can be used: <screen>
2147 def createHdd(ctx,args):
2148 # Show some meaningful error message on wrong input
2149 if (len(args) &lt; 3):
2150 print "usage: createHdd sizeM location type"
2151 return 0
2152
2153 # Get arguments
2154 size = int(args[1])
2155 loc = args[2]
2156 if len(args) &gt; 3:
2157 format = args[3]
2158 else:
2159 # And provide some meaningful defaults
2160 format = "vdi"
2161
2162 # Call VirtualBox API, using context's fields
2163 hdd = ctx['vb'].createHardDisk(format, loc)
2164 # Access constants using ctx['global'].constants
2165 progress = hdd.createBaseStorage(size, ctx['global'].constants.HardDiskVariant_Standard)
2166 # use standard progress bar mechanism
2167 ctx['progressBar'](progress)
2168
2169
2170 # Report errors
2171 if not hdd.id:
2172 print "cannot create disk (file %s exist?)" %(loc)
2173 return 0
2174
2175 # Give user some feedback on success too
2176 print "created HDD with id: %s" %(hdd.id)
2177
2178 # 0 means continue execution, other values mean exit from the interpreter
2179 return 0
2180
2181 commands = {
2182 'myCreateHDD': ['Create virtual HDD, createHdd size location type', createHdd]
2183 }
2184 </screen> Just store the above text in the file
2185 <computeroutput>createHdd</computeroutput> (or any other meaningful name)
2186 in <computeroutput>.VirtualBox/shexts/</computeroutput>. Start the
2187 VirtualBox shell, or just issue the
2188 <computeroutput>reloadExts</computeroutput> command, if the shell is
2189 already running. Your new command will now be available.</para>
2190 </chapter>
2191
2192 <!--$VIRTUALBOX_MAIN_API_REFERENCE-->
2193
2194 <chapter id="hgcm">
2195 <title>Host-Guest Communication Manager</title>
2196
2197 <para>The VirtualBox Host-Guest Communication Manager (HGCM) allows a
2198 guest application or a guest driver to call a host shared library. The
2199 following features of VirtualBox are implemented using HGCM: <itemizedlist>
2200 <listitem>
2201 <para>Shared Folders</para>
2202 </listitem>
2203
2204 <listitem>
2205 <para>Shared Clipboard</para>
2206 </listitem>
2207
2208 <listitem>
2209 <para>Guest configuration interface</para>
2210 </listitem>
2211 </itemizedlist></para>
2212
2213 <para>The shared library contains a so called HGCM service. The guest HGCM
2214 clients establish connections to the service to call it. When calling a
2215 HGCM service the client supplies a function code and a number of
2216 parameters for the function.</para>
2217
2218 <sect1>
2219 <title>Virtual hardware implementation</title>
2220
2221 <para>HGCM uses the VMM virtual PCI device to exchange data between the
2222 guest and the host. The guest always acts as an initiator of requests. A
2223 request is constructed in the guest physical memory, which must be
2224 locked by the guest. The physical address is passed to the VMM device
2225 using a 32 bit <computeroutput>out edx, eax</computeroutput>
2226 instruction. The physical memory must be allocated below 4GB by 64 bit
2227 guests.</para>
2228
2229 <para>The host parses the request header and data and queues the request
2230 for a host HGCM service. The guest continues execution and usually waits
2231 on a HGCM event semaphore.</para>
2232
2233 <para>When the request has been processed by the HGCM service, the VMM
2234 device sets the completion flag in the request header, sets the HGCM
2235 event and raises an IRQ for the guest. The IRQ handler signals the HGCM
2236 event semaphore and all HGCM callers check the completion flag in the
2237 corresponding request header. If the flag is set, the request is
2238 considered completed.</para>
2239 </sect1>
2240
2241 <sect1>
2242 <title>Protocol specification</title>
2243
2244 <para>The HGCM protocol definitions are contained in the
2245 <computeroutput>VBox/VBoxGuest.h</computeroutput></para>
2246
2247 <sect2>
2248 <title>Request header</title>
2249
2250 <para>HGCM request structures contains a generic header
2251 (VMMDevHGCMRequestHeader): <table>
2252 <title>HGCM Request Generic Header</title>
2253
2254 <tgroup cols="2">
2255 <tbody>
2256 <row>
2257 <entry><emphasis role="bold">Name</emphasis></entry>
2258
2259 <entry><emphasis role="bold">Description</emphasis></entry>
2260 </row>
2261
2262 <row>
2263 <entry>size</entry>
2264
2265 <entry>Size of the entire request.</entry>
2266 </row>
2267
2268 <row>
2269 <entry>version</entry>
2270
2271 <entry>Version of the header, must be set to
2272 <computeroutput>0x10001</computeroutput>.</entry>
2273 </row>
2274
2275 <row>
2276 <entry>type</entry>
2277
2278 <entry>Type of the request.</entry>
2279 </row>
2280
2281 <row>
2282 <entry>rc</entry>
2283
2284 <entry>HGCM return code, which will be set by the VMM
2285 device.</entry>
2286 </row>
2287
2288 <row>
2289 <entry>reserved1</entry>
2290
2291 <entry>A reserved field 1.</entry>
2292 </row>
2293
2294 <row>
2295 <entry>reserved2</entry>
2296
2297 <entry>A reserved field 2.</entry>
2298 </row>
2299
2300 <row>
2301 <entry>flags</entry>
2302
2303 <entry>HGCM flags, set by the VMM device.</entry>
2304 </row>
2305
2306 <row>
2307 <entry>result</entry>
2308
2309 <entry>The HGCM result code, set by the VMM device.</entry>
2310 </row>
2311 </tbody>
2312 </tgroup>
2313 </table> <note>
2314 <itemizedlist>
2315 <listitem>
2316 <para>All fields are 32 bit.</para>
2317 </listitem>
2318
2319 <listitem>
2320 <para>Fields from <computeroutput>size</computeroutput> to
2321 <computeroutput>reserved2</computeroutput> are a standard VMM
2322 device request header, which is used for other interfaces as
2323 well.</para>
2324 </listitem>
2325 </itemizedlist>
2326 </note></para>
2327
2328 <para>The <emphasis role="bold">type</emphasis> field indicates the
2329 type of the HGCM request: <table>
2330 <title>Request Types</title>
2331
2332 <tgroup cols="2">
2333 <tbody>
2334 <row>
2335 <entry><emphasis role="bold">Name (decimal
2336 value)</emphasis></entry>
2337
2338 <entry><emphasis role="bold">Description</emphasis></entry>
2339 </row>
2340
2341 <row>
2342 <entry>VMMDevReq_HGCMConnect
2343 (<computeroutput>60</computeroutput>)</entry>
2344
2345 <entry>Connect to a HGCM service.</entry>
2346 </row>
2347
2348 <row>
2349 <entry>VMMDevReq_HGCMDisconnect
2350 (<computeroutput>61</computeroutput>)</entry>
2351
2352 <entry>Disconnect from the service.</entry>
2353 </row>
2354
2355 <row>
2356 <entry>VMMDevReq_HGCMCall32
2357 (<computeroutput>62</computeroutput>)</entry>
2358
2359 <entry>Call a HGCM function using the 32 bit
2360 interface.</entry>
2361 </row>
2362
2363 <row>
2364 <entry>VMMDevReq_HGCMCall64
2365 (<computeroutput>63</computeroutput>)</entry>
2366
2367 <entry>Call a HGCM function using the 64 bit
2368 interface.</entry>
2369 </row>
2370
2371 <row>
2372 <entry>VMMDevReq_HGCMCancel
2373 (<computeroutput>64</computeroutput>)</entry>
2374
2375 <entry>Cancel a HGCM request currently being processed by a
2376 host HGCM service.</entry>
2377 </row>
2378 </tbody>
2379 </tgroup>
2380 </table></para>
2381
2382 <para>The <emphasis role="bold">flags</emphasis> field may contain:
2383 <table>
2384 <title>Flags</title>
2385
2386 <tgroup cols="2">
2387 <tbody>
2388 <row>
2389 <entry><emphasis role="bold">Name (hexadecimal
2390 value)</emphasis></entry>
2391
2392 <entry><emphasis role="bold">Description</emphasis></entry>
2393 </row>
2394
2395 <row>
2396 <entry>VBOX_HGCM_REQ_DONE
2397 (<computeroutput>0x00000001</computeroutput>)</entry>
2398
2399 <entry>The request has been processed by the host
2400 service.</entry>
2401 </row>
2402
2403 <row>
2404 <entry>VBOX_HGCM_REQ_CANCELLED
2405 (<computeroutput>0x00000002</computeroutput>)</entry>
2406
2407 <entry>This request was cancelled.</entry>
2408 </row>
2409 </tbody>
2410 </tgroup>
2411 </table></para>
2412 </sect2>
2413
2414 <sect2>
2415 <title>Connect</title>
2416
2417 <para>The connection request must be issued by the guest HGCM client
2418 before it can call the HGCM service (VMMDevHGCMConnect): <table>
2419 <title>Connect request</title>
2420
2421 <tgroup cols="2">
2422 <tbody>
2423 <row>
2424 <entry><emphasis role="bold">Name</emphasis></entry>
2425
2426 <entry><emphasis role="bold">Description</emphasis></entry>
2427 </row>
2428
2429 <row>
2430 <entry>header</entry>
2431
2432 <entry>The generic HGCM request header with type equal to
2433 VMMDevReq_HGCMConnect
2434 (<computeroutput>60</computeroutput>).</entry>
2435 </row>
2436
2437 <row>
2438 <entry>type</entry>
2439
2440 <entry>The type of the service location information (32
2441 bit).</entry>
2442 </row>
2443
2444 <row>
2445 <entry>location</entry>
2446
2447 <entry>The service location information (128 bytes).</entry>
2448 </row>
2449
2450 <row>
2451 <entry>clientId</entry>
2452
2453 <entry>The client identifier assigned to the connecting
2454 client by the HGCM subsystem (32 bit).</entry>
2455 </row>
2456 </tbody>
2457 </tgroup>
2458 </table> The <emphasis role="bold">type</emphasis> field tells the
2459 HGCM how to look for the requested service: <table>
2460 <title>Location Information Types</title>
2461
2462 <tgroup cols="2">
2463 <tbody>
2464 <row>
2465 <entry><emphasis role="bold">Name (hexadecimal
2466 value)</emphasis></entry>
2467
2468 <entry><emphasis role="bold">Description</emphasis></entry>
2469 </row>
2470
2471 <row>
2472 <entry>VMMDevHGCMLoc_LocalHost
2473 (<computeroutput>0x1</computeroutput>)</entry>
2474
2475 <entry>The requested service is a shared library located on
2476 the host and the location information contains the library
2477 name.</entry>
2478 </row>
2479
2480 <row>
2481 <entry>VMMDevHGCMLoc_LocalHost_Existing
2482 (<computeroutput>0x2</computeroutput>)</entry>
2483
2484 <entry>The requested service is a preloaded one and the
2485 location information contains the service name.</entry>
2486 </row>
2487 </tbody>
2488 </tgroup>
2489 </table> <note>
2490 <para>Currently preloaded HGCM services are hard-coded in
2491 VirtualBox: <itemizedlist>
2492 <listitem>
2493 <para>VBoxSharedFolders</para>
2494 </listitem>
2495
2496 <listitem>
2497 <para>VBoxSharedClipboard</para>
2498 </listitem>
2499
2500 <listitem>
2501 <para>VBoxGuestPropSvc</para>
2502 </listitem>
2503
2504 <listitem>
2505 <para>VBoxSharedOpenGL</para>
2506 </listitem>
2507 </itemizedlist></para>
2508 </note> There is no difference between both types of HGCM services,
2509 only the location mechanism is different.</para>
2510
2511 <para>The client identifier is returned by the host and must be used
2512 in all subsequent requests by the client.</para>
2513 </sect2>
2514
2515 <sect2>
2516 <title>Disconnect</title>
2517
2518 <para>This request disconnects the client and makes the client
2519 identifier invalid (VMMDevHGCMDisconnect): <table>
2520 <title>Disconnect request</title>
2521
2522 <tgroup cols="2">
2523 <tbody>
2524 <row>
2525 <entry><emphasis role="bold">Name</emphasis></entry>
2526
2527 <entry><emphasis role="bold">Description</emphasis></entry>
2528 </row>
2529
2530 <row>
2531 <entry>header</entry>
2532
2533 <entry>The generic HGCM request header with type equal to
2534 VMMDevReq_HGCMDisconnect
2535 (<computeroutput>61</computeroutput>).</entry>
2536 </row>
2537
2538 <row>
2539 <entry>clientId</entry>
2540
2541 <entry>The client identifier previously returned by the
2542 connect request (32 bit).</entry>
2543 </row>
2544 </tbody>
2545 </tgroup>
2546 </table></para>
2547 </sect2>
2548
2549 <sect2>
2550 <title>Call32 and Call64</title>
2551
2552 <para>Calls the HGCM service entry point (VMMDevHGCMCall) using 32 bit
2553 or 64 bit addresses: <table>
2554 <title>Call request</title>
2555
2556 <tgroup cols="2">
2557 <tbody>
2558 <row>
2559 <entry><emphasis role="bold">Name</emphasis></entry>
2560
2561 <entry><emphasis role="bold">Description</emphasis></entry>
2562 </row>
2563
2564 <row>
2565 <entry>header</entry>
2566
2567 <entry>The generic HGCM request header with type equal to
2568 either VMMDevReq_HGCMCall32
2569 (<computeroutput>62</computeroutput>) or
2570 VMMDevReq_HGCMCall64
2571 (<computeroutput>63</computeroutput>).</entry>
2572 </row>
2573
2574 <row>
2575 <entry>clientId</entry>
2576
2577 <entry>The client identifier previously returned by the
2578 connect request (32 bit).</entry>
2579 </row>
2580
2581 <row>
2582 <entry>function</entry>
2583
2584 <entry>The function code to be processed by the service (32
2585 bit).</entry>
2586 </row>
2587
2588 <row>
2589 <entry>cParms</entry>
2590
2591 <entry>The number of following parameters (32 bit). This
2592 value is 0 if the function requires no parameters.</entry>
2593 </row>
2594
2595 <row>
2596 <entry>parms</entry>
2597
2598 <entry>An array of parameter description structures
2599 (HGCMFunctionParameter32 or
2600 HGCMFunctionParameter64).</entry>
2601 </row>
2602 </tbody>
2603 </tgroup>
2604 </table></para>
2605
2606 <para>The 32 bit parameter description (HGCMFunctionParameter32)
2607 consists of 32 bit type field and 8 bytes of an opaque value, so 12
2608 bytes in total. The 64 bit variant (HGCMFunctionParameter64) consists
2609 of the type and 12 bytes of a value, so 16 bytes in total.</para>
2610
2611 <para><table>
2612 <title>Parameter types</title>
2613
2614 <tgroup cols="2">
2615 <tbody>
2616 <row>
2617 <entry><emphasis role="bold">Type</emphasis></entry>
2618
2619 <entry><emphasis role="bold">Format of the
2620 value</emphasis></entry>
2621 </row>
2622
2623 <row>
2624 <entry>VMMDevHGCMParmType_32bit (1)</entry>
2625
2626 <entry>A 32 bit value.</entry>
2627 </row>
2628
2629 <row>
2630 <entry>VMMDevHGCMParmType_64bit (2)</entry>
2631
2632 <entry>A 64 bit value.</entry>
2633 </row>
2634
2635 <row>
2636 <entry>VMMDevHGCMParmType_PhysAddr (3)</entry>
2637
2638 <entry>A 32 bit size followed by a 32 bit or 64 bit guest
2639 physical address.</entry>
2640 </row>
2641
2642 <row>
2643 <entry>VMMDevHGCMParmType_LinAddr (4)</entry>
2644
2645 <entry>A 32 bit size followed by a 32 bit or 64 bit guest
2646 linear address. The buffer is used both for guest to host
2647 and for host to guest data.</entry>
2648 </row>
2649
2650 <row>
2651 <entry>VMMDevHGCMParmType_LinAddr_In (5)</entry>
2652
2653 <entry>Same as VMMDevHGCMParmType_LinAddr but the buffer is
2654 used only for host to guest data.</entry>
2655 </row>
2656
2657 <row>
2658 <entry>VMMDevHGCMParmType_LinAddr_Out (6)</entry>
2659
2660 <entry>Same as VMMDevHGCMParmType_LinAddr but the buffer is
2661 used only for guest to host data.</entry>
2662 </row>
2663
2664 <row>
2665 <entry>VMMDevHGCMParmType_LinAddr_Locked (7)</entry>
2666
2667 <entry>Same as VMMDevHGCMParmType_LinAddr but the buffer is
2668 already locked by the guest.</entry>
2669 </row>
2670
2671 <row>
2672 <entry>VMMDevHGCMParmType_LinAddr_Locked_In (1)</entry>
2673
2674 <entry>Same as VMMDevHGCMParmType_LinAddr_In but the buffer
2675 is already locked by the guest.</entry>
2676 </row>
2677
2678 <row>
2679 <entry>VMMDevHGCMParmType_LinAddr_Locked_Out (1)</entry>
2680
2681 <entry>Same as VMMDevHGCMParmType_LinAddr_Out but the buffer
2682 is already locked by the guest.</entry>
2683 </row>
2684 </tbody>
2685 </tgroup>
2686 </table></para>
2687
2688 <para>The</para>
2689 </sect2>
2690
2691 <sect2>
2692 <title>Cancel</title>
2693
2694 <para>This request cancels a call request (VMMDevHGCMCancel): <table>
2695 <title>Cancel request</title>
2696
2697 <tgroup cols="2">
2698 <tbody>
2699 <row>
2700 <entry><emphasis role="bold">Name</emphasis></entry>
2701
2702 <entry><emphasis role="bold">Description</emphasis></entry>
2703 </row>
2704
2705 <row>
2706 <entry>header</entry>
2707
2708 <entry>The generic HGCM request header with type equal to
2709 VMMDevReq_HGCMCancel
2710 (<computeroutput>64</computeroutput>).</entry>
2711 </row>
2712 </tbody>
2713 </tgroup>
2714 </table></para>
2715 </sect2>
2716 </sect1>
2717
2718 <sect1>
2719 <title>Guest software interface</title>
2720
2721 <para>The guest HGCM clients can call HGCM services from both drivers
2722 and applications.</para>
2723
2724 <sect2>
2725 <title>The guest driver interface</title>
2726
2727 <para>The driver interface is implemented in the VirtualBox guest
2728 additions driver (VBoxGuest), which works with the VMM virtual device.
2729 Drivers must use the VBox Guest Library (VBGL), which provides an API
2730 for HGCM clients (<computeroutput>VBox/VBoxGuestLib.h</computeroutput>
2731 and <computeroutput>VBox/VBoxGuest.h</computeroutput>).</para>
2732
2733 <para><screen>
2734DECLVBGL(int) VbglHGCMConnect (VBGLHGCMHANDLE *pHandle, VBoxGuestHGCMConnectInfo *pData);
2735 </screen> Connects to the service: <screen>
2736 VBoxGuestHGCMConnectInfo data;
2737
2738 memset (&amp;data, sizeof (VBoxGuestHGCMConnectInfo));
2739
2740 data.result = VINF_SUCCESS;
2741 data.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
2742 strcpy (data.Loc.u.host.achName, "VBoxSharedFolders");
2743
2744 rc = VbglHGCMConnect (&amp;handle, &amp;data);
2745
2746 if (RT_SUCCESS (rc))
2747 {
2748 rc = data.result;
2749 }
2750
2751 if (RT_SUCCESS (rc))
2752 {
2753 /* Get the assigned client identifier. */
2754 ulClientID = data.u32ClientID;
2755 }
2756 </screen></para>
2757
2758 <para><screen>
2759DECLVBGL(int) VbglHGCMDisconnect (VBGLHGCMHANDLE handle, VBoxGuestHGCMDisconnectInfo *pData);
2760 </screen> Disconnects from the service. <screen>
2761 VBoxGuestHGCMDisconnectInfo data;
2762
2763 RtlZeroMemory (&amp;data, sizeof (VBoxGuestHGCMDisconnectInfo));
2764
2765 data.result = VINF_SUCCESS;
2766 data.u32ClientID = ulClientID;
2767
2768 rc = VbglHGCMDisconnect (handle, &amp;data);
2769 </screen></para>
2770
2771 <para><screen>
2772DECLVBGL(int) VbglHGCMCall (VBGLHGCMHANDLE handle, VBoxGuestHGCMCallInfo *pData, uint32_t cbData);
2773 </screen> Calls a function in the service. <screen>
2774typedef struct _VBoxSFRead
2775{
2776 VBoxGuestHGCMCallInfo callInfo;
2777
2778 /** pointer, in: SHFLROOT
2779 * Root handle of the mapping which name is queried.
2780 */
2781 HGCMFunctionParameter root;
2782
2783 /** value64, in:
2784 * SHFLHANDLE of object to read from.
2785 */
2786 HGCMFunctionParameter handle;
2787
2788 /** value64, in:
2789 * Offset to read from.
2790 */
2791 HGCMFunctionParameter offset;
2792
2793 /** value64, in/out:
2794 * Bytes to read/How many were read.
2795 */
2796 HGCMFunctionParameter cb;
2797
2798 /** pointer, out:
2799 * Buffer to place data to.
2800 */
2801 HGCMFunctionParameter buffer;
2802
2803} VBoxSFRead;
2804
2805/** Number of parameters */
2806#define SHFL_CPARMS_READ (5)
2807
2808...
2809
2810 VBoxSFRead data;
2811
2812 /* The call information. */
2813 data.callInfo.result = VINF_SUCCESS; /* Will be returned by HGCM. */
2814 data.callInfo.u32ClientID = ulClientID; /* Client identifier. */
2815 data.callInfo.u32Function = SHFL_FN_READ; /* The function code. */
2816 data.callInfo.cParms = SHFL_CPARMS_READ; /* Number of parameters. */
2817
2818 /* Initialize parameters. */
2819 data.root.type = VMMDevHGCMParmType_32bit;
2820 data.root.u.value32 = pMap-&gt;root;
2821
2822 data.handle.type = VMMDevHGCMParmType_64bit;
2823 data.handle.u.value64 = hFile;
2824
2825 data.offset.type = VMMDevHGCMParmType_64bit;
2826 data.offset.u.value64 = offset;
2827
2828 data.cb.type = VMMDevHGCMParmType_32bit;
2829 data.cb.u.value32 = *pcbBuffer;
2830
2831 data.buffer.type = VMMDevHGCMParmType_LinAddr_Out;
2832 data.buffer.u.Pointer.size = *pcbBuffer;
2833 data.buffer.u.Pointer.u.linearAddr = (uintptr_t)pBuffer;
2834
2835 rc = VbglHGCMCall (handle, &amp;data.callInfo, sizeof (data));
2836
2837 if (RT_SUCCESS (rc))
2838 {
2839 rc = data.callInfo.result;
2840 *pcbBuffer = data.cb.u.value32; /* This is returned by the HGCM service. */
2841 }
2842 </screen></para>
2843 </sect2>
2844
2845 <sect2>
2846 <title>Guest application interface</title>
2847
2848 <para>Applications call the VirtualBox Guest Additions driver to
2849 utilize the HGCM interface. There are IOCTL's which correspond to the
2850 <computeroutput>Vbgl*</computeroutput> functions: <itemizedlist>
2851 <listitem>
2852 <para><computeroutput>VBOXGUEST_IOCTL_HGCM_CONNECT</computeroutput></para>
2853 </listitem>
2854
2855 <listitem>
2856 <para><computeroutput>VBOXGUEST_IOCTL_HGCM_DISCONNECT</computeroutput></para>
2857 </listitem>
2858
2859 <listitem>
2860 <para><computeroutput>VBOXGUEST_IOCTL_HGCM_CALL</computeroutput></para>
2861 </listitem>
2862 </itemizedlist></para>
2863
2864 <para>These IOCTL's get the same input buffer as
2865 <computeroutput>VbglHGCM*</computeroutput> functions and the output
2866 buffer has the same format as the input buffer. The same address can
2867 be used as the input and output buffers.</para>
2868
2869 <para>For example see the guest part of shared clipboard, which runs
2870 as an application and uses the HGCM interface.</para>
2871 </sect2>
2872 </sect1>
2873
2874 <sect1>
2875 <title>HGCM Service Implementation</title>
2876
2877 <para>The HGCM service is a shared library with a specific set of entry
2878 points. The library must export the
2879 <computeroutput>VBoxHGCMSvcLoad</computeroutput> entry point: <screen>
2880extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad (VBOXHGCMSVCFNTABLE *ptable)
2881 </screen></para>
2882
2883 <para>The service must check the
2884 <computeroutput>ptable-&gt;cbSize</computeroutput> and
2885 <computeroutput>ptable-&gt;u32Version</computeroutput> fields of the
2886 input structure and fill the remaining fields with function pointers of
2887 entry points and the size of the required client buffer size.</para>
2888
2889 <para>The HGCM service gets a dedicated thread, which calls service
2890 entry points synchronously, that is the service will be called again
2891 only when a previous call has returned. However, the guest calls can be
2892 processed asynchronously. The service must call a completion callback
2893 when the operation is actually completed. The callback can be issued
2894 from another thread as well.</para>
2895
2896 <para>Service entry points are listed in the
2897 <computeroutput>VBox/hgcmsvc.h</computeroutput> in the
2898 <computeroutput>VBOXHGCMSVCFNTABLE</computeroutput> structure. <table>
2899 <title>Service entry points</title>
2900
2901 <tgroup cols="2">
2902 <tbody>
2903 <row>
2904 <entry><emphasis role="bold">Entry</emphasis></entry>
2905
2906 <entry><emphasis role="bold">Description</emphasis></entry>
2907 </row>
2908
2909 <row>
2910 <entry>pfnUnload</entry>
2911
2912 <entry>The service is being unloaded.</entry>
2913 </row>
2914
2915 <row>
2916 <entry>pfnConnect</entry>
2917
2918 <entry>A client <computeroutput>u32ClientID</computeroutput>
2919 is connected to the service. The
2920 <computeroutput>pvClient</computeroutput> parameter points to
2921 an allocated memory buffer which can be used by the service to
2922 store the client information.</entry>
2923 </row>
2924
2925 <row>
2926 <entry>pfnDisconnect</entry>
2927
2928 <entry>A client is being disconnected.</entry>
2929 </row>
2930
2931 <row>
2932 <entry>pfnCall</entry>
2933
2934 <entry>A guest client calls a service function. The
2935 <computeroutput>callHandle</computeroutput> must be used in
2936 the VBOXHGCMSVCHELPERS::pfnCallComplete callback when the call
2937 has been processed.</entry>
2938 </row>
2939
2940 <row>
2941 <entry>pfnHostCall</entry>
2942
2943 <entry>Called by the VirtualBox host components to perform
2944 functions which should be not accessible by the guest. Usually
2945 this entry point is used by VirtualBox to configure the
2946 service.</entry>
2947 </row>
2948
2949 <row>
2950 <entry>pfnSaveState</entry>
2951
2952 <entry>The VM state is being saved and the service must save
2953 relevant information using the SSM API
2954 (<computeroutput>VBox/ssm.h</computeroutput>).</entry>
2955 </row>
2956
2957 <row>
2958 <entry>pfnLoadState</entry>
2959
2960 <entry>The VM is being restored from the saved state and the
2961 service must load the saved information and be able to
2962 continue operations from the saved state.</entry>
2963 </row>
2964 </tbody>
2965 </tgroup>
2966 </table></para>
2967 </sect1>
2968 </chapter>
2969
2970 <chapter id="rdpweb">
2971 <title>RDP Web Control</title>
2972
2973 <para>The VirtualBox <emphasis>RDP Web Control</emphasis> (RDPWeb)
2974 provides remote access to a running VM. RDPWeb is a RDP (Remote Desktop
2975 Protocol) client based on Flash technology and can be used from a Web
2976 browser with a Flash plugin.</para>
2977
2978 <sect1>
2979 <title>RDPWeb features</title>
2980
2981 <para>RDPWeb is embedded into a Web page and can connect to VRDP server
2982 in order to displays the VM screen and pass keyboard and mouse events to
2983 the VM.</para>
2984 </sect1>
2985
2986 <sect1>
2987 <title>RDPWeb reference</title>
2988
2989 <para>RDPWeb consists of two required components:<itemizedlist>
2990 <listitem>
2991 <para>Flash movie
2992 <computeroutput>RDPClientUI.swf</computeroutput></para>
2993 </listitem>
2994
2995 <listitem>
2996 <para>JavaScript helpers
2997 <computeroutput>webclient.js</computeroutput></para>
2998 </listitem>
2999 </itemizedlist></para>
3000
3001 <para>The VirtualBox SDK contains sample HTML code
3002 including:<itemizedlist>
3003 <listitem>
3004 <para>JavaScript library for embedding Flash content
3005 <computeroutput>SWFObject.js</computeroutput></para>
3006 </listitem>
3007
3008 <listitem>
3009 <para>Sample HTML page
3010 <computeroutput>webclient3.html</computeroutput></para>
3011 </listitem>
3012 </itemizedlist></para>
3013
3014 <sect2>
3015 <title>RDPWeb functions</title>
3016
3017 <para><computeroutput>RDPClientUI.swf</computeroutput> and
3018 <computeroutput>webclient.js</computeroutput> work with each other.
3019 JavaScript code is responsible for a proper SWF initialization,
3020 delivering mouse events to the SWF and processing resize requests from
3021 the SWF. On the other hand, the SWF contains a few JavaScript callable
3022 methods, which are used both from
3023 <computeroutput>webclient.js</computeroutput> and the user HTML
3024 page.</para>
3025
3026 <sect3>
3027 <title>JavaScript functions</title>
3028
3029 <para><computeroutput>webclient.js</computeroutput> contains helper
3030 functions. In the following table ElementId refers to an HTML
3031 element name or attribute, and Element to the HTML element itself.
3032 HTML code<programlisting>
3033 &lt;div id="FlashRDP"&gt;
3034 &lt;/div&gt;
3035</programlisting> would have ElementId equal to FlashRDP and Element equal to
3036 the div element.</para>
3037
3038 <para><itemizedlist>
3039 <listitem>
3040 <programlisting>RDPWebClient.embedSWF(SWFFileName, ElementId)</programlisting>
3041
3042 <para>Uses SWFObject library to replace the HTML element with
3043 the Flash movie.</para>
3044 </listitem>
3045
3046 <listitem>
3047 <programlisting>RDPWebClient.isRDPWebControlById(ElementId)</programlisting>
3048
3049 <para>Returns true if the given id refers to a RDPWeb Flash
3050 element.</para>
3051 </listitem>
3052
3053 <listitem>
3054 <programlisting>RDPWebClient.isRDPWebControlByElement(Element)</programlisting>
3055
3056 <para>Returns true if the given element is a RDPWeb Flash
3057 element.</para>
3058 </listitem>
3059
3060 <listitem>
3061 <programlisting>RDPWebClient.getFlashById(ElementId)</programlisting>
3062
3063 <para>Returns an element, which is referenced by the given id.
3064 This function will try to resolve any element, event if it is
3065 not a Flash movie.</para>
3066 </listitem>
3067 </itemizedlist></para>
3068 </sect3>
3069
3070 <sect3>
3071 <title>Flash methods callable from JavaScript</title>
3072
3073 <para><computeroutput>RDPWebClienUI.swf</computeroutput> methods can
3074 be called directly from JavaScript code on a HTML page.</para>
3075
3076 <itemizedlist>
3077 <listitem>
3078 <para>getProperty(Name)</para>
3079 </listitem>
3080
3081 <listitem>
3082 <para>setProperty(Name)</para>
3083 </listitem>
3084
3085 <listitem>
3086 <para>connect()</para>
3087 </listitem>
3088
3089 <listitem>
3090 <para>disconnect()</para>
3091 </listitem>
3092
3093 <listitem>
3094 <para>keyboardSendCAD()</para>
3095 </listitem>
3096 </itemizedlist>
3097 </sect3>
3098
3099 <sect3>
3100 <title>Flash JavaScript callbacks</title>
3101
3102 <para><computeroutput>RDPWebClienUI.swf</computeroutput> calls
3103 JavaScript functions provided by the HTML page.</para>
3104 </sect3>
3105 </sect2>
3106
3107 <sect2>
3108 <title>Embedding RDPWeb in an HTML page</title>
3109
3110 <para>It is necessary to include
3111 <computeroutput>webclient.js</computeroutput> helper script. If
3112 SWFObject library is used, the
3113 <computeroutput>swfobject.js</computeroutput> must be also included
3114 and RDPWeb flash content can be embedded to a Web page using dynamic
3115 HTML. The HTML must include a "placeholder", which consists of 2
3116 <computeroutput>div</computeroutput> elements.</para>
3117 </sect2>
3118 </sect1>
3119
3120 <sect1>
3121 <title>RDPWeb change log</title>
3122
3123 <sect2>
3124 <title>Version 1.2.28</title>
3125
3126 <itemizedlist>
3127 <listitem>
3128 <para><computeroutput>keyboardLayout</computeroutput>,
3129 <computeroutput>keyboardLayouts</computeroutput>,
3130 <computeroutput>UUID</computeroutput> properties.</para>
3131 </listitem>
3132
3133 <listitem>
3134 <para>Support for German keyboard layout on the client.</para>
3135 </listitem>
3136
3137 <listitem>
3138 <para>Rebranding to Oracle.</para>
3139 </listitem>
3140 </itemizedlist>
3141 </sect2>
3142
3143 <sect2>
3144 <title>Version 1.1.26</title>
3145
3146 <itemizedlist>
3147 <listitem>
3148 <para><computeroutput>webclient.js</computeroutput> is a part of
3149 the distribution package.</para>
3150 </listitem>
3151
3152 <listitem>
3153 <para><computeroutput>lastError</computeroutput> property.</para>
3154 </listitem>
3155
3156 <listitem>
3157 <para><computeroutput>keyboardSendScancodes</computeroutput> and
3158 <computeroutput>keyboardSendCAD</computeroutput> methods.</para>
3159 </listitem>
3160 </itemizedlist>
3161 </sect2>
3162
3163 <sect2>
3164 <title>Version 1.0.24</title>
3165
3166 <itemizedlist>
3167 <listitem>
3168 <para>Initial release.</para>
3169 </listitem>
3170 </itemizedlist>
3171 </sect2>
3172 </sect1>
3173 </chapter>
3174
3175 <chapter id="javaapi">
3176 <title>Using Java API</title>
3177
3178 <sect1>
3179 <title>Introduction</title>
3180
3181 <para>VirtualBox can be controlled by a Java API, both locally
3182 (COM/XPCOM) and from remote (SOAP) clients. As with the Python bindings,
3183 a generic glue layer tries to hide all platform differences, allowing
3184 for source and binary compatibility on different platforms.</para>
3185 </sect1>
3186
3187 <sect1>
3188 <title>Requirements</title>
3189
3190 <para>To use the Java bindings, there are certain requirements depending
3191 on the platform. First of all, you need JDK 1.5 (Java 5) or later. Also
3192 please make sure that the version of the VirtualBox API .jar file
3193 exactly matches the version of VirtualBox you use. To avoid confusion,
3194 the VirtualBox API provides versioning in the Java package name, e.g.
3195 the package is named <computeroutput>org.virtualbox_3_2</computeroutput>
3196 for VirtualBox version 3.2. <itemizedlist>
3197 <listitem>
3198 <para><emphasis role="bold">XPCOM:</emphasis> - for all platforms,
3199 but Microsoft Windows. A Java bridge based on JavaXPCOM is shipped
3200 with VirtualBox. The classpath must contain
3201 <computeroutput>vboxjxpcom.jar</computeroutput> and the
3202 <computeroutput>vbox.home</computeroutput> property must be set to
3203 location where the VirtualBox binaries are. Please make sure that
3204 the JVM bitness matches bitness of VirtualBox you use as the XPCOM
3205 bridge relies on native libraries.</para>
3206
3207 <para>Start your application like this: <programlisting>
3208 java -cp vboxjxpcom.jar -Dvbox.home=/opt/virtualbox MyProgram
3209 </programlisting></para>
3210 </listitem>
3211
3212 <listitem>
3213 <para><emphasis role="bold">COM:</emphasis> - for Microsoft
3214 Windows. We rely on <computeroutput>Jacob</computeroutput> - a
3215 generic Java to COM bridge - which has to be installed seperately.
3216 See <ulink
3217 url="http://sourceforge.net/projects/jacob-project/">http://sourceforge.net/projects/jacob-project/</ulink>
3218 for installation instructions. Also, the VirtualBox provided
3219 <computeroutput>vboxjmscom.jar</computeroutput> must be in the
3220 class path.</para>
3221
3222 <para>Start your application like this: <programlisting>
3223 java -cp vboxjmscom.jar;c:\jacob\jacob.jar -Djava.library.path=c:\jacob MyProgram
3224 </programlisting></para>
3225 </listitem>
3226
3227 <listitem>
3228 <para><emphasis role="bold">SOAP</emphasis> - all platforms. Java
3229 6 is required, as it comes with builtin support for SOAP via the
3230 JAX-WS library. Also, the VirtualBox provided
3231 <computeroutput>vbojws.jar</computeroutput> must be in the class
3232 path. In the SOAP case it's possible to create several
3233 VirtualBoxManager instances to communicate with multiple
3234 VirtualBox hosts.</para>
3235
3236 <para>Start your application like this: <programlisting>
3237 java -cp vboxjws.jar MyProgram
3238 </programlisting></para>
3239 </listitem>
3240 </itemizedlist></para>
3241
3242 <para>Exception handling is also generalized by the generic glue layer,
3243 so that all methods could throw
3244 <computeroutput>VBoxException</computeroutput> containing human-readable
3245 text message (see <computeroutput>getMessage()</computeroutput> method)
3246 along with wrapped original exception (see
3247 <computeroutput>getWrapped()</computeroutput> method).</para>
3248 </sect1>
3249
3250 <sect1>
3251 <title>Example</title>
3252
3253 <para>This example shows a simple use case of the Java API. Differences
3254 for SOAP vs. local version are minimal, and limited to the connection
3255 setup phase (see <computeroutput>ws</computeroutput> variable). In the
3256 SOAP case it's possible to create several VirtualBoxManager instances to
3257 communicate with multiple VirtualBox hosts. <programlisting>
3258 import org.virtualbox_3_3.*;
3259 ....
3260 VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
3261 boolean ws = false; // or true, if we need the SOAP version
3262 if (ws)
3263 {
3264 String url = "http://myhost:18034";
3265 String user = "test";
3266 String passwd = "test";
3267 mgr.connect(url, user, passwd);
3268 }
3269 IVirtualBox vbox = mgr.getVBox();
3270 System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
3271 // get first VM name
3272 String m = vbox.getMachines().get(0).getName();
3273 System.out.println("\nAttempting to start VM '" + m + "'");
3274 // start it
3275 mgr.startVm(m, null, 7000);
3276
3277 if (ws)
3278 mgr.disconnect();
3279
3280 mgr.cleanup();
3281 </programlisting> For more a complete example, see
3282 <computeroutput>TestVBox.java</computeroutput>, shipped with the
3283 SDK.</para>
3284 </sect1>
3285 </chapter>
3286
3287 <chapter>
3288 <title>License information</title>
3289
3290 <para>The sample code files shipped with the SDK are generally licensed
3291 liberally to make it easy for anyone to use this code for their own
3292 application code.</para>
3293
3294 <para>The Java files under
3295 <computeroutput>bindings/webservice/java/jax-ws/</computeroutput> (library
3296 files for the object-oriented web service) are, by contrast, licensed
3297 under the GNU Lesser General Public License (LGPL) V2.1.</para>
3298
3299 <para>See
3300 <computeroutput>sdk/bindings/webservice/java/jax-ws/src/COPYING.LIB</computeroutput>
3301 for the full text of the LGPL 2.1.</para>
3302
3303 <para>When in doubt, please refer to the individual source code files
3304 shipped with this SDK.</para>
3305 </chapter>
3306
3307 <chapter>
3308 <title>Main API change log</title>
3309
3310 <para>Generally, VirtualBox will maintain API compatibility within a major
3311 release; a major release occurs when the first or the second of the three
3312 version components of VirtualBox change (that is, in the x.y.z scheme, a
3313 major release is one where x or y change, but not when only z
3314 changes).</para>
3315
3316 <para>In other words, updates like those from 2.0.0 to 2.0.2 will not come
3317 with API breakages.</para>
3318
3319 <para>Migration between major releases most likely will lead to API
3320 breakage, so please make sure you updated code accordingly. The OOWS Java
3321 wrappers enforce that mechanism by putting VirtualBox classes into
3322 version-specific packages such as
3323 <computeroutput>org.virtualbox_2_2</computeroutput>. This approach allows
3324 for connecting to multiple VirtualBox versions simultaneously from the
3325 same Java application.</para>
3326
3327 <para>The following sections list incompatible changes that the Main API
3328 underwent since the original release of this SDK Reference with VirtualBox
3329 2.0. A change is deemed "incompatible" only if it breaks existing client
3330 code (e.g. changes in method parameter lists, renamed or removed
3331 interfaces and similar). In other words, the list does not contain new
3332 interfaces, methods or attributes or other changes that do not affect
3333 existing client code.</para>
3334
3335 <sect1>
3336 <title>Incompatible API changes with version 4.0</title>
3337
3338 <itemizedlist>
3339 <listitem>
3340 <para>The confusingly named and impractical session APIs were
3341 changed. In existing client code, the following changes need to be
3342 made:<itemizedlist>
3343 <listitem>
3344 <para>Replace any
3345 <computeroutput>IVirtualBox::openSession(uuidMachine,
3346 ...)</computeroutput> API call with the machine's <xref
3347 linkend="IMachine__lockMachine"
3348 xreflabel="IMachine::lockMachine()" /> call and a
3349 <computeroutput>LockType.Write</computeroutput> argument. The
3350 functionality is unchanged, but instead of "opening a direct
3351 session on a machine" all documentation now refers to
3352 "obtaining a write lock on a machine for the client
3353 session".</para>
3354 </listitem>
3355
3356 <listitem>
3357 <para>Similarly, replace any
3358 <computeroutput>IVirtualBox::openExistingSession(uuidMachine,
3359 ...)</computeroutput> call with the machine's <xref
3360 linkend="IMachine__lockMachine"
3361 xreflabel="IMachine::lockMachine()" /> call and a
3362 <computeroutput>LockType.Shared</computeroutput> argument.
3363 Whereas it was previously impossible to connect a client
3364 session to a running VM process in a race-free manner, the new
3365 API will atomically either write-lock the machine for the
3366 current session or establish a remote link to an existing
3367 session. Existing client code which tried calling both
3368 <computeroutput>openSession()</computeroutput> and
3369 <computeroutput>openExistingSession()</computeroutput> can now
3370 use this one call instead.</para>
3371 </listitem>
3372
3373 <listitem>
3374 <para>Third, replace any
3375 <computeroutput>IVirtualBox::openRemoteSession(uuidMachine,
3376 ...)</computeroutput> call with the machine's <xref
3377 linkend="IMachine__launchVMProcess"
3378 xreflabel="IMachine::launchVMProcess()" /> call. The
3379 functionality is unchanged.</para>
3380 </listitem>
3381
3382 <listitem>
3383 <para>The <xref linkend="SessionState"
3384 xreflabel="SessionState" /> enum was adjusted accordingly:
3385 "Open" is now "Locked", "Closed" is now "Unlocked", "Closing"
3386 is now "Unlocking".</para>
3387 </listitem>
3388 </itemizedlist></para>
3389 </listitem>
3390
3391 <listitem>
3392 <para>Virtual machines created with VirtualBox 4.0 or later no
3393 longer register their media in the global media registry in the
3394 <computeroutput>VirtualBox.xml</computeroutput> file. Instead, such
3395 machines list all their media in their own machine XML files. As a
3396 result, a number of media-related APIs had to be modified again.
3397 <itemizedlist>
3398 <listitem>
3399 <para>Neither <xref linkend="IVirtualBox__createHardDisk"
3400 xreflabel="IVirtualBox::createHardDisk()" /> nor <xref
3401 linkend="IVirtualBox__openMedium"
3402 xreflabel="IVirtualBox::openMedium()" /> register media
3403 automatically any more.</para>
3404 </listitem>
3405
3406 <listitem>
3407 <para><xref linkend="IMachine__attachDevice"
3408 xreflabel="IMachine::attachDevice()" /> and <xref
3409 linkend="IMachine__mountMedium"
3410 xreflabel="IMachine::mountMedium()" /> now take an IMedium
3411 object instead of a UUID as an argument. It is these two calls
3412 which add media to a registry now (either a machine registry
3413 for machines created with VirtualBox 4.0 or later or the
3414 global registry otherwise). As a consequence, if a medium is
3415 opened but never attached to a machine, it is no longer added
3416 to any registry any more.</para>
3417 </listitem>
3418
3419 <listitem>
3420 <para>To reduce code duplication, the APIs
3421 IVirtualBox::findHardDisk(), getHardDisk(), findDVDImage(),
3422 getDVDImage(), findFloppyImage() and getFloppyImage() have all
3423 been merged into <xref linkend="IVirtualBox__findMedium"
3424 xreflabel="IVirtualBox::findMedium()" />, and
3425 IVirtualBox::openHardDisk(), openDVDImage() and
3426 openFloppyImage() have all been merged into <xref
3427 linkend="IVirtualBox__openMedium"
3428 xreflabel="IVirtualBox::openMedium()" />.</para>
3429 </listitem>
3430
3431 <listitem>
3432 <para>The rare use case of changing the UUID and parent UUID
3433 of a medium previously handled by openHardDisk() is now in a
3434 separate <xref linkend="IMedium__setIDs"
3435 xreflabel="IMedium::setIDs" /> method.</para>
3436 </listitem>
3437 </itemizedlist></para>
3438 </listitem>
3439
3440 <listitem>
3441 <para>To reduce code duplication and for consistency with the
3442 aforementioned changes, IVirtualBox::getMachine() has been merged
3443 with <xref linkend="IVirtualBox__findMachine"
3444 xreflabel="IVirtualBox::findMachine()" />, and
3445 IMachine::getSnapshot() has been merged with <xref
3446 linkend="IMachine__findSnapshot"
3447 xreflabel="IMachine::findSnapshot()" />.</para>
3448 </listitem>
3449
3450 <listitem>
3451 <para>IVirtualBox::unregisterMachine() was replaced with <xref
3452 linkend="IMachine__unregister" xreflabel="IMachine::unregister()" />
3453 with additional functionality.</para>
3454 </listitem>
3455
3456 <listitem>
3457 <para><xref linkend="IVirtualBox__createMachine"
3458 xreflabel="IVirtualBox::createMachine()" /> is no longer restricted
3459 to creating machines in the default "Machines" folder, but can now
3460 create machines at arbitrary locations. For this to work, the
3461 parameter list had to be changed.</para>
3462 </listitem>
3463
3464 <listitem>
3465 <para>IConsole::forgetSavedState has been renamed to <xref
3466 linkend="IConsole__discardSavedState"
3467 xreflabel="IConsole::discardSavedState()" />.</para>
3468 </listitem>
3469
3470 <listitem>
3471 <para>All event callbacks APIs were replaced with a new, generic
3472 event mechanism that can be used both locally (COM, XPCOM) and
3473 remotely (web services). Also, the new mechanism is usable from
3474 scripting languages and a local Java. See <xref linkend="IEvent"
3475 xreflabel="events" /> for details. The new concept will require
3476 changes to all clients that used event callbacks.</para>
3477 </listitem>
3478
3479 <listitem>
3480 <para><xref linkend="IGuest__additionsVersion"
3481 xreflabel="IGuest::additionsVersion()" /> no longer returns the
3482 Guest Additions interface version but the installed Guest Additions
3483 version and revision in form of
3484 <computeroutput>3.3.0r12345</computeroutput>.</para>
3485 </listitem>
3486
3487 <listitem>
3488 <para>additionsActive() was replaced with <xref
3489 linkend="IGuest__additionsRunLevel"
3490 xreflabel="additionsRunLevel()" /> and <xref
3491 linkend="IGuest__getAdditionsStatus"
3492 xreflabel="getAdditionsStatus()" /> in order to support a more
3493 detailed status of the current Guest Additions loading/readiness
3494 state.</para>
3495 </listitem>
3496
3497 <listitem>
3498 <para>To address shared folders auto-mounting support, the following
3499 APIs were extended to require an additional
3500 <computeroutput>automount</computeroutput> parameter: <itemizedlist>
3501 <listitem>
3502 <para><xref linkend="IVirtualBox__createSharedFolder"
3503 xreflabel="IVirtualBox::createSharedFolder()" /></para>
3504 </listitem>
3505
3506 <listitem>
3507 <para><xref linkend="IMachine__createSharedFolder"
3508 xreflabel="IMachine::createSharedFolder()" /></para>
3509 </listitem>
3510
3511 <listitem>
3512 <para><xref linkend="IConsole__createSharedFolder"
3513 xreflabel="IConsole::createSharedFolder()" /></para>
3514 </listitem>
3515 </itemizedlist> Also, a new property named
3516 <computeroutput>autoMount</computeroutput> was added to the <xref
3517 linkend="ISharedFolder" xreflabel="ISharedFolder" />
3518 interface.</para>
3519 </listitem>
3520
3521 <listitem>
3522 <para><xref linkend="IMachine__export"
3523 xreflabel="IMachine::export()" /> received an extra parameter
3524 <computeroutput>location</computeroutput>, which is used to decide
3525 for the disk naming.</para>
3526 </listitem>
3527
3528 <listitem>
3529 <para><xref linkend="IAppliance__write"
3530 xreflabel="IAppliance::write()" /> received an extra parameter
3531 <computeroutput>manifest</computeroutput>, which can suppress
3532 creating the manifest file on export.</para>
3533 </listitem>
3534
3535 <listitem>
3536 <para><xref linkend="IVFSExplorer__entryList"
3537 xreflabel="IVFSExplorer::entryList()" /> received two extra
3538 parameters <computeroutput>sizes</computeroutput> and
3539 <computeroutput>modes</computeroutput>, which contains the sizes (in
3540 bytes) and the file access modes (in octal form) of the returned
3541 files.</para>
3542 </listitem>
3543
3544 <listitem>
3545 <para>The long-deprecated IVirtualBox::createLegacyMachine() API has
3546 been removed.</para>
3547 </listitem>
3548
3549 <listitem>
3550 <para>ISystemProperties::get/setDefaultHardDiskFolder() have been
3551 removed.</para>
3552 </listitem>
3553
3554 <listitem>
3555 <para>ISystemProperties::getMaxVDISize() is now <xref
3556 linkend="ISystemProperties__getMaxVDSize"
3557 xreflabel="ISystemProperties::getMaxVDSize()" /> and the returned
3558 unit has changed from megabytes to bytes.</para>
3559 </listitem>
3560
3561 <listitem>
3562 <para>A new Java glue layer replacing the previous OOWS JAX-WS
3563 bindings was introduced. The new library allows for uniform code
3564 targeting both local (COM/XPCOM) and remote (SOAP) transports. Now,
3565 instead of <computeroutput>IWebsessionManager</computeroutput>, the
3566 new class <computeroutput>VirtualBoxManager</computeroutput> must be
3567 used. See <xref linkend="javaapi" xreflabel="Java API chapter" />
3568 for details.</para>
3569 </listitem>
3570 </itemizedlist>
3571 </sect1>
3572
3573 <sect1>
3574 <title>Incompatible API changes with version 3.2</title>
3575
3576 <itemizedlist>
3577 <listitem>
3578 <para>The following interfaces were renamed for consistency:
3579 <itemizedlist>
3580 <listitem>
3581 <para>IMachine::getCpuProperty() is now <xref
3582 linkend="IMachine__getCPUProperty"
3583 xreflabel="IMachine::getCPUProperty()" />;</para>
3584 </listitem>
3585
3586 <listitem>
3587 <para>IMachine::setCpuProperty() is now <xref
3588 linkend="IMachine__setCPUProperty"
3589 xreflabel="IMachine::setCPUProperty()" />;</para>
3590 </listitem>
3591
3592 <listitem>
3593 <para>IMachine::getCpuIdLeaf() is now <xref
3594 linkend="IMachine__getCPUIDLeaf"
3595 xreflabel="IMachine::getCPUIDLeaf()" />;</para>
3596 </listitem>
3597
3598 <listitem>
3599 <para>IMachine::setCpuIdLeaf() is now <xref
3600 linkend="IMachine__setCPUIDLeaf"
3601 xreflabel="IMachine::setCPUIDLeaf()" />;</para>
3602 </listitem>
3603
3604 <listitem>
3605 <para>IMachine::removeCpuIdLeaf() is now <xref
3606 linkend="IMachine__removeCPUIDLeaf"
3607 xreflabel="IMachine::removeCPUIDLeaf()" />;</para>
3608 </listitem>
3609
3610 <listitem>
3611 <para>IMachine::removeAllCpuIdLeafs() is now <xref
3612 linkend="IMachine__removeAllCPUIDLeaves"
3613 xreflabel="IMachine::removeAllCPUIDLeaves()" />;</para>
3614 </listitem>
3615
3616 <listitem>
3617 <para>the CpuPropertyType enum is now <xref
3618 linkend="CPUPropertyType"
3619 xreflabel="CPUPropertyType" />.</para>
3620 </listitem>
3621
3622 <listitem>
3623 <para>IVirtualBoxCallback::onSnapshotDiscarded() is now
3624 IVirtualBoxCallback::onSnapshotDeleted.</para>
3625 </listitem>
3626 </itemizedlist></para>
3627 </listitem>
3628
3629 <listitem>
3630 <para>When creating a VM configuration with <xref
3631 linkend="IVirtualBox__createMachine"
3632 xreflabel="IVirtualBox::createMachine" />) it is now possible to
3633 ignore existing configuration files which would previously have
3634 caused a failure. For this the
3635 <computeroutput>override</computeroutput> parameter was
3636 added.</para>
3637 </listitem>
3638
3639 <listitem>
3640 <para>Deleting snapshots via <xref
3641 linkend="IConsole__deleteSnapshot"
3642 xreflabel="IConsole::deleteSnapshot()" /> is now possible while the
3643 associated VM is running in almost all cases. The API is unchanged,
3644 but client code that verifies machine states to determine whether
3645 snapshots can be deleted may need to be adjusted.</para>
3646 </listitem>
3647
3648 <listitem>
3649 <para>The IoBackendType enumeration was replaced with a boolean flag
3650 (see <xref linkend="IStorageController__useHostIOCache"
3651 xreflabel="IStorageController::useHostIOCache" />).</para>
3652 </listitem>
3653
3654 <listitem>
3655 <para>To address multi-monitor support, the following APIs were
3656 extended to require an additional
3657 <computeroutput>screenId</computeroutput> parameter: <itemizedlist>
3658 <listitem>
3659 <para><xref linkend="IMachine__querySavedThumbnailSize"
3660 xreflabel="IMachine::querySavedThumbnailSize()" /></para>
3661 </listitem>
3662
3663 <listitem>
3664 <para><xref linkend="IMachine__readSavedThumbnailToArray"
3665 xreflabel="IMachine::readSavedThumbnailToArray()" /></para>
3666 </listitem>
3667
3668 <listitem>
3669 <para><xref linkend="IMachine__querySavedScreenshotPNGSize"
3670 xreflabel="IMachine::querySavedScreenshotPNGSize()" /></para>
3671 </listitem>
3672
3673 <listitem>
3674 <para><xref linkend="IMachine__readSavedScreenshotPNGToArray"
3675 xreflabel="IMachine::readSavedScreenshotPNGToArray()" /></para>
3676 </listitem>
3677 </itemizedlist></para>
3678 </listitem>
3679
3680 <listitem>
3681 <para>The <computeroutput>shape</computeroutput> parameter of
3682 IConsoleCallback::onMousePointerShapeChange was changed from a
3683 implementation-specific pointer to a safearray, enabling scripting
3684 languages to process pointer shapes.</para>
3685 </listitem>
3686 </itemizedlist>
3687 </sect1>
3688
3689 <sect1>
3690 <title>Incompatible API changes with version 3.1</title>
3691
3692 <itemizedlist>
3693 <listitem>
3694 <para>Due to the new flexibility in medium attachments that was
3695 introduced with version 3.1 (in particular, full flexibility with
3696 attaching CD/DVD drives to arbitrary controllers), we seized the
3697 opportunity to rework all interfaces dealing with storage media to
3698 make the API more flexible as well as logical. The <xref
3699 linkend="IStorageController" xreflabel="IStorageController" />,
3700 <xref linkend="IMedium" xreflabel="IMedium" />, <xref
3701 linkend="IMediumAttachment" xreflabel="IMediumAttachment" /> and,
3702 <xref linkend="IMachine" xreflabel="IMachine" /> interfaces were
3703 affected the most. Existing code using them to configure storage and
3704 media needs to be carefully checked.</para>
3705
3706 <para>All media (hard disks, floppies and CDs/DVDs) are now
3707 uniformly handled through the <xref linkend="IMedium"
3708 xreflabel="IMedium" /> interface. The device-specific interfaces
3709 (<code>IHardDisk</code>, <code>IDVDImage</code>,
3710 <code>IHostDVDDrive</code>, <code>IFloppyImage</code> and
3711 <code>IHostFloppyDrive</code>) have been merged into IMedium; CD/DVD
3712 and floppy media no longer need special treatment. The device type
3713 of a medium determines in which context it can be used. Some
3714 functionality was moved to the other storage-related
3715 interfaces.</para>
3716
3717 <para><code>IMachine::attachHardDisk</code> and similar methods have
3718 been renamed and generalized to deal with any type of drive and
3719 medium. <xref linkend="IMachine__attachDevice"
3720 xreflabel="IMachine::attachDevice()" /> is the API method for adding
3721 any drive to a storage controller. The floppy and DVD/CD drives are
3722 no longer handled specially, and that means you can have more than
3723 one of them. As before, drives can only be changed while the VM is
3724 powered off. Mounting (or unmounting) removable media at runtime is
3725 possible with <xref linkend="IMachine__mountMedium"
3726 xreflabel="IMachine::mountMedium()" />.</para>
3727
3728 <para>Newly created virtual machines have no storage controllers
3729 associated with them. Even the IDE Controller needs to be created
3730 explicitly. The floppy controller is now visible as a separate
3731 controller, with a new storage bus type. For each storage bus type
3732 you can query the device types which can be attached, so that it is
3733 not necessary to hardcode any attachment rules.</para>
3734
3735 <para>This required matching changes e.g. in the callback interfaces
3736 (the medium specific change notification was replaced by a generic
3737 medium change notification) and removing associated enums (e.g.
3738 <code>DriveState</code>). In many places the incorrect use of the
3739 plural form "media" was replaced by "medium", to improve
3740 consistency.</para>
3741 </listitem>
3742
3743 <listitem>
3744 <para>Reading the <xref linkend="IMedium__state"
3745 xreflabel="IMedium::state" xrefstyle="" /> attribute no longer
3746 automatically performs an accessibility check; a new method <xref
3747 linkend="IMedium__refreshState"
3748 xreflabel="IMedium::refreshState()" /> does this. The attribute only
3749 returns the state any more.</para>
3750 </listitem>
3751
3752 <listitem>
3753 <para>There were substantial changes related to snapshots, triggered
3754 by the "branched snapshots" functionality introduced with version
3755 3.1. IConsole::discardSnapshot was renamed to <xref
3756 linkend="IConsole__deleteSnapshot"
3757 xreflabel="IConsole::deleteSnapshot()" />.
3758 IConsole::discardCurrentState and
3759 IConsole::discardCurrentSnapshotAndState were removed; corresponding
3760 new functionality is in <xref linkend="IConsole__restoreSnapshot"
3761 xreflabel="IConsole::restoreSnapshot()" />. Also, when <xref
3762 linkend="IConsole__takeSnapshot"
3763 xreflabel="IConsole::takeSnapshot()" /> is called on a running
3764 virtual machine, a live snapshot will be created. The old behavior
3765 was to temporarily pause the virtual machine while creating an
3766 online snapshot.</para>
3767 </listitem>
3768
3769 <listitem>
3770 <para>The <xref linkend="IVRDPServer" xreflabel="IVRDPServer" />,
3771 <xref linkend="IRemoteDisplayInfo" xreflabel="IRemoteDisplayInfo" />
3772 and IConsoleCallback interfaces were changed to reflect VRDP server
3773 ability to bind to one of available ports from a list of
3774 ports.</para>
3775
3776 <para>The <computeroutput>IVRDPServer::port</computeroutput>
3777 attribute has been replaced with <xref linkend="IVRDPServer__ports"
3778 xreflabel="IVRDPServer::ports" />, which is a comma-separated list
3779 of ports or ranges of ports.</para>
3780
3781 <para>An <xref linkend="IRemoteDisplayInfo__port"
3782 xreflabel="IRemoteDisplayInfo::port" /> attribute has been added for
3783 querying the actual port VRDP server listens on.</para>
3784
3785 <para>An IConsoleCallback::onRemoteDisplayInfoChange() notification
3786 callback has been added.</para>
3787 </listitem>
3788
3789 <listitem>
3790 <para>The parameter lists for the following functions were
3791 modified:<itemizedlist>
3792 <listitem>
3793 <para><xref linkend="IHost__removeHostOnlyNetworkInterface"
3794 xreflabel="IHost::removeHostOnlyNetworkInterface()" /></para>
3795 </listitem>
3796
3797 <listitem>
3798 <para><xref linkend="IHost__removeUSBDeviceFilter"
3799 xreflabel="IHost::removeUSBDeviceFilter()" /></para>
3800 </listitem>
3801 </itemizedlist></para>
3802 </listitem>
3803
3804 <listitem>
3805 <para>In the OOWS bindings for JAX-WS, the behavior of structures
3806 changed: for one, we implemented natural structures field access so
3807 you can just call a "get" method to obtain a field. Secondly,
3808 setters in structures were disabled as they have no expected effect
3809 and were at best misleading.</para>
3810 </listitem>
3811 </itemizedlist>
3812 </sect1>
3813
3814 <sect1>
3815 <title>Incompatible API changes with version 3.0</title>
3816
3817 <itemizedlist>
3818 <listitem>
3819 <para>In the object-oriented web service bindings for JAX-WS, proper
3820 inheritance has been introduced for some classes, so explicit
3821 casting is no longer needed to call methods from a parent class. In
3822 particular, IHardDisk and other classes now properly derive from
3823 <xref linkend="IMedium" xreflabel="IMedium" />.</para>
3824 </listitem>
3825
3826 <listitem>
3827 <para>All object identifiers (machines, snapshots, disks, etc)
3828 switched from GUIDs to strings (now still having string
3829 representation of GUIDs inside). As a result, no particular internal
3830 structure can be assumed for object identifiers; instead, they
3831 should be treated as opaque unique handles. This change mostly
3832 affects Java and C++ programs; for other languages, GUIDs are
3833 transparently converted to strings.</para>
3834 </listitem>
3835
3836 <listitem>
3837 <para>The uses of NULL strings have been changed greatly. All out
3838 parameters now use empty strings to signal a null value. For in
3839 parameters both the old NULL and empty string is allowed. This
3840 change was necessary to support more client bindings, especially
3841 using the webservice API. Many of them either have no special NULL
3842 value or have trouble dealing with it correctly in the respective
3843 library code.</para>
3844 </listitem>
3845
3846 <listitem>
3847 <para>Accidentally, the <code>TSBool</code> interface still appeared
3848 in 3.0.0, and was removed in 3.0.2. This is an SDK bug, do not use
3849 the SDK for VirtualBox 3.0.0 for developing clients.</para>
3850 </listitem>
3851
3852 <listitem>
3853 <para>The type of <xref linkend="IVirtualBoxErrorInfo__resultCode"
3854 xreflabel="IVirtualBoxErrorInfo::resultCode" /> changed from
3855 <computeroutput>result</computeroutput> to
3856 <computeroutput>long</computeroutput>.</para>
3857 </listitem>
3858
3859 <listitem>
3860 <para>The parameter list of IVirtualBox::openHardDisk was
3861 changed.</para>
3862 </listitem>
3863
3864 <listitem>
3865 <para>The method IConsole::discardSavedState was renamed to
3866 IConsole::forgetSavedState, and a parameter was added.</para>
3867 </listitem>
3868
3869 <listitem>
3870 <para>The method IConsole::powerDownAsync was renamed to <xref
3871 linkend="IConsole__powerDown" xreflabel="IConsole::powerDown" />,
3872 and the previous method with that name was deleted. So effectively a
3873 parameter was added.</para>
3874 </listitem>
3875
3876 <listitem>
3877 <para>In the <xref linkend="IFramebuffer"
3878 xreflabel="IFramebuffer" /> interface, the following were
3879 removed:<itemizedlist>
3880 <listitem>
3881 <para>the <computeroutput>operationSupported</computeroutput>
3882 attribute;</para>
3883
3884 <para>(as a result, the
3885 <computeroutput>FramebufferAccelerationOperation</computeroutput>
3886 enum was no longer needed and removed as well);</para>
3887 </listitem>
3888
3889 <listitem>
3890 <para>the <computeroutput>solidFill()</computeroutput>
3891 method;</para>
3892 </listitem>
3893
3894 <listitem>
3895 <para>the <computeroutput>copyScreenBits()</computeroutput>
3896 method.</para>
3897 </listitem>
3898 </itemizedlist></para>
3899 </listitem>
3900
3901 <listitem>
3902 <para>In the <xref linkend="IDisplay" xreflabel="IDisplay" />
3903 interface, the following were removed:<itemizedlist>
3904 <listitem>
3905 <para>the
3906 <computeroutput>setupInternalFramebuffer()</computeroutput>
3907 method;</para>
3908 </listitem>
3909
3910 <listitem>
3911 <para>the <computeroutput>lockFramebuffer()</computeroutput>
3912 method;</para>
3913 </listitem>
3914
3915 <listitem>
3916 <para>the <computeroutput>unlockFramebuffer()</computeroutput>
3917 method;</para>
3918 </listitem>
3919
3920 <listitem>
3921 <para>the
3922 <computeroutput>registerExternalFramebuffer()</computeroutput>
3923 method.</para>
3924 </listitem>
3925 </itemizedlist></para>
3926 </listitem>
3927 </itemizedlist>
3928 </sect1>
3929
3930 <sect1>
3931 <title>Incompatible API changes with version 2.2</title>
3932
3933 <itemizedlist>
3934 <listitem>
3935 <para>Added explicit version number into JAX-WS Java package names,
3936 such as <computeroutput>org.virtualbox_2_2</computeroutput>,
3937 allowing connect to multiple VirtualBox clients from single Java
3938 application.</para>
3939 </listitem>
3940
3941 <listitem>
3942 <para>The interfaces having a "2" suffix attached to them with
3943 version 2.1 were renamed again to have that suffix removed. This
3944 time around, this change involves only the name, there are no
3945 functional differences.</para>
3946
3947 <para>As a result, IDVDImage2 is now IDVDImage; IHardDisk2 is now
3948 IHardDisk; IHardDisk2Attachment is now IHardDiskAttachment.</para>
3949
3950 <para>Consequentially, all related methods and attributes that had a
3951 "2" suffix have been renamed; for example, IMachine::attachHardDisk2
3952 now becomes IMachine::attachHardDisk().</para>
3953 </listitem>
3954
3955 <listitem>
3956 <para>IVirtualBox::openHardDisk has an extra parameter for opening a
3957 disk read/write or read-only.</para>
3958 </listitem>
3959
3960 <listitem>
3961 <para>The remaining collections were replaced by more performant
3962 safe-arrays. This affects the following collections:</para>
3963
3964 <itemizedlist>
3965 <listitem>
3966 <para>IGuestOSTypeCollection</para>
3967 </listitem>
3968
3969 <listitem>
3970 <para>IHostDVDDriveCollection</para>
3971 </listitem>
3972
3973 <listitem>
3974 <para>IHostFloppyDriveCollection</para>
3975 </listitem>
3976
3977 <listitem>
3978 <para>IHostUSBDeviceCollection</para>
3979 </listitem>
3980
3981 <listitem>
3982 <para>IHostUSBDeviceFilterCollection</para>
3983 </listitem>
3984
3985 <listitem>
3986 <para>IProgressCollection</para>
3987 </listitem>
3988
3989 <listitem>
3990 <para>ISharedFolderCollection</para>
3991 </listitem>
3992
3993 <listitem>
3994 <para>ISnapshotCollection</para>
3995 </listitem>
3996
3997 <listitem>
3998 <para>IUSBDeviceCollection</para>
3999 </listitem>
4000
4001 <listitem>
4002 <para>IUSBDeviceFilterCollection</para>
4003 </listitem>
4004 </itemizedlist>
4005 </listitem>
4006
4007 <listitem>
4008 <para>Since "Host Interface Networking" was renamed to "bridged
4009 networking" and host-only networking was introduced, all associated
4010 interfaces needed renaming as well. In detail:</para>
4011
4012 <itemizedlist>
4013 <listitem>
4014 <para>The HostNetworkInterfaceType enum has been renamed to
4015 <xref linkend="HostNetworkInterfaceMediumType"
4016 xreflabel="HostNetworkInterfaceMediumType" /></para>
4017 </listitem>
4018
4019 <listitem>
4020 <para>The IHostNetworkInterface::type attribute has been renamed
4021 to <xref linkend="IHostNetworkInterface__mediumType"
4022 xreflabel="IHostNetworkInterface::mediumType" /></para>
4023 </listitem>
4024
4025 <listitem>
4026 <para>INetworkAdapter::attachToHostInterface() has been renamed
4027 to <xref linkend="INetworkAdapter__attachToBridgedInterface"
4028 xreflabel="INetworkAdapter::attachToBridgedInterface()" /></para>
4029 </listitem>
4030
4031 <listitem>
4032 <para>In the IHost interface, createHostNetworkInterface() has
4033 been renamed to <xref
4034 linkend="IHost__createHostOnlyNetworkInterface"
4035 xreflabel="createHostOnlyNetworkInterface()" /></para>
4036 </listitem>
4037
4038 <listitem>
4039 <para>Similarly, removeHostNetworkInterface() has been renamed
4040 to <xref linkend="IHost__removeHostOnlyNetworkInterface"
4041 xreflabel="removeHostOnlyNetworkInterface()" /></para>
4042 </listitem>
4043 </itemizedlist>
4044 </listitem>
4045 </itemizedlist>
4046 </sect1>
4047
4048 <sect1>
4049 <title>Incompatible API changes with version 2.1</title>
4050
4051 <itemizedlist>
4052 <listitem>
4053 <para>With VirtualBox 2.1, error codes were added to many error
4054 infos that give the caller a machine-readable (numeric) feedback in
4055 addition to the error string that has always been available. This is
4056 an ongoing process, and future versions of this SDK reference will
4057 document the error codes for each method call.</para>
4058 </listitem>
4059
4060 <listitem>
4061 <para>The hard disk and other media interfaces were completely
4062 redesigned. This was necessary to account for the support of VMDK,
4063 VHD and other image types; since backwards compatibility had to be
4064 broken anyway, we seized the moment to redesign the interfaces in a
4065 more logical way.</para>
4066
4067 <itemizedlist>
4068 <listitem>
4069 <para>Previously, the old IHardDisk interface had several
4070 derivatives called IVirtualDiskImage, IVMDKImage, IVHDImage,
4071 IISCSIHardDisk and ICustomHardDisk for the various disk formats
4072 supported by VirtualBox. The new IHardDisk2 interface that comes
4073 with version 2.1 now supports all hard disk image formats
4074 itself.</para>
4075 </listitem>
4076
4077 <listitem>
4078 <para>IHardDiskFormat is a new interface to describe the
4079 available back-ends for hard disk images (e.g. VDI, VMDK, VHD or
4080 iSCSI). The IHardDisk2::format attribute can be used to find out
4081 the back-end that is in use for a particular hard disk image.
4082 ISystemProperties::hardDiskFormats[] contains a list of all
4083 back-ends supported by the system. <xref
4084 linkend="ISystemProperties__defaultHardDiskFormat"
4085 xreflabel="ISystemProperties::defaultHardDiskFormat" /> contains
4086 the default system format.</para>
4087 </listitem>
4088
4089 <listitem>
4090 <para>In addition, the new <xref linkend="IMedium"
4091 xreflabel="IMedium" /> interface is a generic interface for hard
4092 disk, DVD and floppy images that contains the attributes and
4093 methods shared between them. It can be considered a parent class
4094 of the more specific interfaces for those images, which are now
4095 IHardDisk2, IDVDImage2 and IFloppyImage2.</para>
4096
4097 <para>In each case, the "2" versions of these interfaces replace
4098 the earlier versions that did not have the "2" suffix.
4099 Previously, the IDVDImage and IFloppyImage interfaces were
4100 entirely unrelated to IHardDisk.</para>
4101 </listitem>
4102
4103 <listitem>
4104 <para>As a result, all parts of the API that previously
4105 referenced IHardDisk, IDVDImage or IFloppyImage or any of the
4106 old subclasses are gone and will have replacements that use
4107 IHardDisk2, IDVDImage2 and IFloppyImage2; see, for example,
4108 IMachine::attachHardDisk2.</para>
4109 </listitem>
4110
4111 <listitem>
4112 <para>In particular, the IVirtualBox::hardDisks2 array replaces
4113 the earlier IVirtualBox::hardDisks collection.</para>
4114 </listitem>
4115 </itemizedlist>
4116 </listitem>
4117
4118 <listitem>
4119 <para><xref linkend="IGuestOSType" xreflabel="IGuestOSType" /> was
4120 extended to group operating systems into families and for 64-bit
4121 support.</para>
4122 </listitem>
4123
4124 <listitem>
4125 <para>The <xref linkend="IHostNetworkInterface"
4126 xreflabel="IHostNetworkInterface" /> interface was completely
4127 rewritten to account for the changes in how Host Interface
4128 Networking is now implemented in VirtualBox 2.1.</para>
4129 </listitem>
4130
4131 <listitem>
4132 <para>The IVirtualBox::machines2[] array replaces the former
4133 IVirtualBox::machines collection.</para>
4134 </listitem>
4135
4136 <listitem>
4137 <para>Added <xref linkend="IHost__getProcessorFeature"
4138 xreflabel="IHost::getProcessorFeature()" /> and <xref
4139 linkend="ProcessorFeature" xreflabel="ProcessorFeature" />
4140 enumeration.</para>
4141 </listitem>
4142
4143 <listitem>
4144 <para>The parameter list for <xref
4145 linkend="IVirtualBox__createMachine"
4146 xreflabel="IVirtualBox::createMachine()" /> was modified.</para>
4147 </listitem>
4148
4149 <listitem>
4150 <para>Added IMachine::pushGuestProperty.</para>
4151 </listitem>
4152
4153 <listitem>
4154 <para>New attributes in IMachine: <xref
4155 linkend="IMachine__accelerate3DEnabled"
4156 xreflabel="accelerate3DEnabled" />, HWVirtExVPIDEnabled, <xref
4157 linkend="IMachine__guestPropertyNotificationPatterns"
4158 xreflabel="guestPropertyNotificationPatterns" />, <xref
4159 linkend="IMachine__CPUCount" xreflabel="CPUCount" />.</para>
4160 </listitem>
4161
4162 <listitem>
4163 <para>Added <xref linkend="IConsole__powerUpPaused"
4164 xreflabel="IConsole::powerUpPaused()" /> and <xref
4165 linkend="IConsole__getGuestEnteredACPIMode"
4166 xreflabel="IConsole::getGuestEnteredACPIMode()" />.</para>
4167 </listitem>
4168
4169 <listitem>
4170 <para>Removed ResourceUsage enumeration.</para>
4171 </listitem>
4172 </itemizedlist>
4173 </sect1>
4174 </chapter>
4175</book>
4176<!-- vim: set shiftwidth=2 tabstop=2 expandtab: -->
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette