[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] Support for virtualbox version 3.1



Hi All,

I have just added support for VirtualBox Version 3.1 (works for the beta as 
well) in the libvirt vbox driver.

The patch for the same is attached below.

Regards,
Pritesh
>From 944b70c8078f33d864e69bcf73cc003107244728 Mon Sep 17 00:00:00 2001
From: Pritesh Kothari <Pritesh Kothari Sun com>
Date: Tue, 17 Nov 2009 12:03:31 +0100
Subject: [PATCH] Support for virtualbox version 3.1

---
 src/Makefile.am           |    3 +-
 src/vbox/vbox_CAPI_v3_1.h | 5273 ++++++++++++++++++++++++++++++
 src/vbox/vbox_V3_1.c      |   37 +
 src/vbox/vbox_driver.c    |    8 +
 src/vbox/vbox_tmpl.c      | 7771 ++++++++++++++++++++++++---------------------
 5 files changed, 9457 insertions(+), 3635 deletions(-)
 create mode 100644 src/vbox/vbox_CAPI_v3_1.h
 create mode 100644 src/vbox/vbox_V3_1.c

diff --git a/src/Makefile.am b/src/Makefile.am
index d22a103..340501e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -177,7 +177,8 @@ VBOX_DRIVER_SOURCES =						\
         vbox/vbox_XPCOMCGlue.c vbox/vbox_XPCOMCGlue.h		\
         vbox/vbox_driver.c vbox/vbox_driver.h			\
         vbox/vbox_V2_2.c vbox/vbox_CAPI_v2_2.h			\
-        vbox/vbox_V3_0.c vbox/vbox_CAPI_v3_0.h
+        vbox/vbox_V3_0.c vbox/vbox_CAPI_v3_0.h			\
+        vbox/vbox_V3_1.c vbox/vbox_CAPI_v3_1.h
 
 VBOX_DRIVER_EXTRA_DIST = vbox/vbox_tmpl.c vbox/README
 
diff --git a/src/vbox/vbox_CAPI_v3_1.h b/src/vbox/vbox_CAPI_v3_1.h
new file mode 100644
index 0000000..4222d89
--- /dev/null
+++ b/src/vbox/vbox_CAPI_v3_1.h
@@ -0,0 +1,5273 @@
+
+/*
+ *  DO NOT EDIT! This is a generated file.
+ *
+ *  XPCOM IDL (XPIDL) definition for VirtualBox Main API (COM interfaces)
+ *  generated from XIDL (XML interface definition).
+ *
+ *  Source    : src/VBox/Main/idl/VirtualBox.xidl
+ *  Generator : src/VBox/Main/idl/xpcidl.xsl
+ *
+ *  This file contains portions from the following Mozilla XPCOM files:
+ *      xpcom/include/xpcom/nsID.h
+ *      xpcom/include/nsIException.h
+ *      xpcom/include/nsprpub/prtypes.h
+ *      xpcom/include/xpcom/nsISupportsBase.h
+ *
+ * These files were originally triple-licensed (MPL/GPL2/LGPL2.1). Sun
+ * elects to distribute this derived work under the LGPL2.1 only.
+ */
+
+/*
+ * Copyright (C) 2008-2009 Sun Microsystems, Inc.
+ *
+ * This file is part of a free software library; you can redistribute
+ * it and/or modify it under the terms of the GNU Lesser General
+ * Public License version 2.1 as published by the Free Software
+ * Foundation and shipped in the "COPYING" file with this library.
+ * The library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY of any kind.
+ *
+ * Sun LGPL Disclaimer: For the avoidance of doubt, except that if
+ * any license choice other than GPL or LGPL is available it will
+ * apply instead, Sun elects to use only the Lesser General Public
+ * License version 2.1 (LGPLv2) at this time for any software where
+ * a choice of LGPL license versions is made available with the
+ * language indicating that LGPLv2 or any later version may be used,
+ * or where a choice of which version of the LGPL is applied is
+ * otherwise unspecified.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
+ * Clara, CA 95054 USA or visit http://www.sun.com if you need
+ * additional information or have any questions.
+ */
+
+#ifndef ___VirtualBox_CXPCOM_h
+#define ___VirtualBox_CXPCOM_h
+
+#ifdef __cplusplus
+# include "VirtualBox_XPCOM.h"
+#else /* !__cplusplus */
+
+#include <stddef.h>
+#include "wchar.h"
+
+#if defined(WIN32)
+
+#define PR_EXPORT(__type) extern __declspec(dllexport) __type
+#define PR_EXPORT_DATA(__type) extern __declspec(dllexport) __type
+#define PR_IMPORT(__type) __declspec(dllimport) __type
+#define PR_IMPORT_DATA(__type) __declspec(dllimport) __type
+
+#define PR_EXTERN(__type) extern __declspec(dllexport) __type
+#define PR_IMPLEMENT(__type) __declspec(dllexport) __type
+#define PR_EXTERN_DATA(__type) extern __declspec(dllexport) __type
+#define PR_IMPLEMENT_DATA(__type) __declspec(dllexport) __type
+
+#define PR_CALLBACK
+#define PR_CALLBACK_DECL
+#define PR_STATIC_CALLBACK(__x) static __x
+
+#elif defined(XP_BEOS)
+
+#define PR_EXPORT(__type) extern __declspec(dllexport) __type
+#define PR_EXPORT_DATA(__type) extern __declspec(dllexport) __type
+#define PR_IMPORT(__type) extern __declspec(dllexport) __type
+#define PR_IMPORT_DATA(__type) extern __declspec(dllexport) __type
+
+#define PR_EXTERN(__type) extern __declspec(dllexport) __type
+#define PR_IMPLEMENT(__type) __declspec(dllexport) __type
+#define PR_EXTERN_DATA(__type) extern __declspec(dllexport) __type
+#define PR_IMPLEMENT_DATA(__type) __declspec(dllexport) __type
+
+#define PR_CALLBACK
+#define PR_CALLBACK_DECL
+#define PR_STATIC_CALLBACK(__x) static __x
+
+#elif defined(WIN16)
+
+#define PR_CALLBACK_DECL        __cdecl
+
+#if defined(_WINDLL)
+#define PR_EXPORT(__type) extern __type _cdecl _export _loadds
+#define PR_IMPORT(__type) extern __type _cdecl _export _loadds
+#define PR_EXPORT_DATA(__type) extern __type _export
+#define PR_IMPORT_DATA(__type) extern __type _export
+
+#define PR_EXTERN(__type) extern __type _cdecl _export _loadds
+#define PR_IMPLEMENT(__type) __type _cdecl _export _loadds
+#define PR_EXTERN_DATA(__type) extern __type _export
+#define PR_IMPLEMENT_DATA(__type) __type _export
+
+#define PR_CALLBACK             __cdecl __loadds
+#define PR_STATIC_CALLBACK(__x) static __x PR_CALLBACK
+
+#else /* this must be .EXE */
+#define PR_EXPORT(__type) extern __type _cdecl _export
+#define PR_IMPORT(__type) extern __type _cdecl _export
+#define PR_EXPORT_DATA(__type) extern __type _export
+#define PR_IMPORT_DATA(__type) extern __type _export
+
+#define PR_EXTERN(__type) extern __type _cdecl _export
+#define PR_IMPLEMENT(__type) __type _cdecl _export
+#define PR_EXTERN_DATA(__type) extern __type _export
+#define PR_IMPLEMENT_DATA(__type) __type _export
+
+#define PR_CALLBACK             __cdecl __loadds
+#define PR_STATIC_CALLBACK(__x) __x PR_CALLBACK
+#endif /* _WINDLL */
+
+#elif defined(XP_MAC)
+
+#define PR_EXPORT(__type) extern __declspec(export) __type
+#define PR_EXPORT_DATA(__type) extern __declspec(export) __type
+#define PR_IMPORT(__type) extern __declspec(export) __type
+#define PR_IMPORT_DATA(__type) extern __declspec(export) __type
+
+#define PR_EXTERN(__type) extern __declspec(export) __type
+#define PR_IMPLEMENT(__type) __declspec(export) __type
+#define PR_EXTERN_DATA(__type) extern __declspec(export) __type
+#define PR_IMPLEMENT_DATA(__type) __declspec(export) __type
+
+#define PR_CALLBACK
+#define PR_CALLBACK_DECL
+#define PR_STATIC_CALLBACK(__x) static __x
+
+#elif defined(XP_OS2) && defined(__declspec)
+
+#define PR_EXPORT(__type) extern __declspec(dllexport) __type
+#define PR_EXPORT_DATA(__type) extern __declspec(dllexport) __type
+#define PR_IMPORT(__type) __declspec(dllimport) __type
+#define PR_IMPORT_DATA(__type) __declspec(dllimport) __type
+
+#define PR_EXTERN(__type) extern __declspec(dllexport) __type
+#define PR_IMPLEMENT(__type) __declspec(dllexport) __type
+#define PR_EXTERN_DATA(__type) extern __declspec(dllexport) __type
+#define PR_IMPLEMENT_DATA(__type) __declspec(dllexport) __type
+
+#define PR_CALLBACK
+#define PR_CALLBACK_DECL
+#define PR_STATIC_CALLBACK(__x) static __x
+
+#elif defined(XP_OS2_VACPP)
+
+#define PR_EXPORT(__type) extern __type
+#define PR_EXPORT_DATA(__type) extern __type
+#define PR_IMPORT(__type) extern __type
+#define PR_IMPORT_DATA(__type) extern __type
+
+#define PR_EXTERN(__type) extern __type
+#define PR_IMPLEMENT(__type) __type
+#define PR_EXTERN_DATA(__type) extern __type
+#define PR_IMPLEMENT_DATA(__type) __type
+#define PR_CALLBACK _Optlink
+#define PR_CALLBACK_DECL
+#define PR_STATIC_CALLBACK(__x) static __x PR_CALLBACK
+
+#else /* Unix */
+
+# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
+#  define PR_EXPORT(__type) __attribute__((visibility("default"))) extern __type
+#  define PR_EXPORT_DATA(__type) __attribute__((visibility("default"))) extern __type
+#  define PR_IMPORT(__type) extern __type
+#  define PR_IMPORT_DATA(__type) extern __type
+#  define PR_EXTERN(__type) __attribute__((visibility("default"))) extern __type
+#  define PR_IMPLEMENT(__type) __attribute__((visibility("default"))) __type
+#  define PR_EXTERN_DATA(__type) __attribute__((visibility("default"))) extern __type
+#  define PR_IMPLEMENT_DATA(__type) __attribute__((visibility("default"))) __type
+#  define PR_CALLBACK
+#  define PR_CALLBACK_DECL
+#  define PR_STATIC_CALLBACK(__x) static __x
+# else
+#  define PR_EXPORT(__type) extern __type
+#  define PR_EXPORT_DATA(__type) extern __type
+#  define PR_IMPORT(__type) extern __type
+#  define PR_IMPORT_DATA(__type) extern __type
+#  define PR_EXTERN(__type) extern __type
+#  define PR_IMPLEMENT(__type) __type
+#  define PR_EXTERN_DATA(__type) extern __type
+#  define PR_IMPLEMENT_DATA(__type) __type
+#  define PR_CALLBACK
+#  define PR_CALLBACK_DECL
+#  define PR_STATIC_CALLBACK(__x) static __x
+# endif
+#endif
+
+#if defined(_NSPR_BUILD_)
+#define NSPR_API(__type) PR_EXPORT(__type)
+#define NSPR_DATA_API(__type) PR_EXPORT_DATA(__type)
+#else
+#define NSPR_API(__type) PR_IMPORT(__type)
+#define NSPR_DATA_API(__type) PR_IMPORT_DATA(__type)
+#endif
+
+typedef unsigned char PRUint8;
+#if (defined(HPUX) && defined(__cplusplus) \
+        && !defined(__GNUC__) && __cplusplus < 199707L) \
+    || (defined(SCO) && defined(__cplusplus) \
+        && !defined(__GNUC__) && __cplusplus == 1L)
+typedef char PRInt8;
+#else
+typedef signed char PRInt8;
+#endif
+
+#define PR_INT8_MAX 127
+#define PR_INT8_MIN (-128)
+#define PR_UINT8_MAX 255U
+
+typedef unsigned short PRUint16;
+typedef short PRInt16;
+
+#define PR_INT16_MAX 32767
+#define PR_INT16_MIN (-32768)
+#define PR_UINT16_MAX 65535U
+
+typedef unsigned int PRUint32;
+typedef int PRInt32;
+#define PR_INT32(x)  x
+#define PR_UINT32(x) x ## U
+
+#define PR_INT32_MAX PR_INT32(2147483647)
+#define PR_INT32_MIN (-PR_INT32_MAX - 1)
+#define PR_UINT32_MAX PR_UINT32(4294967295)
+
+typedef long PRInt64;
+typedef unsigned long PRUint64;
+typedef int PRIntn;
+typedef unsigned int PRUintn;
+
+typedef double          PRFloat64;
+typedef size_t PRSize;
+
+typedef ptrdiff_t PRPtrdiff;
+
+typedef unsigned long PRUptrdiff;
+
+typedef PRIntn PRBool;
+
+#define PR_TRUE 1
+#define PR_FALSE 0
+
+typedef PRUint8 PRPackedBool;
+
+/*
+** Status code used by some routines that have a single point of failure or
+** special status return.
+*/
+typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
+
+#ifndef __PRUNICHAR__
+#define __PRUNICHAR__
+#if defined(WIN32) || defined(XP_MAC)
+typedef wchar_t PRUnichar;
+#else
+typedef PRUint16 PRUnichar;
+#endif
+#endif
+
+typedef long PRWord;
+typedef unsigned long PRUword;
+
+#define nsnull 0
+typedef PRUint32 nsresult;
+
+#if defined(__GNUC__) && (__GNUC__ > 2)
+#define NS_LIKELY(x)    (__builtin_expect((x), 1))
+#define NS_UNLIKELY(x)  (__builtin_expect((x), 0))
+#else
+#define NS_LIKELY(x)    (x)
+#define NS_UNLIKELY(x)  (x)
+#endif
+
+#define NS_FAILED(_nsresult) (NS_UNLIKELY((_nsresult) & 0x80000000))
+#define NS_SUCCEEDED(_nsresult) (NS_LIKELY(!((_nsresult) & 0x80000000)))
+
+#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
+# define PR_IntervalNow VBoxNsprPR_IntervalNow
+# define PR_TicksPerSecond VBoxNsprPR_TicksPerSecond
+# define PR_SecondsToInterval VBoxNsprPR_SecondsToInterval
+# define PR_MillisecondsToInterval VBoxNsprPR_MillisecondsToInterval
+# define PR_MicrosecondsToInterval VBoxNsprPR_MicrosecondsToInterval
+# define PR_IntervalToSeconds VBoxNsprPR_IntervalToSeconds
+# define PR_IntervalToMilliseconds VBoxNsprPR_IntervalToMilliseconds
+# define PR_IntervalToMicroseconds VBoxNsprPR_IntervalToMicroseconds
+# define PR_EnterMonitor VBoxNsprPR_EnterMonitor
+# define PR_ExitMonitor VBoxNsprPR_ExitMonitor
+# define PR_Notify VBoxNsprPR_Notify
+# define PR_NotifyAll VBoxNsprPR_NotifyAll
+# define PR_Wait VBoxNsprPR_Wait
+# define PR_NewMonitor VBoxNsprPR_NewMonitor
+# define PR_DestroyMonitor VBoxNsprPR_DestroyMonitor
+#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
+
+typedef PRUint32 PRIntervalTime;
+
+#define PR_INTERVAL_MIN 1000UL
+#define PR_INTERVAL_MAX 100000UL
+#define PR_INTERVAL_NO_WAIT 0UL
+#define PR_INTERVAL_NO_TIMEOUT 0xffffffffUL
+
+NSPR_API(PRIntervalTime) PR_IntervalNow(void);
+NSPR_API(PRUint32) PR_TicksPerSecond(void);
+NSPR_API(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds);
+NSPR_API(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli);
+NSPR_API(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro);
+NSPR_API(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks);
+NSPR_API(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks);
+NSPR_API(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks);
+
+typedef struct PRMonitor PRMonitor;
+
+NSPR_API(PRMonitor*) PR_NewMonitor(void);
+NSPR_API(void) PR_DestroyMonitor(PRMonitor *mon);
+NSPR_API(void) PR_EnterMonitor(PRMonitor *mon);
+NSPR_API(PRStatus) PR_ExitMonitor(PRMonitor *mon);
+NSPR_API(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks);
+NSPR_API(PRStatus) PR_Notify(PRMonitor *mon);
+NSPR_API(PRStatus) PR_NotifyAll(PRMonitor *mon);
+
+#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
+# define PR_CreateThread VBoxNsprPR_CreateThread
+# define PR_JoinThread VBoxNsprPR_JoinThread
+# define PR_Sleep VBoxNsprPR_Sleep
+# define PR_GetCurrentThread VBoxNsprPR_GetCurrentThread
+# define PR_GetThreadState VBoxNsprPR_GetThreadState
+# define PR_SetThreadPrivate VBoxNsprPR_SetThreadPrivate
+# define PR_GetThreadPrivate VBoxNsprPR_GetThreadPrivate
+# define PR_NewThreadPrivateIndex VBoxNsprPR_NewThreadPrivateIndex
+# define PR_GetThreadPriority VBoxNsprPR_GetThreadPriority
+# define PR_SetThreadPriority VBoxNsprPR_SetThreadPriority
+# define PR_Interrupt VBoxNsprPR_Interrupt
+# define PR_ClearInterrupt VBoxNsprPR_ClearInterrupt
+# define PR_BlockInterrupt VBoxNsprPR_BlockInterrupt
+# define PR_UnblockInterrupt VBoxNsprPR_UnblockInterrupt
+# define PR_GetThreadScope VBoxNsprPR_GetThreadScope
+# define PR_GetThreadType VBoxNsprPR_GetThreadType
+#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
+
+typedef struct PRThread PRThread;
+typedef struct PRThreadStack PRThreadStack;
+
+typedef enum PRThreadType {
+    PR_USER_THREAD,
+    PR_SYSTEM_THREAD
+} PRThreadType;
+
+typedef enum PRThreadScope {
+    PR_LOCAL_THREAD,
+    PR_GLOBAL_THREAD,
+    PR_GLOBAL_BOUND_THREAD
+} PRThreadScope;
+
+typedef enum PRThreadState {
+    PR_JOINABLE_THREAD,
+    PR_UNJOINABLE_THREAD
+} PRThreadState;
+
+typedef enum PRThreadPriority
+{
+    PR_PRIORITY_FIRST = 0,      /* just a placeholder */
+    PR_PRIORITY_LOW = 0,        /* the lowest possible priority */
+    PR_PRIORITY_NORMAL = 1,     /* most common expected priority */
+    PR_PRIORITY_HIGH = 2,       /* slightly more aggressive scheduling */
+    PR_PRIORITY_URGENT = 3,     /* it does little good to have more than one */
+    PR_PRIORITY_LAST = 3        /* this is just a placeholder */
+} PRThreadPriority;
+
+NSPR_API(PRThread*) PR_CreateThread(PRThreadType type,
+                     void (PR_CALLBACK *start)(void *arg),
+                     void *arg,
+                     PRThreadPriority priority,
+                     PRThreadScope scope,
+                     PRThreadState state,
+                     PRUint32 stackSize);
+NSPR_API(PRStatus) PR_JoinThread(PRThread *thread);
+NSPR_API(PRThread*) PR_GetCurrentThread(void);
+#ifndef NO_NSPR_10_SUPPORT
+#define PR_CurrentThread() PR_GetCurrentThread() /* for nspr1.0 compat. */
+#endif /* NO_NSPR_10_SUPPORT */
+NSPR_API(PRThreadPriority) PR_GetThreadPriority(const PRThread *thread);
+NSPR_API(void) PR_SetThreadPriority(PRThread *thread, PRThreadPriority priority);
+
+typedef void (PR_CALLBACK *PRThreadPrivateDTOR)(void *priv);
+
+NSPR_API(PRStatus) PR_NewThreadPrivateIndex(
+    PRUintn *newIndex, PRThreadPrivateDTOR destructor);
+NSPR_API(PRStatus) PR_SetThreadPrivate(PRUintn tpdIndex, void *priv);
+NSPR_API(void*) PR_GetThreadPrivate(PRUintn tpdIndex);
+NSPR_API(PRStatus) PR_Interrupt(PRThread *thread);
+NSPR_API(void) PR_ClearInterrupt(void);
+NSPR_API(void) PR_BlockInterrupt(void);
+NSPR_API(void) PR_UnblockInterrupt(void);
+NSPR_API(PRStatus) PR_Sleep(PRIntervalTime ticks);
+NSPR_API(PRThreadScope) PR_GetThreadScope(const PRThread *thread);
+NSPR_API(PRThreadType) PR_GetThreadType(const PRThread *thread);
+NSPR_API(PRThreadState) PR_GetThreadState(const PRThread *thread);
+
+#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
+# define PR_DestroyLock VBoxNsprPR_DestroyLock
+# define PR_Lock VBoxNsprPR_Lock
+# define PR_NewLock VBoxNsprPR_NewLock
+# define PR_Unlock VBoxNsprPR_Unlock
+#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
+
+typedef struct PRLock PRLock;
+
+NSPR_API(PRLock*) PR_NewLock(void);
+NSPR_API(void) PR_DestroyLock(PRLock *lock);
+NSPR_API(void) PR_Lock(PRLock *lock);
+NSPR_API(PRStatus) PR_Unlock(PRLock *lock);
+
+#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
+# define PR_NewCondVar VBoxNsprPR_NewCondVar
+# define PR_DestroyCondVar VBoxNsprPR_DestroyCondVar
+# define PR_WaitCondVar VBoxNsprPR_WaitCondVar
+# define PR_NotifyCondVar VBoxNsprPR_NotifyCondVar
+# define PR_NotifyAllCondVar VBoxNsprPR_NotifyAllCondVar
+#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
+
+typedef struct PRCondVar PRCondVar;
+
+NSPR_API(PRCondVar*) PR_NewCondVar(PRLock *lock);
+NSPR_API(void) PR_DestroyCondVar(PRCondVar *cvar);
+NSPR_API(PRStatus) PR_WaitCondVar(PRCondVar *cvar, PRIntervalTime timeout);
+NSPR_API(PRStatus) PR_NotifyCondVar(PRCondVar *cvar);
+NSPR_API(PRStatus) PR_NotifyAllCondVar(PRCondVar *cvar);
+
+typedef struct PRCListStr PRCList;
+
+struct PRCListStr {
+    PRCList *next;
+    PRCList *prev;
+};
+
+#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
+# define PL_DestroyEvent VBoxNsplPL_DestroyEvent
+# define PL_HandleEvent VBoxNsplPL_HandleEvent
+# define PL_InitEvent VBoxNsplPL_InitEvent
+# define PL_CreateEventQueue VBoxNsplPL_CreateEventQueue
+# define PL_CreateMonitoredEventQueue VBoxNsplPL_CreateMonitoredEventQueue
+# define PL_CreateNativeEventQueue VBoxNsplPL_CreateNativeEventQueue
+# define PL_DequeueEvent VBoxNsplPL_DequeueEvent
+# define PL_DestroyEventQueue VBoxNsplPL_DestroyEventQueue
+# define PL_EventAvailable VBoxNsplPL_EventAvailable
+# define PL_EventLoop VBoxNsplPL_EventLoop
+# define PL_GetEvent VBoxNsplPL_GetEvent
+# define PL_GetEventOwner VBoxNsplPL_GetEventOwner
+# define PL_GetEventQueueMonitor VBoxNsplPL_GetEventQueueMonitor
+# define PL_GetEventQueueSelectFD VBoxNsplPL_GetEventQueueSelectFD
+# define PL_MapEvents VBoxNsplPL_MapEvents
+# define PL_PostEvent VBoxNsplPL_PostEvent
+# define PL_PostSynchronousEvent VBoxNsplPL_PostSynchronousEvent
+# define PL_ProcessEventsBeforeID VBoxNsplPL_ProcessEventsBeforeID
+# define PL_ProcessPendingEvents VBoxNsplPL_ProcessPendingEvents
+# define PL_RegisterEventIDFunc VBoxNsplPL_RegisterEventIDFunc
+# define PL_RevokeEvents VBoxNsplPL_RevokeEvents
+# define PL_UnregisterEventIDFunc VBoxNsplPL_UnregisterEventIDFunc
+# define PL_WaitForEvent VBoxNsplPL_WaitForEvent
+# define PL_IsQueueNative VBoxNsplPL_IsQueueNative
+# define PL_IsQueueOnCurrentThread VBoxNsplPL_IsQueueOnCurrentThread
+# define PL_FavorPerformanceHint VBoxNsplPL_FavorPerformanceHint
+#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
+
+typedef struct PLEvent PLEvent;
+typedef struct PLEventQueue PLEventQueue;
+
+PR_EXTERN(PLEventQueue*)
+PL_CreateEventQueue(const char* name, PRThread* handlerThread);
+PR_EXTERN(PLEventQueue *)
+    PL_CreateNativeEventQueue(
+        const char *name,
+        PRThread *handlerThread
+    );
+PR_EXTERN(PLEventQueue *)
+    PL_CreateMonitoredEventQueue(
+        const char *name,
+        PRThread *handlerThread
+    );
+PR_EXTERN(void)
+PL_DestroyEventQueue(PLEventQueue* self);
+PR_EXTERN(PRMonitor*)
+PL_GetEventQueueMonitor(PLEventQueue* self);
+
+#define PL_ENTER_EVENT_QUEUE_MONITOR(queue) \
+    PR_EnterMonitor(PL_GetEventQueueMonitor(queue))
+
+#define PL_EXIT_EVENT_QUEUE_MONITOR(queue)  \
+    PR_ExitMonitor(PL_GetEventQueueMonitor(queue))
+
+PR_EXTERN(PRStatus) PL_PostEvent(PLEventQueue* self, PLEvent* event);
+PR_EXTERN(void*) PL_PostSynchronousEvent(PLEventQueue* self, PLEvent* event);
+PR_EXTERN(PLEvent*) PL_GetEvent(PLEventQueue* self);
+PR_EXTERN(PRBool) PL_EventAvailable(PLEventQueue* self);
+
+typedef void (PR_CALLBACK *PLEventFunProc)(PLEvent* event, void* data, PLEventQueue* queue);
+
+PR_EXTERN(void) PL_MapEvents(PLEventQueue* self, PLEventFunProc fun, void* data);
+PR_EXTERN(void) PL_RevokeEvents(PLEventQueue* self, void* owner);
+PR_EXTERN(void) PL_ProcessPendingEvents(PLEventQueue* self);
+PR_EXTERN(PLEvent*) PL_WaitForEvent(PLEventQueue* self);
+PR_EXTERN(void) PL_EventLoop(PLEventQueue* self);
+PR_EXTERN(PRInt32) PL_GetEventQueueSelectFD(PLEventQueue* self);
+PR_EXTERN(PRBool) PL_IsQueueOnCurrentThread( PLEventQueue *queue );
+PR_EXTERN(PRBool) PL_IsQueueNative(PLEventQueue *queue);
+
+typedef void* (PR_CALLBACK *PLHandleEventProc)(PLEvent* self);
+typedef void (PR_CALLBACK *PLDestroyEventProc)(PLEvent* self);
+PR_EXTERN(void)
+PL_InitEvent(PLEvent* self, void* owner,
+             PLHandleEventProc handler,
+             PLDestroyEventProc destructor);
+PR_EXTERN(void*) PL_GetEventOwner(PLEvent* self);
+PR_EXTERN(void) PL_HandleEvent(PLEvent* self);
+PR_EXTERN(void) PL_DestroyEvent(PLEvent* self);
+PR_EXTERN(void) PL_DequeueEvent(PLEvent* self, PLEventQueue* queue);
+PR_EXTERN(void) PL_FavorPerformanceHint(PRBool favorPerformanceOverEventStarvation, PRUint32 starvationDelay);
+
+struct PLEvent {
+    PRCList             link;
+    PLHandleEventProc   handler;
+    PLDestroyEventProc  destructor;
+    void*               owner;
+    void*               synchronousResult;
+    PRLock*             lock;
+    PRCondVar*          condVar;
+    PRBool              handled;
+#ifdef PL_POST_TIMINGS
+    PRIntervalTime      postTime;
+#endif
+#ifdef XP_UNIX
+    unsigned long       id;
+#endif /* XP_UNIX */
+    /* other fields follow... */
+};
+
+#if defined(XP_WIN) || defined(XP_OS2)
+
+PR_EXTERN(HWND)
+    PL_GetNativeEventReceiverWindow(
+        PLEventQueue *eqp
+    );
+#endif /* XP_WIN || XP_OS2 */
+
+#ifdef XP_UNIX
+
+PR_EXTERN(PRInt32)
+PL_ProcessEventsBeforeID(PLEventQueue *aSelf, unsigned long aID);
+
+typedef unsigned long (PR_CALLBACK *PLGetEventIDFunc)(void *aClosure);
+
+PR_EXTERN(void)
+PL_RegisterEventIDFunc(PLEventQueue *aSelf, PLGetEventIDFunc aFunc,
+                       void *aClosure);
+PR_EXTERN(void) PL_UnregisterEventIDFunc(PLEventQueue *aSelf);
+
+#endif /* XP_UNIX */
+
+/* Standard "it worked" return value */
+#define NS_OK                              0
+
+#define NS_ERROR_BASE                      ((nsresult) 0xC1F30000)
+
+/* Returned when an instance is not initialized */
+#define NS_ERROR_NOT_INITIALIZED           (NS_ERROR_BASE + 1)
+
+/* Returned when an instance is already initialized */
+#define NS_ERROR_ALREADY_INITIALIZED       (NS_ERROR_BASE + 2)
+
+/* Returned by a not implemented function */
+#define NS_ERROR_NOT_IMPLEMENTED           ((nsresult) 0x80004001L)
+
+/* Returned when a given interface is not supported. */
+#define NS_NOINTERFACE                     ((nsresult) 0x80004002L)
+#define NS_ERROR_NO_INTERFACE              NS_NOINTERFACE
+
+#define NS_ERROR_INVALID_POINTER           ((nsresult) 0x80004003L)
+#define NS_ERROR_NULL_POINTER              NS_ERROR_INVALID_POINTER
+
+/* Returned when a function aborts */
+#define NS_ERROR_ABORT                     ((nsresult) 0x80004004L)
+
+/* Returned when a function fails */
+#define NS_ERROR_FAILURE                   ((nsresult) 0x80004005L)
+
+/* Returned when an unexpected error occurs */
+#define NS_ERROR_UNEXPECTED                ((nsresult) 0x8000ffffL)
+
+/* Returned when a memory allocation fails */
+#define NS_ERROR_OUT_OF_MEMORY             ((nsresult) 0x8007000eL)
+
+/* Returned when an illegal value is passed */
+#define NS_ERROR_ILLEGAL_VALUE             ((nsresult) 0x80070057L)
+#define NS_ERROR_INVALID_ARG               NS_ERROR_ILLEGAL_VALUE
+
+/* Returned when a class doesn't allow aggregation */
+#define NS_ERROR_NO_AGGREGATION            ((nsresult) 0x80040110L)
+
+/* Returned when an operation can't complete due to an unavailable resource */
+#define NS_ERROR_NOT_AVAILABLE             ((nsresult) 0x80040111L)
+
+/* Returned when a class is not registered */
+#define NS_ERROR_FACTORY_NOT_REGISTERED    ((nsresult) 0x80040154L)
+
+/* Returned when a class cannot be registered, but may be tried again later */
+#define NS_ERROR_FACTORY_REGISTER_AGAIN    ((nsresult) 0x80040155L)
+
+/* Returned when a dynamically loaded factory couldn't be found */
+#define NS_ERROR_FACTORY_NOT_LOADED        ((nsresult) 0x800401f8L)
+
+/* Returned when a factory doesn't support signatures */
+#define NS_ERROR_FACTORY_NO_SIGNATURE_SUPPORT \
+                                           (NS_ERROR_BASE + 0x101)
+
+/* Returned when a factory already is registered */
+#define NS_ERROR_FACTORY_EXISTS            (NS_ERROR_BASE + 0x100)
+
+
+/**
+ * An "interface id" which can be used to uniquely identify a given
+ * interface.
+ * A "unique identifier". This is modeled after OSF DCE UUIDs.
+ */
+
+struct nsID {
+  PRUint32 m0;
+  PRUint16 m1;
+  PRUint16 m2;
+  PRUint8 m3[8];
+};
+
+typedef struct nsID nsID;
+typedef nsID nsIID;
+
+struct nsISupports;   /* forward declaration */
+struct nsIStackFrame; /* forward declaration */
+struct nsIException;  /* forward declaration */
+typedef struct nsISupports nsISupports;     /* forward declaration */
+typedef struct nsIStackFrame nsIStackFrame; /* forward declaration */
+typedef struct nsIException nsIException;   /* forward declaration */
+
+/**
+ * IID for the nsISupports interface
+ * {00000000-0000-0000-c000-000000000046}
+ *
+ * To maintain binary compatibility with COM's IUnknown, we define the IID
+ * of nsISupports to be the same as that of COM's IUnknown.
+ */
+#define NS_ISUPPORTS_IID                                                      \
+  { 0x00000000, 0x0000, 0x0000,                                               \
+    {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} }
+
+/**
+ * Reference count values
+ *
+ * This is the return type for AddRef() and Release() in nsISupports.
+ * IUnknown of COM returns an unsigned long from equivalent functions.
+ * The following ifdef exists to maintain binary compatibility with
+ * IUnknown.
+ */
+
+/**
+ * Basic component object model interface. Objects which implement
+ * this interface support runtime interface discovery (QueryInterface)
+ * and a reference counted memory model (AddRef/Release). This is
+ * modelled after the win32 IUnknown API.
+ */
+struct nsISupports_vtbl {
+
+  /**
+   * @name Methods
+   */
+
+  /**
+   * A run time mechanism for interface discovery.
+   * @param aIID         [in]  A requested interface IID
+   * @param aInstancePtr [out] A pointer to an interface pointer to
+   *                           receive the result.
+   * @return            NS_OK if the interface is supported by the associated
+   *                          instance, NS_NOINTERFACE if it is not.
+   * NS_ERROR_INVALID_POINTER if aInstancePtr is NULL.
+   */
+  nsresult (*QueryInterface)(nsISupports *pThis, const nsID *iid, void **resultp);
+  /**
+   * Increases the reference count for this interface.
+   * The associated instance will not be deleted unless
+   * the reference count is returned to zero.
+   *
+   * @return The resulting reference count.
+   */
+  nsresult (*AddRef)(nsISupports *pThis);
+
+  /**
+   * Decreases the reference count for this interface.
+   * Generally, if the reference count returns to zero,
+   * the associated instance is deleted.
+   *
+   * @return The resulting reference count.
+   */
+  nsresult (*Release)(nsISupports *pThis);
+
+};
+
+struct nsISupports {
+    struct nsISupports_vtbl *vtbl;
+};
+
+/* starting interface:    nsIException */
+#define NS_IEXCEPTION_IID_STR "f3a8d3b4-c424-4edc-8bf6-8974c983ba78"
+
+#define NS_IEXCEPTION_IID \
+  {0xf3a8d3b4, 0xc424, 0x4edc, \
+    { 0x8b, 0xf6, 0x89, 0x74, 0xc9, 0x83, 0xba, 0x78 }}
+
+struct nsIException_vtbl {
+
+  /* Methods from the Class nsISupports */
+  struct nsISupports_vtbl nsisupports;
+
+  /* readonly attribute string message; */
+  nsresult (*GetMessage)(nsIException *pThis, PRUnichar * *aMessage);
+
+  /* readonly attribute nsresult (*result; */
+  nsresult (*GetResult)(nsIException *pThis, nsresult *aResult);
+
+  /* readonly attribute string name; */
+  nsresult (*GetName)(nsIException *pThis, PRUnichar * *aName);
+
+  /* readonly attribute string filename; */
+  nsresult (*GetFilename)(nsIException *pThis, PRUnichar * *aFilename);
+
+  /* readonly attribute PRUint32 lineNumber; */
+  nsresult (*GetLineNumber)(nsIException *pThis, PRUint32 *aLineNumber);
+
+  /* readonly attribute PRUint32 columnNumber; */
+  nsresult (*GetColumnNumber)(nsIException *pThis, PRUint32 *aColumnNumber);
+
+  /* readonly attribute nsIStackFrame location; */
+  nsresult (*GetLocation)(nsIException *pThis, nsIStackFrame * *aLocation);
+
+  /* readonly attribute nsIException inner; */
+  nsresult (*GetInner)(nsIException *pThis, nsIException * *aInner);
+
+  /* readonly attribute nsISupports data; */
+  nsresult (*GetData)(nsIException *pThis, nsISupports * *aData);
+
+  /* string toString (); */
+  nsresult (*ToString)(nsIException *pThis, PRUnichar **_retval);
+};
+
+struct nsIException {
+    struct nsIException_vtbl *vtbl;
+};
+
+/* starting interface:    nsIStackFrame */
+#define NS_ISTACKFRAME_IID_STR "91d82105-7c62-4f8b-9779-154277c0ee90"
+
+#define NS_ISTACKFRAME_IID \
+  {0x91d82105, 0x7c62, 0x4f8b, \
+    { 0x97, 0x79, 0x15, 0x42, 0x77, 0xc0, 0xee, 0x90 }}
+
+struct nsIStackFrame_vtbl {
+
+  /* Methods from the Class nsISupports */
+  struct nsISupports_vtbl nsisupports;
+
+  /* readonly attribute PRUint32 language; */
+  nsresult (*GetLanguage)(nsIStackFrame *pThis, PRUint32 *aLanguage);
+
+  /* readonly attribute string languageName; */
+  nsresult (*GetLanguageName)(nsIStackFrame *pThis, PRUnichar * *aLanguageName);
+
+  /* readonly attribute string filename; */
+  nsresult (*GetFilename)(nsIStackFrame *pThis, PRUnichar * *aFilename);
+
+  /* readonly attribute string name; */
+  nsresult (*GetName)(nsIStackFrame *pThis, PRUnichar * *aName);
+
+  /* readonly attribute PRInt32 lineNumber; */
+  nsresult (*GetLineNumber)(nsIStackFrame *pThis, PRInt32 *aLineNumber);
+
+  /* readonly attribute string sourceLine; */
+  nsresult (*GetSourceLine)(nsIStackFrame *pThis, PRUnichar * *aSourceLine);
+
+  /* readonly attribute nsIStackFrame caller; */
+  nsresult (*GetCaller)(nsIStackFrame *pThis, nsIStackFrame * *aCaller);
+
+  /* string toString (); */
+  nsresult (*ToString)(nsIStackFrame *pThis, PRUnichar **_retval);
+};
+
+struct nsIStackFrame {
+    struct nsIStackFrame_vtbl *vtbl;
+};
+
+/* starting interface:    nsIEventTarget */
+#define NS_IEVENTTARGET_IID_STR "ea99ad5b-cc67-4efb-97c9-2ef620a59f2a"
+
+#define NS_IEVENTTARGET_IID \
+  {0xea99ad5b, 0xcc67, 0x4efb, \
+    { 0x97, 0xc9, 0x2e, 0xf6, 0x20, 0xa5, 0x9f, 0x2a }}
+
+struct nsIEventTarget;
+typedef struct nsIEventTarget nsIEventTarget;
+
+struct nsIEventTarget_vtbl {
+
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*PostEvent)(nsIEventTarget *pThis, PLEvent * aEvent);
+
+    nsresult (*IsOnCurrentThread)(nsIEventTarget *pThis, PRBool *_retval);
+
+};
+
+struct nsIEventTarget {
+    struct nsIEventTarget_vtbl *vtbl;
+};
+
+/* starting interface:    nsIEventQueue */
+#define NS_IEVENTQUEUE_IID_STR "176afb41-00a4-11d3-9f2a-00400553eef0"
+
+#define NS_IEVENTQUEUE_IID \
+  {0x176afb41, 0x00a4, 0x11d3, \
+    { 0x9f, 0x2a, 0x00, 0x40, 0x05, 0x53, 0xee, 0xf0 }}
+
+struct nsIEventQueue;
+typedef struct nsIEventQueue nsIEventQueue;
+
+struct nsIEventQueue_vtbl {
+
+    struct nsIEventTarget_vtbl nsieventtarget;
+
+    nsresult (*InitEvent)(nsIEventQueue *pThis, PLEvent * aEvent, void * owner, PLHandleEventProc handler, PLDestroyEventProc destructor);
+
+    nsresult (*PostSynchronousEvent)(nsIEventQueue *pThis, PLEvent * aEvent, void * *aResult);
+
+    nsresult (*PendingEvents)(nsIEventQueue *pThis, PRBool *_retval);
+
+    nsresult (*ProcessPendingEvents)(nsIEventQueue *pThis);
+
+    nsresult (*EventLoop)(nsIEventQueue *pThis);
+
+    nsresult (*EventAvailable)(nsIEventQueue *pThis, PRBool *aResult);
+
+    nsresult (*GetEvent)(nsIEventQueue *pThis, PLEvent * *_retval);
+
+    nsresult (*HandleEvent)(nsIEventQueue *pThis, PLEvent * aEvent);
+
+    nsresult (*WaitForEvent)(nsIEventQueue *pThis, PLEvent * *_retval);
+
+    PRInt32 (*GetEventQueueSelectFD)(nsIEventQueue *pThis);
+
+    nsresult (*Init)(nsIEventQueue *pThis, PRBool aNative);
+
+    nsresult (*InitFromPRThread)(nsIEventQueue *pThis, PRThread * thread, PRBool aNative);
+
+    nsresult (*InitFromPLQueue)(nsIEventQueue *pThis, PLEventQueue * aQueue);
+
+    nsresult (*EnterMonitor)(nsIEventQueue *pThis);
+
+    nsresult (*ExitMonitor)(nsIEventQueue *pThis);
+
+    nsresult (*RevokeEvents)(nsIEventQueue *pThis, void * owner);
+
+    nsresult (*GetPLEventQueue)(nsIEventQueue *pThis, PLEventQueue * *_retval);
+
+    nsresult (*IsQueueNative)(nsIEventQueue *pThis, PRBool *_retval);
+
+    nsresult (*StopAcceptingEvents)(nsIEventQueue *pThis);
+
+};
+
+struct nsIEventQueue {
+    struct nsIEventQueue_vtbl *vtbl;
+};
+
+
+#define VBOX_E_OBJECT_NOT_FOUND 0x80BB0001
+#define VBOX_E_INVALID_VM_STATE 0x80BB0002
+#define VBOX_E_VM_ERROR 0x80BB0003
+#define VBOX_E_FILE_ERROR 0x80BB0004
+#define VBOX_E_IPRT_ERROR 0x80BB0005
+#define VBOX_E_PDM_ERROR 0x80BB0006
+#define VBOX_E_INVALID_OBJECT_STATE 0x80BB0007
+#define VBOX_E_HOST_ERROR 0x80BB0008
+#define VBOX_E_NOT_SUPPORTED 0x80BB0009
+#define VBOX_E_XML_ERROR 0x80BB000A
+#define VBOX_E_INVALID_SESSION_STATE 0x80BB000B
+#define VBOX_E_OBJECT_IN_USE 0x80BB000C
+
+
+struct IVirtualBoxErrorInfo;
+struct ILocalOwner;
+struct IVirtualBoxCallback;
+struct IDHCPServer;
+struct IVirtualBox;
+struct IVFSExplorer;
+struct IAppliance;
+struct IVirtualSystemDescription;
+struct IInternalMachineControl;
+struct IBIOSSettings;
+struct IMachine;
+struct IConsoleCallback;
+struct IRemoteDisplayInfo;
+struct IConsole;
+struct IHostNetworkInterface;
+struct IHost;
+struct ISystemProperties;
+struct IGuestOSType;
+struct IGuest;
+struct IProgress;
+struct ISnapshot;
+struct IMediumAttachment;
+struct IMedium;
+struct IMediumFormat;
+struct IKeyboard;
+struct IMouse;
+struct IFramebuffer;
+struct IFramebufferOverlay;
+struct IDisplay;
+struct INetworkAdapter;
+struct ISerialPort;
+struct IParallelPort;
+struct IMachineDebugger;
+struct IUSBController;
+struct IUSBDevice;
+struct IUSBDeviceFilter;
+struct IHostUSBDevice;
+struct IHostUSBDeviceFilter;
+struct IAudioAdapter;
+struct IVRDPServer;
+struct ISharedFolder;
+struct IInternalSessionControl;
+struct ISession;
+struct IStorageController;
+struct IManagedObjectRef;
+struct IWebsessionManager;
+struct IPerformanceMetric;
+struct IPerformanceCollector;
+
+typedef struct IVirtualBoxErrorInfo IVirtualBoxErrorInfo;
+typedef struct ILocalOwner ILocalOwner;
+typedef struct IVirtualBoxCallback IVirtualBoxCallback;
+typedef struct IDHCPServer IDHCPServer;
+typedef struct IVirtualBox IVirtualBox;
+typedef struct IVFSExplorer IVFSExplorer;
+typedef struct IAppliance IAppliance;
+typedef struct IVirtualSystemDescription IVirtualSystemDescription;
+typedef struct IInternalMachineControl IInternalMachineControl;
+typedef struct IBIOSSettings IBIOSSettings;
+typedef struct IMachine IMachine;
+typedef struct IConsoleCallback IConsoleCallback;
+typedef struct IRemoteDisplayInfo IRemoteDisplayInfo;
+typedef struct IConsole IConsole;
+typedef struct IHostNetworkInterface IHostNetworkInterface;
+typedef struct IHost IHost;
+typedef struct ISystemProperties ISystemProperties;
+typedef struct IGuestOSType IGuestOSType;
+typedef struct IGuest IGuest;
+typedef struct IProgress IProgress;
+typedef struct ISnapshot ISnapshot;
+typedef struct IMediumAttachment IMediumAttachment;
+typedef struct IMedium IMedium;
+typedef struct IMediumFormat IMediumFormat;
+typedef struct IKeyboard IKeyboard;
+typedef struct IMouse IMouse;
+typedef struct IFramebuffer IFramebuffer;
+typedef struct IFramebufferOverlay IFramebufferOverlay;
+typedef struct IDisplay IDisplay;
+typedef struct INetworkAdapter INetworkAdapter;
+typedef struct ISerialPort ISerialPort;
+typedef struct IParallelPort IParallelPort;
+typedef struct IMachineDebugger IMachineDebugger;
+typedef struct IUSBController IUSBController;
+typedef struct IUSBDevice IUSBDevice;
+typedef struct IUSBDeviceFilter IUSBDeviceFilter;
+typedef struct IHostUSBDevice IHostUSBDevice;
+typedef struct IHostUSBDeviceFilter IHostUSBDeviceFilter;
+typedef struct IAudioAdapter IAudioAdapter;
+typedef struct IVRDPServer IVRDPServer;
+typedef struct ISharedFolder ISharedFolder;
+typedef struct IInternalSessionControl IInternalSessionControl;
+typedef struct ISession ISession;
+typedef struct IStorageController IStorageController;
+typedef struct IManagedObjectRef IManagedObjectRef;
+typedef struct IWebsessionManager IWebsessionManager;
+typedef struct IPerformanceMetric IPerformanceMetric;
+typedef struct IPerformanceCollector IPerformanceCollector;
+
+/* Start of enum SettingsVersion Declaration */
+#define SETTINGSVERSION_IID_STR "52bd6f5f-1adb-4493-975d-581a9c4b803f"
+#define SETTINGSVERSION_IID { \
+    0x52bd6f5f, 0x1adb, 0x4493, \
+    { 0x97, 0x5d, 0x58, 0x1a, 0x9c, 0x4b, 0x80, 0x3f } \
+}
+enum SettingsVersion
+{
+    SettingsVersion_Null = 0,
+    SettingsVersion_v1_0 = 1,
+    SettingsVersion_v1_1 = 2,
+    SettingsVersion_v1_2 = 3,
+    SettingsVersion_v1_3pre = 4,
+    SettingsVersion_v1_3 = 5,
+    SettingsVersion_v1_4 = 6,
+    SettingsVersion_v1_5 = 7,
+    SettingsVersion_v1_6 = 8,
+    SettingsVersion_v1_7 = 9,
+    SettingsVersion_v1_8 = 10,
+    SettingsVersion_v1_9 = 11,
+    SettingsVersion_Future = 12
+};
+/* End of enum SettingsVersion Declaration */
+
+
+/* Start of enum AccessMode Declaration */
+#define ACCESSMODE_IID_STR "1da0007c-ddf7-4be8-bcac-d84a1558785f"
+#define ACCESSMODE_IID { \
+    0x1da0007c, 0xddf7, 0x4be8, \
+    { 0xbc, 0xac, 0xd8, 0x4a, 0x15, 0x58, 0x78, 0x5f } \
+}
+enum AccessMode
+{
+    AccessMode_ReadOnly = 1,
+    AccessMode_ReadWrite = 2
+};
+/* End of enum AccessMode Declaration */
+
+
+/* Start of enum MachineState Declaration */
+#define MACHINESTATE_IID_STR "36518cf6-cdf0-4d0d-ad2a-5ee9c60c7494"
+#define MACHINESTATE_IID { \
+    0x36518cf6, 0xcdf0, 0x4d0d, \
+    { 0xad, 0x2a, 0x5e, 0xe9, 0xc6, 0x0c, 0x74, 0x94 } \
+}
+enum MachineState
+{
+    MachineState_Null = 0,
+    MachineState_PoweredOff = 1,
+    MachineState_Saved = 2,
+    MachineState_Teleported = 3,
+    MachineState_Aborted = 4,
+    MachineState_Running = 5,
+    MachineState_Paused = 6,
+    MachineState_Stuck = 7,
+    MachineState_Teleporting = 8,
+    MachineState_LiveSnapshotting = 9,
+    MachineState_Starting = 10,
+    MachineState_Stopping = 11,
+    MachineState_Saving = 12,
+    MachineState_Restoring = 13,
+    MachineState_TeleportingPausedVM = 14,
+    MachineState_TeleportingIn = 15,
+    MachineState_RestoringSnapshot = 16,
+    MachineState_DeletingSnapshot = 17,
+    MachineState_SettingUp = 18,
+    MachineState_FirstOnline = 5,
+    MachineState_LastOnline = 13,
+    MachineState_FirstTransient = 8,
+    MachineState_LastTransient = 18
+};
+/* End of enum MachineState Declaration */
+
+
+/* Start of enum SessionState Declaration */
+#define SESSIONSTATE_IID_STR "cf2700c0-ea4b-47ae-9725-7810114b94d8"
+#define SESSIONSTATE_IID { \
+    0xcf2700c0, 0xea4b, 0x47ae, \
+    { 0x97, 0x25, 0x78, 0x10, 0x11, 0x4b, 0x94, 0xd8 } \
+}
+enum SessionState
+{
+    SessionState_Null = 0,
+    SessionState_Closed = 1,
+    SessionState_Open = 2,
+    SessionState_Spawning = 3,
+    SessionState_Closing = 4
+};
+/* End of enum SessionState Declaration */
+
+
+/* Start of enum CpuPropertyType Declaration */
+#define CPUPROPERTYTYPE_IID_STR "af7bb668-eeb1-4404-b77f-a114b30c92d6"
+#define CPUPROPERTYTYPE_IID { \
+    0xaf7bb668, 0xeeb1, 0x4404, \
+    { 0xb7, 0x7f, 0xa1, 0x14, 0xb3, 0x0c, 0x92, 0xd6 } \
+}
+enum CpuPropertyType
+{
+    CpuPropertyType_Null = 0,
+    CpuPropertyType_PAE = 1,
+    CpuPropertyType_Synthetic = 2
+};
+/* End of enum CpuPropertyType Declaration */
+
+
+/* Start of enum HWVirtExPropertyType Declaration */
+#define HWVIRTEXPROPERTYTYPE_IID_STR "ce81dfdd-d2b8-4a90-bbea-40ee8b7ffcee"
+#define HWVIRTEXPROPERTYTYPE_IID { \
+    0xce81dfdd, 0xd2b8, 0x4a90, \
+    { 0xbb, 0xea, 0x40, 0xee, 0x8b, 0x7f, 0xfc, 0xee } \
+}
+enum HWVirtExPropertyType
+{
+    HWVirtExPropertyType_Null = 0,
+    HWVirtExPropertyType_Enabled = 1,
+    HWVirtExPropertyType_Exclusive = 2,
+    HWVirtExPropertyType_VPID = 3,
+    HWVirtExPropertyType_NestedPaging = 4
+};
+/* End of enum HWVirtExPropertyType Declaration */
+
+
+/* Start of enum SessionType Declaration */
+#define SESSIONTYPE_IID_STR "A13C02CB-0C2C-421E-8317-AC0E8AAA153A"
+#define SESSIONTYPE_IID { \
+    0xA13C02CB, 0x0C2C, 0x421E, \
+    { 0x83, 0x17, 0xAC, 0x0E, 0x8A, 0xAA, 0x15, 0x3A } \
+}
+enum SessionType
+{
+    SessionType_Null = 0,
+    SessionType_Direct = 1,
+    SessionType_Remote = 2,
+    SessionType_Existing = 3
+};
+/* End of enum SessionType Declaration */
+
+
+/* Start of enum DeviceType Declaration */
+#define DEVICETYPE_IID_STR "6d9420f7-0b56-4636-99f9-7346f1b01e57"
+#define DEVICETYPE_IID { \
+    0x6d9420f7, 0x0b56, 0x4636, \
+    { 0x99, 0xf9, 0x73, 0x46, 0xf1, 0xb0, 0x1e, 0x57 } \
+}
+enum DeviceType
+{
+    DeviceType_Null = 0,
+    DeviceType_Floppy = 1,
+    DeviceType_DVD = 2,
+    DeviceType_HardDisk = 3,
+    DeviceType_Network = 4,
+    DeviceType_USB = 5,
+    DeviceType_SharedFolder = 6
+};
+/* End of enum DeviceType Declaration */
+
+
+/* Start of enum DeviceActivity Declaration */
+#define DEVICEACTIVITY_IID_STR "6FC8AEAA-130A-4eb5-8954-3F921422D707"
+#define DEVICEACTIVITY_IID { \
+    0x6FC8AEAA, 0x130A, 0x4eb5, \
+    { 0x89, 0x54, 0x3F, 0x92, 0x14, 0x22, 0xD7, 0x07 } \
+}
+enum DeviceActivity
+{
+    DeviceActivity_Null = 0,
+    DeviceActivity_Idle = 1,
+    DeviceActivity_Reading = 2,
+    DeviceActivity_Writing = 3
+};
+/* End of enum DeviceActivity Declaration */
+
+
+/* Start of enum ClipboardMode Declaration */
+#define CLIPBOARDMODE_IID_STR "33364716-4008-4701-8f14-be0fa3d62950"
+#define CLIPBOARDMODE_IID { \
+    0x33364716, 0x4008, 0x4701, \
+    { 0x8f, 0x14, 0xbe, 0x0f, 0xa3, 0xd6, 0x29, 0x50 } \
+}
+enum ClipboardMode
+{
+    ClipboardMode_Disabled = 0,
+    ClipboardMode_HostToGuest = 1,
+    ClipboardMode_GuestToHost = 2,
+    ClipboardMode_Bidirectional = 3
+};
+/* End of enum ClipboardMode Declaration */
+
+
+/* Start of enum Scope Declaration */
+#define SCOPE_IID_STR "7c91096e-499e-4eca-9f9b-9001438d7855"
+#define SCOPE_IID { \
+    0x7c91096e, 0x499e, 0x4eca, \
+    { 0x9f, 0x9b, 0x90, 0x01, 0x43, 0x8d, 0x78, 0x55 } \
+}
+enum Scope
+{
+    Scope_Global = 0,
+    Scope_Machine = 1,
+    Scope_Session = 2
+};
+/* End of enum Scope Declaration */
+
+
+/* Start of enum GuestStatisticType Declaration */
+#define GUESTSTATISTICTYPE_IID_STR "aa7c1d71-aafe-47a8-9608-27d2d337cf55"
+#define GUESTSTATISTICTYPE_IID { \
+    0xaa7c1d71, 0xaafe, 0x47a8, \
+    { 0x96, 0x08, 0x27, 0xd2, 0xd3, 0x37, 0xcf, 0x55 } \
+}
+enum GuestStatisticType
+{
+    GuestStatisticType_CPULoad_Idle = 0,
+    GuestStatisticType_CPULoad_Kernel = 1,
+    GuestStatisticType_CPULoad_User = 2,
+    GuestStatisticType_Threads = 3,
+    GuestStatisticType_Processes = 4,
+    GuestStatisticType_Handles = 5,
+    GuestStatisticType_MemoryLoad = 6,
+    GuestStatisticType_PhysMemTotal = 7,
+    GuestStatisticType_PhysMemAvailable = 8,
+    GuestStatisticType_PhysMemBalloon = 9,
+    GuestStatisticType_MemCommitTotal = 10,
+    GuestStatisticType_MemKernelTotal = 11,
+    GuestStatisticType_MemKernelPaged = 12,
+    GuestStatisticType_MemKernelNonpaged = 13,
+    GuestStatisticType_MemSystemCache = 14,
+    GuestStatisticType_PageFileSize = 15,
+    GuestStatisticType_SampleNumber = 16,
+    GuestStatisticType_MaxVal = 17
+};
+/* End of enum GuestStatisticType Declaration */
+
+
+/* Start of enum BIOSBootMenuMode Declaration */
+#define BIOSBOOTMENUMODE_IID_STR "ae4fb9f7-29d2-45b4-b2c7-d579603135d5"
+#define BIOSBOOTMENUMODE_IID { \
+    0xae4fb9f7, 0x29d2, 0x45b4, \
+    { 0xb2, 0xc7, 0xd5, 0x79, 0x60, 0x31, 0x35, 0xd5 } \
+}
+enum BIOSBootMenuMode
+{
+    BIOSBootMenuMode_Disabled = 0,
+    BIOSBootMenuMode_MenuOnly = 1,
+    BIOSBootMenuMode_MessageAndMenu = 2
+};
+/* End of enum BIOSBootMenuMode Declaration */
+
+
+/* Start of enum ProcessorFeature Declaration */
+#define PROCESSORFEATURE_IID_STR "64c38e6b-8bcf-45ad-ac03-9b406287c5bf"
+#define PROCESSORFEATURE_IID { \
+    0x64c38e6b, 0x8bcf, 0x45ad, \
+    { 0xac, 0x03, 0x9b, 0x40, 0x62, 0x87, 0xc5, 0xbf } \
+}
+enum ProcessorFeature
+{
+    ProcessorFeature_HWVirtEx = 0,
+    ProcessorFeature_PAE = 1,
+    ProcessorFeature_LongMode = 2,
+    ProcessorFeature_NestedPaging = 3
+};
+/* End of enum ProcessorFeature Declaration */
+
+
+/* Start of enum FirmwareType Declaration */
+#define FIRMWARETYPE_IID_STR "7ceea938-8b49-41e2-bb47-667219c0d586"
+#define FIRMWARETYPE_IID { \
+    0x7ceea938, 0x8b49, 0x41e2, \
+    { 0xbb, 0x47, 0x66, 0x72, 0x19, 0xc0, 0xd5, 0x86 } \
+}
+enum FirmwareType
+{
+    FirmwareType_BIOS = 1,
+    FirmwareType_EFI = 2,
+    FirmwareType_EFI64 = 3,
+    FirmwareType_EFIDUAL = 4
+};
+/* End of enum FirmwareType Declaration */
+
+
+/* Start of enum VFSType Declaration */
+#define VFSTYPE_IID_STR "813999ba-b949-48a8-9230-aadc6285e2f2"
+#define VFSTYPE_IID { \
+    0x813999ba, 0xb949, 0x48a8, \
+    { 0x92, 0x30, 0xaa, 0xdc, 0x62, 0x85, 0xe2, 0xf2 } \
+}
+enum VFSType
+{
+    VFSType_File = 1,
+    VFSType_Cloud = 2,
+    VFSType_S3 = 3,
+    VFSType_WebDav = 4
+};
+/* End of enum VFSType Declaration */
+
+
+/* Start of enum VFSFileType Declaration */
+#define VFSFILETYPE_IID_STR "714333cd-44e2-415f-a245-d378fa9b1242"
+#define VFSFILETYPE_IID { \
+    0x714333cd, 0x44e2, 0x415f, \
+    { 0xa2, 0x45, 0xd3, 0x78, 0xfa, 0x9b, 0x12, 0x42 } \
+}
+enum VFSFileType
+{
+    VFSFileType_Unknown = 1,
+    VFSFileType_Fifo = 2,
+    VFSFileType_DevChar = 3,
+    VFSFileType_Directory = 4,
+    VFSFileType_DevBlock = 5,
+    VFSFileType_File = 6,
+    VFSFileType_SymLink = 7,
+    VFSFileType_Socket = 8,
+    VFSFileType_WhiteOut = 9
+};
+/* End of enum VFSFileType Declaration */
+
+
+/* Start of enum VirtualSystemDescriptionType Declaration */
+#define VIRTUALSYSTEMDESCRIPTIONTYPE_IID_STR "aacc58de-5b45-4f82-ae2e-dd9a824fc3b5"
+#define VIRTUALSYSTEMDESCRIPTIONTYPE_IID { \
+    0xaacc58de, 0x5b45, 0x4f82, \
+    { 0xae, 0x2e, 0xdd, 0x9a, 0x82, 0x4f, 0xc3, 0xb5 } \
+}
+enum VirtualSystemDescriptionType
+{
+    VirtualSystemDescriptionType_Ignore = 1,
+    VirtualSystemDescriptionType_OS = 2,
+    VirtualSystemDescriptionType_Name = 3,
+    VirtualSystemDescriptionType_Product = 4,
+    VirtualSystemDescriptionType_Vendor = 5,
+    VirtualSystemDescriptionType_Version = 6,
+    VirtualSystemDescriptionType_ProductUrl = 7,
+    VirtualSystemDescriptionType_VendorUrl = 8,
+    VirtualSystemDescriptionType_Description = 9,
+    VirtualSystemDescriptionType_License = 10,
+    VirtualSystemDescriptionType_Miscellaneous = 11,
+    VirtualSystemDescriptionType_CPU = 12,
+    VirtualSystemDescriptionType_Memory = 13,
+    VirtualSystemDescriptionType_HardDiskControllerIDE = 14,
+    VirtualSystemDescriptionType_HardDiskControllerSATA = 15,
+    VirtualSystemDescriptionType_HardDiskControllerSCSI = 16,
+    VirtualSystemDescriptionType_HardDiskImage = 17,
+    VirtualSystemDescriptionType_Floppy = 18,
+    VirtualSystemDescriptionType_CDROM = 19,
+    VirtualSystemDescriptionType_NetworkAdapter = 20,
+    VirtualSystemDescriptionType_USBController = 21,
+    VirtualSystemDescriptionType_SoundCard = 22
+};
+/* End of enum VirtualSystemDescriptionType Declaration */
+
+
+/* Start of enum VirtualSystemDescriptionValueType Declaration */
+#define VIRTUALSYSTEMDESCRIPTIONVALUETYPE_IID_STR "56d9403f-3425-4118-9919-36f2a9b8c77c"
+#define VIRTUALSYSTEMDESCRIPTIONVALUETYPE_IID { \
+    0x56d9403f, 0x3425, 0x4118, \
+    { 0x99, 0x19, 0x36, 0xf2, 0xa9, 0xb8, 0xc7, 0x7c } \
+}
+enum VirtualSystemDescriptionValueType
+{
+    VirtualSystemDescriptionValueType_Reference = 1,
+    VirtualSystemDescriptionValueType_Original = 2,
+    VirtualSystemDescriptionValueType_Auto = 3,
+    VirtualSystemDescriptionValueType_ExtraConfig = 4
+};
+/* End of enum VirtualSystemDescriptionValueType Declaration */
+
+
+/* Start of enum HostNetworkInterfaceMediumType Declaration */
+#define HOSTNETWORKINTERFACEMEDIUMTYPE_IID_STR "1aa54aaf-2497-45a2-bfb1-8eb225e93d5b"
+#define HOSTNETWORKINTERFACEMEDIUMTYPE_IID { \
+    0x1aa54aaf, 0x2497, 0x45a2, \
+    { 0xbf, 0xb1, 0x8e, 0xb2, 0x25, 0xe9, 0x3d, 0x5b } \
+}
+enum HostNetworkInterfaceMediumType
+{
+    HostNetworkInterfaceMediumType_Unknown = 0,
+    HostNetworkInterfaceMediumType_Ethernet = 1,
+    HostNetworkInterfaceMediumType_PPP = 2,
+    HostNetworkInterfaceMediumType_SLIP = 3
+};
+/* End of enum HostNetworkInterfaceMediumType Declaration */
+
+
+/* Start of enum HostNetworkInterfaceStatus Declaration */
+#define HOSTNETWORKINTERFACESTATUS_IID_STR "CC474A69-2710-434B-8D99-C38E5D5A6F41"
+#define HOSTNETWORKINTERFACESTATUS_IID { \
+    0xCC474A69, 0x2710, 0x434B, \
+    { 0x8D, 0x99, 0xC3, 0x8E, 0x5D, 0x5A, 0x6F, 0x41 } \
+}
+enum HostNetworkInterfaceStatus
+{
+    HostNetworkInterfaceStatus_Unknown = 0,
+    HostNetworkInterfaceStatus_Up = 1,
+    HostNetworkInterfaceStatus_Down = 2
+};
+/* End of enum HostNetworkInterfaceStatus Declaration */
+
+
+/* Start of enum HostNetworkInterfaceType Declaration */
+#define HOSTNETWORKINTERFACETYPE_IID_STR "67431b00-9946-48a2-bc02-b25c5919f4f3"
+#define HOSTNETWORKINTERFACETYPE_IID { \
+    0x67431b00, 0x9946, 0x48a2, \
+    { 0xbc, 0x02, 0xb2, 0x5c, 0x59, 0x19, 0xf4, 0xf3 } \
+}
+enum HostNetworkInterfaceType
+{
+    HostNetworkInterfaceType_Bridged = 1,
+    HostNetworkInterfaceType_HostOnly = 2
+};
+/* End of enum HostNetworkInterfaceType Declaration */
+
+
+/* Start of enum MediumState Declaration */
+#define MEDIUMSTATE_IID_STR "ef41e980-e012-43cd-9dea-479d4ef14d13"
+#define MEDIUMSTATE_IID { \
+    0xef41e980, 0xe012, 0x43cd, \
+    { 0x9d, 0xea, 0x47, 0x9d, 0x4e, 0xf1, 0x4d, 0x13 } \
+}
+enum MediumState
+{
+    MediumState_NotCreated = 0,
+    MediumState_Created = 1,
+    MediumState_LockedRead = 2,
+    MediumState_LockedWrite = 3,
+    MediumState_Inaccessible = 4,
+    MediumState_Creating = 5,
+    MediumState_Deleting = 6
+};
+/* End of enum MediumState Declaration */
+
+
+/* Start of enum MediumType Declaration */
+#define MEDIUMTYPE_IID_STR "11f6f7a5-0327-409a-9d42-7db6a0cec578"
+#define MEDIUMTYPE_IID { \
+    0x11f6f7a5, 0x0327, 0x409a, \
+    { 0x9d, 0x42, 0x7d, 0xb6, 0xa0, 0xce, 0xc5, 0x78 } \
+}
+enum MediumType
+{
+    MediumType_Normal = 0,
+    MediumType_Immutable = 1,
+    MediumType_Writethrough = 2
+};
+/* End of enum MediumType Declaration */
+
+
+/* Start of enum MediumVariant Declaration */
+#define MEDIUMVARIANT_IID_STR "584ea502-143b-4ab0-ad14-d1028fdf0316"
+#define MEDIUMVARIANT_IID { \
+    0x584ea502, 0x143b, 0x4ab0, \
+    { 0xad, 0x14, 0xd1, 0x02, 0x8f, 0xdf, 0x03, 0x16 } \
+}
+enum MediumVariant
+{
+    MediumVariant_Standard = 0,
+    MediumVariant_VmdkSplit2G = 0x01,
+    MediumVariant_VmdkStreamOptimized = 0x04,
+    MediumVariant_VmdkESX = 0x08,
+    MediumVariant_Fixed = 0x10000,
+    MediumVariant_Diff = 0x20000
+};
+/* End of enum MediumVariant Declaration */
+
+
+/* Start of enum DataType Declaration */
+#define DATATYPE_IID_STR "d90ea51e-a3f1-4a01-beb1-c1723c0d3ba7"
+#define DATATYPE_IID { \
+    0xd90ea51e, 0xa3f1, 0x4a01, \
+    { 0xbe, 0xb1, 0xc1, 0x72, 0x3c, 0x0d, 0x3b, 0xa7 } \
+}
+enum DataType
+{
+    DataType_Int32 = 0,
+    DataType_Int8 = 1,
+    DataType_String = 2
+};
+/* End of enum DataType Declaration */
+
+
+/* Start of enum DataFlags Declaration */
+#define DATAFLAGS_IID_STR "86884dcf-1d6b-4f1b-b4bf-f5aa44959d60"
+#define DATAFLAGS_IID { \
+    0x86884dcf, 0x1d6b, 0x4f1b, \
+    { 0xb4, 0xbf, 0xf5, 0xaa, 0x44, 0x95, 0x9d, 0x60 } \
+}
+enum DataFlags
+{
+    DataFlags_None = 0x00,
+    DataFlags_Mandatory = 0x01,
+    DataFlags_Expert = 0x02,
+    DataFlags_Array = 0x04,
+    DataFlags_FlagMask = 0x07
+};
+/* End of enum DataFlags Declaration */
+
+
+/* Start of enum MediumFormatCapabilities Declaration */
+#define MEDIUMFORMATCAPABILITIES_IID_STR "70fcf810-99e8-4edc-aee4-7f51d489e657"
+#define MEDIUMFORMATCAPABILITIES_IID { \
+    0x70fcf810, 0x99e8, 0x4edc, \
+    { 0xae, 0xe4, 0x7f, 0x51, 0xd4, 0x89, 0xe6, 0x57 } \
+}
+enum MediumFormatCapabilities
+{
+    MediumFormatCapabilities_Uuid = 0x01,
+    MediumFormatCapabilities_CreateFixed = 0x02,
+    MediumFormatCapabilities_CreateDynamic = 0x04,
+    MediumFormatCapabilities_CreateSplit2G = 0x08,
+    MediumFormatCapabilities_Differencing = 0x10,
+    MediumFormatCapabilities_Asynchronous = 0x20,
+    MediumFormatCapabilities_File = 0x40,
+    MediumFormatCapabilities_Properties = 0x80,
+    MediumFormatCapabilities_CapabilityMask = 0xFF
+};
+/* End of enum MediumFormatCapabilities Declaration */
+
+
+/* Start of enum MouseButtonState Declaration */
+#define MOUSEBUTTONSTATE_IID_STR "9ee094b8-b28a-4d56-a166-973cb588d7f8"
+#define MOUSEBUTTONSTATE_IID { \
+    0x9ee094b8, 0xb28a, 0x4d56, \
+    { 0xa1, 0x66, 0x97, 0x3c, 0xb5, 0x88, 0xd7, 0xf8 } \
+}
+enum MouseButtonState
+{
+    MouseButtonState_LeftButton = 0x01,
+    MouseButtonState_RightButton = 0x02,
+    MouseButtonState_MiddleButton = 0x04,
+    MouseButtonState_WheelUp = 0x08,
+    MouseButtonState_WheelDown = 0x10,
+    MouseButtonState_XButton1 = 0x20,
+    MouseButtonState_XButton2 = 0x40,
+    MouseButtonState_MouseStateMask = 0x7F
+};
+/* End of enum MouseButtonState Declaration */
+
+
+/* Start of enum FramebufferPixelFormat Declaration */
+#define FRAMEBUFFERPIXELFORMAT_IID_STR "7acfd5ed-29e3-45e3-8136-73c9224f3d2d"
+#define FRAMEBUFFERPIXELFORMAT_IID { \
+    0x7acfd5ed, 0x29e3, 0x45e3, \
+    { 0x81, 0x36, 0x73, 0xc9, 0x22, 0x4f, 0x3d, 0x2d } \
+}
+enum FramebufferPixelFormat
+{
+    FramebufferPixelFormat_Opaque = 0,
+    FramebufferPixelFormat_FOURCC_RGB = 0x32424752
+};
+/* End of enum FramebufferPixelFormat Declaration */
+
+
+/* Start of enum NetworkAttachmentType Declaration */
+#define NETWORKATTACHMENTTYPE_IID_STR "44bce1ee-99f7-4e8e-89fc-80597fd9eeaf"
+#define NETWORKATTACHMENTTYPE_IID { \
+    0x44bce1ee, 0x99f7, 0x4e8e, \
+    { 0x89, 0xfc, 0x80, 0x59, 0x7f, 0xd9, 0xee, 0xaf } \
+}
+enum NetworkAttachmentType
+{
+    NetworkAttachmentType_Null = 0,
+    NetworkAttachmentType_NAT = 1,
+    NetworkAttachmentType_Bridged = 2,
+    NetworkAttachmentType_Internal = 3,
+    NetworkAttachmentType_HostOnly = 4
+};
+/* End of enum NetworkAttachmentType Declaration */
+
+
+/* Start of enum NetworkAdapterType Declaration */
+#define NETWORKADAPTERTYPE_IID_STR "3c2281e4-d952-4e87-8c7d-24379cb6a81c"
+#define NETWORKADAPTERTYPE_IID { \
+    0x3c2281e4, 0xd952, 0x4e87, \
+    { 0x8c, 0x7d, 0x24, 0x37, 0x9c, 0xb6, 0xa8, 0x1c } \
+}
+enum NetworkAdapterType
+{
+    NetworkAdapterType_Null = 0,
+    NetworkAdapterType_Am79C970A = 1,
+    NetworkAdapterType_Am79C973 = 2,
+    NetworkAdapterType_I82540EM = 3,
+    NetworkAdapterType_I82543GC = 4,
+    NetworkAdapterType_I82545EM = 5,
+    NetworkAdapterType_Virtio = 6
+};
+/* End of enum NetworkAdapterType Declaration */
+
+
+/* Start of enum PortMode Declaration */
+#define PORTMODE_IID_STR "533b5fe3-0185-4197-86a7-17e37dd39d76"
+#define PORTMODE_IID { \
+    0x533b5fe3, 0x0185, 0x4197, \
+    { 0x86, 0xa7, 0x17, 0xe3, 0x7d, 0xd3, 0x9d, 0x76 } \
+}
+enum PortMode
+{
+    PortMode_Disconnected = 0,
+    PortMode_HostPipe = 1,
+    PortMode_HostDevice = 2,
+    PortMode_RawFile = 3
+};
+/* End of enum PortMode Declaration */
+
+
+/* Start of enum USBDeviceState Declaration */
+#define USBDEVICESTATE_IID_STR "b99a2e65-67fb-4882-82fd-f3e5e8193ab4"
+#define USBDEVICESTATE_IID { \
+    0xb99a2e65, 0x67fb, 0x4882, \
+    { 0x82, 0xfd, 0xf3, 0xe5, 0xe8, 0x19, 0x3a, 0xb4 } \
+}
+enum USBDeviceState
+{
+    USBDeviceState_NotSupported = 0,
+    USBDeviceState_Unavailable = 1,
+    USBDeviceState_Busy = 2,
+    USBDeviceState_Available = 3,
+    USBDeviceState_Held = 4,
+    USBDeviceState_Captured = 5
+};
+/* End of enum USBDeviceState Declaration */
+
+
+/* Start of enum USBDeviceFilterAction Declaration */
+#define USBDEVICEFILTERACTION_IID_STR "cbc30a49-2f4e-43b5-9da6-121320475933"
+#define USBDEVICEFILTERACTION_IID { \
+    0xcbc30a49, 0x2f4e, 0x43b5, \
+    { 0x9d, 0xa6, 0x12, 0x13, 0x20, 0x47, 0x59, 0x33 } \
+}
+enum USBDeviceFilterAction
+{
+    USBDeviceFilterAction_Null = 0,
+    USBDeviceFilterAction_Ignore = 1,
+    USBDeviceFilterAction_Hold = 2
+};
+/* End of enum USBDeviceFilterAction Declaration */
+
+
+/* Start of enum AudioDriverType Declaration */
+#define AUDIODRIVERTYPE_IID_STR "4bcc3d73-c2fe-40db-b72f-0c2ca9d68496"
+#define AUDIODRIVERTYPE_IID { \
+    0x4bcc3d73, 0xc2fe, 0x40db, \
+    { 0xb7, 0x2f, 0x0c, 0x2c, 0xa9, 0xd6, 0x84, 0x96 } \
+}
+enum AudioDriverType
+{
+    AudioDriverType_Null = 0,
+    AudioDriverType_WinMM = 1,
+    AudioDriverType_OSS = 2,
+    AudioDriverType_ALSA = 3,
+    AudioDriverType_DirectSound = 4,
+    AudioDriverType_CoreAudio = 5,
+    AudioDriverType_MMPM = 6,
+    AudioDriverType_Pulse = 7,
+    AudioDriverType_SolAudio = 8
+};
+/* End of enum AudioDriverType Declaration */
+
+
+/* Start of enum AudioControllerType Declaration */
+#define AUDIOCONTROLLERTYPE_IID_STR "7afd395c-42c3-444e-8788-3ce80292f36c"
+#define AUDIOCONTROLLERTYPE_IID { \
+    0x7afd395c, 0x42c3, 0x444e, \
+    { 0x87, 0x88, 0x3c, 0xe8, 0x02, 0x92, 0xf3, 0x6c } \
+}
+enum AudioControllerType
+{
+    AudioControllerType_AC97 = 0,
+    AudioControllerType_SB16 = 1
+};
+/* End of enum AudioControllerType Declaration */
+
+
+/* Start of enum VRDPAuthType Declaration */
+#define VRDPAUTHTYPE_IID_STR "3d91887a-b67f-4b33-85bf-2da7ab1ea83a"
+#define VRDPAUTHTYPE_IID { \
+    0x3d91887a, 0xb67f, 0x4b33, \
+    { 0x85, 0xbf, 0x2d, 0xa7, 0xab, 0x1e, 0xa8, 0x3a } \
+}
+enum VRDPAuthType
+{
+    VRDPAuthType_Null = 0,
+    VRDPAuthType_External = 1,
+    VRDPAuthType_Guest = 2
+};
+/* End of enum VRDPAuthType Declaration */
+
+
+/* Start of enum StorageBus Declaration */
+#define STORAGEBUS_IID_STR "eee67ab3-668d-4ef5-91e0-7025fe4a0d7a"
+#define STORAGEBUS_IID { \
+    0xeee67ab3, 0x668d, 0x4ef5, \
+    { 0x91, 0xe0, 0x70, 0x25, 0xfe, 0x4a, 0x0d, 0x7a } \
+}
+enum StorageBus
+{
+    StorageBus_Null = 0,
+    StorageBus_IDE = 1,
+    StorageBus_SATA = 2,
+    StorageBus_SCSI = 3,
+    StorageBus_Floppy = 4
+};
+/* End of enum StorageBus Declaration */
+
+
+/* Start of enum StorageControllerType Declaration */
+#define STORAGECONTROLLERTYPE_IID_STR "8a412b8a-f43e-4456-bd37-b474f0879a58"
+#define STORAGECONTROLLERTYPE_IID { \
+    0x8a412b8a, 0xf43e, 0x4456, \
+    { 0xbd, 0x37, 0xb4, 0x74, 0xf0, 0x87, 0x9a, 0x58 } \
+}
+enum StorageControllerType
+{
+    StorageControllerType_Null = 0,
+    StorageControllerType_LsiLogic = 1,
+    StorageControllerType_BusLogic = 2,
+    StorageControllerType_IntelAhci = 3,
+    StorageControllerType_PIIX3 = 4,
+    StorageControllerType_PIIX4 = 5,
+    StorageControllerType_ICH6 = 6,
+    StorageControllerType_I82078 = 7
+};
+/* End of enum StorageControllerType Declaration */
+
+
+/* Start of struct IVirtualBoxErrorInfo Declaration */
+#define IVIRTUALBOXERRORINFO_IID_STR "4b86d186-407e-4f9e-8be8-e50061be8725"
+#define IVIRTUALBOXERRORINFO_IID { \
+    0x4b86d186, 0x407e, 0x4f9e, \
+    { 0x8b, 0xe8, 0xe5, 0x00, 0x61, 0xbe, 0x87, 0x25 } \
+}
+struct IVirtualBoxErrorInfo_vtbl
+{
+    struct nsIException_vtbl nsiexception;
+
+    nsresult (*GetResultCode)(IVirtualBoxErrorInfo *pThis, PRInt32 *resultCode);
+
+    nsresult (*GetInterfaceID)(IVirtualBoxErrorInfo *pThis, PRUnichar * *interfaceID);
+
+    nsresult (*GetComponent)(IVirtualBoxErrorInfo *pThis, PRUnichar * *component);
+
+    nsresult (*GetText)(IVirtualBoxErrorInfo *pThis, PRUnichar * *text);
+
+    nsresult (*GetNext)(IVirtualBoxErrorInfo *pThis, IVirtualBoxErrorInfo * *next);
+
+};
+
+struct IVirtualBoxErrorInfo
+{
+    struct IVirtualBoxErrorInfo_vtbl *vtbl;
+};
+/* End of struct IVirtualBoxErrorInfo Declaration */
+
+
+/* Start of struct ILocalOwner Declaration */
+#define ILOCALOWNER_IID_STR "308FF42A-DC45-49D4-A950-B1EEE5E00BB5"
+#define ILOCALOWNER_IID { \
+    0x308FF42A, 0xDC45, 0x49D4, \
+    { 0xA9, 0x50, 0xB1, 0xEE, 0xE5, 0xE0, 0x0B, 0xB5 } \
+}
+struct ILocalOwner_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*SetLocalObject)(
+        ILocalOwner *pThis,
+        nsISupports * object
+    );
+
+};
+
+struct ILocalOwner
+{
+    struct ILocalOwner_vtbl *vtbl;
+};
+/* End of struct ILocalOwner Declaration */
+
+
+/* Start of struct IVirtualBoxCallback Declaration */
+#define IVIRTUALBOXCALLBACK_IID_STR "9a65adf2-3ee6-406b-bca2-2b1fa05f0d0b"
+#define IVIRTUALBOXCALLBACK_IID { \
+    0x9a65adf2, 0x3ee6, 0x406b, \
+    { 0xbc, 0xa2, 0x2b, 0x1f, 0xa0, 0x5f, 0x0d, 0x0b } \
+}
+struct IVirtualBoxCallback_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*OnMachineStateChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUint32 state
+    );
+
+    nsresult (*OnMachineDataChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId
+    );
+
+    nsresult (*OnExtraDataCanChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUnichar * key,
+        PRUnichar * value,
+        PRUnichar * * error,
+        PRBool * allowChange
+    );
+
+    nsresult (*OnExtraDataChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUnichar * key,
+        PRUnichar * value
+    );
+
+    nsresult (*OnMediumRegistered)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * mediumId,
+        PRUint32 mediumType,
+        PRBool registered
+    );
+
+    nsresult (*OnMachineRegistered)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRBool registered
+    );
+
+    nsresult (*OnSessionStateChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUint32 state
+    );
+
+    nsresult (*OnSnapshotTaken)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUnichar * snapshotId
+    );
+
+    nsresult (*OnSnapshotDiscarded)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUnichar * snapshotId
+    );
+
+    nsresult (*OnSnapshotChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUnichar * snapshotId
+    );
+
+    nsresult (*OnGuestPropertyChange)(
+        IVirtualBoxCallback *pThis,
+        PRUnichar * machineId,
+        PRUnichar * name,
+        PRUnichar * value,
+        PRUnichar * flags
+    );
+
+};
+
+struct IVirtualBoxCallback
+{
+    struct IVirtualBoxCallback_vtbl *vtbl;
+};
+/* End of struct IVirtualBoxCallback Declaration */
+
+
+/* Start of struct IDHCPServer Declaration */
+#define IDHCPSERVER_IID_STR "6cfe387c-74fb-4ca7-bff6-973bec8af7a3"
+#define IDHCPSERVER_IID { \
+    0x6cfe387c, 0x74fb, 0x4ca7, \
+    { 0xbf, 0xf6, 0x97, 0x3b, 0xec, 0x8a, 0xf7, 0xa3 } \
+}
+struct IDHCPServer_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetEnabled)(IDHCPServer *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(IDHCPServer *pThis, PRBool enabled);
+
+    nsresult (*GetIPAddress)(IDHCPServer *pThis, PRUnichar * *IPAddress);
+
+    nsresult (*GetNetworkMask)(IDHCPServer *pThis, PRUnichar * *networkMask);
+
+    nsresult (*GetNetworkName)(IDHCPServer *pThis, PRUnichar * *networkName);
+
+    nsresult (*GetLowerIP)(IDHCPServer *pThis, PRUnichar * *lowerIP);
+
+    nsresult (*GetUpperIP)(IDHCPServer *pThis, PRUnichar * *upperIP);
+
+    nsresult (*SetConfiguration)(
+        IDHCPServer *pThis,
+        PRUnichar * IPAddress,
+        PRUnichar * networkMask,
+        PRUnichar * FromIPAddress,
+        PRUnichar * ToIPAddress
+    );
+
+    nsresult (*Start)(
+        IDHCPServer *pThis,
+        PRUnichar * networkName,
+        PRUnichar * trunkName,
+        PRUnichar * trunkType
+    );
+
+    nsresult (*Stop)(IDHCPServer *pThis );
+
+};
+
+struct IDHCPServer
+{
+    struct IDHCPServer_vtbl *vtbl;
+};
+/* End of struct IDHCPServer Declaration */
+
+
+/* Start of struct IVirtualBox Declaration */
+#define IVIRTUALBOX_IID_STR "cdc78bfb-f4f6-4ab6-aadf-0407ae12fe85"
+#define IVIRTUALBOX_IID { \
+    0xcdc78bfb, 0xf4f6, 0x4ab6, \
+    { 0xaa, 0xdf, 0x04, 0x07, 0xae, 0x12, 0xfe, 0x85 } \
+}
+struct IVirtualBox_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetVersion)(IVirtualBox *pThis, PRUnichar * *version);
+
+    nsresult (*GetRevision)(IVirtualBox *pThis, PRUint32 *revision);
+
+    nsresult (*GetPackageType)(IVirtualBox *pThis, PRUnichar * *packageType);
+
+    nsresult (*GetHomeFolder)(IVirtualBox *pThis, PRUnichar * *homeFolder);
+
+    nsresult (*GetSettingsFilePath)(IVirtualBox *pThis, PRUnichar * *settingsFilePath);
+
+    nsresult (*GetHost)(IVirtualBox *pThis, IHost * *host);
+
+    nsresult (*GetSystemProperties)(IVirtualBox *pThis, ISystemProperties * *systemProperties);
+
+    nsresult (*GetMachines)(IVirtualBox *pThis, PRUint32 *machinesSize, IMachine * **machines);
+
+    nsresult (*GetHardDisks)(IVirtualBox *pThis, PRUint32 *hardDisksSize, IMedium * **hardDisks);
+
+    nsresult (*GetDVDImages)(IVirtualBox *pThis, PRUint32 *DVDImagesSize, IMedium * **DVDImages);
+
+    nsresult (*GetFloppyImages)(IVirtualBox *pThis, PRUint32 *floppyImagesSize, IMedium * **floppyImages);
+
+    nsresult (*GetProgressOperations)(IVirtualBox *pThis, PRUint32 *progressOperationsSize, IProgress * **progressOperations);
+
+    nsresult (*GetGuestOSTypes)(IVirtualBox *pThis, PRUint32 *guestOSTypesSize, IGuestOSType * **guestOSTypes);
+
+    nsresult (*GetSharedFolders)(IVirtualBox *pThis, PRUint32 *sharedFoldersSize, ISharedFolder * **sharedFolders);
+
+    nsresult (*GetPerformanceCollector)(IVirtualBox *pThis, IPerformanceCollector * *performanceCollector);
+
+    nsresult (*GetDHCPServers)(IVirtualBox *pThis, PRUint32 *DHCPServersSize, IDHCPServer * **DHCPServers);
+
+    nsresult (*CreateMachine)(
+        IVirtualBox *pThis,
+        PRUnichar * name,
+        PRUnichar * osTypeId,
+        PRUnichar * baseFolder,
+        PRUnichar * id,
+        IMachine * * machine
+    );
+
+    nsresult (*CreateLegacyMachine)(
+        IVirtualBox *pThis,
+        PRUnichar * name,
+        PRUnichar * osTypeId,
+        PRUnichar * settingsFile,
+        PRUnichar * id,
+        IMachine * * machine
+    );
+
+    nsresult (*OpenMachine)(
+        IVirtualBox *pThis,
+        PRUnichar * settingsFile,
+        IMachine * * machine
+    );
+
+    nsresult (*RegisterMachine)(
+        IVirtualBox *pThis,
+        IMachine * machine
+    );
+
+    nsresult (*GetMachine)(
+        IVirtualBox *pThis,
+        PRUnichar * id,
+        IMachine * * machine
+    );
+
+    nsresult (*FindMachine)(
+        IVirtualBox *pThis,
+        PRUnichar * name,
+        IMachine * * machine
+    );
+
+    nsresult (*UnregisterMachine)(
+        IVirtualBox *pThis,
+        PRUnichar * id,
+        IMachine * * machine
+    );
+
+    nsresult (*CreateAppliance)(
+        IVirtualBox *pThis,
+        IAppliance * * appliance
+    );
+
+    nsresult (*CreateHardDisk)(
+        IVirtualBox *pThis,
+        PRUnichar * format,
+        PRUnichar * location,
+        IMedium * * medium
+    );
+
+    nsresult (*OpenHardDisk)(
+        IVirtualBox *pThis,
+        PRUnichar * location,
+        PRUint32 accessMode,
+        PRBool setImageId,
+        PRUnichar * imageId,
+        PRBool setParentId,
+        PRUnichar * parentId,
+        IMedium * * medium
+    );
+
+    nsresult (*GetHardDisk)(
+        IVirtualBox *pThis,
+        PRUnichar * id,
+        IMedium * * medium
+    );
+
+    nsresult (*FindHardDisk)(
+        IVirtualBox *pThis,
+        PRUnichar * location,
+        IMedium * * medium
+    );
+
+    nsresult (*OpenDVDImage)(
+        IVirtualBox *pThis,
+        PRUnichar * location,
+        PRUnichar * id,
+        IMedium * * image
+    );
+
+    nsresult (*GetDVDImage)(
+        IVirtualBox *pThis,
+        PRUnichar * id,
+        IMedium * * image
+    );
+
+    nsresult (*FindDVDImage)(
+        IVirtualBox *pThis,
+        PRUnichar * location,
+        IMedium * * image
+    );
+
+    nsresult (*OpenFloppyImage)(
+        IVirtualBox *pThis,
+        PRUnichar * location,
+        PRUnichar * id,
+        IMedium * * image
+    );
+
+    nsresult (*GetFloppyImage)(
+        IVirtualBox *pThis,
+        PRUnichar * id,
+        IMedium * * image
+    );
+
+    nsresult (*FindFloppyImage)(
+        IVirtualBox *pThis,
+        PRUnichar * location,
+        IMedium * * image
+    );
+
+    nsresult (*GetGuestOSType)(
+        IVirtualBox *pThis,
+        PRUnichar * id,
+        IGuestOSType * * type
+    );
+
+    nsresult (*CreateSharedFolder)(
+        IVirtualBox *pThis,
+        PRUnichar * name,
+        PRUnichar * hostPath,
+        PRBool writable
+    );
+
+    nsresult (*RemoveSharedFolder)(
+        IVirtualBox *pThis,
+        PRUnichar * name
+    );
+
+    nsresult (*GetExtraDataKeys)(
+        IVirtualBox *pThis,
+        PRUint32 *valueSize,
+        PRUnichar *** value
+    );
+
+    nsresult (*GetExtraData)(
+        IVirtualBox *pThis,
+        PRUnichar * key,
+        PRUnichar * * value
+    );
+
+    nsresult (*SetExtraData)(
+        IVirtualBox *pThis,
+        PRUnichar * key,
+        PRUnichar * value
+    );
+
+    nsresult (*OpenSession)(
+        IVirtualBox *pThis,
+        ISession * session,
+        PRUnichar * machineId
+    );
+
+    nsresult (*OpenRemoteSession)(
+        IVirtualBox *pThis,
+        ISession * session,
+        PRUnichar * machineId,
+        PRUnichar * type,
+        PRUnichar * environment,
+        IProgress * * progress
+    );
+
+    nsresult (*OpenExistingSession)(
+        IVirtualBox *pThis,
+        ISession * session,
+        PRUnichar * machineId
+    );
+
+    nsresult (*RegisterCallback)(
+        IVirtualBox *pThis,
+        IVirtualBoxCallback * callback
+    );
+
+    nsresult (*UnregisterCallback)(
+        IVirtualBox *pThis,
+        IVirtualBoxCallback * callback
+    );
+
+    nsresult (*WaitForPropertyChange)(
+        IVirtualBox *pThis,
+        PRUnichar * what,
+        PRUint32 timeout,
+        PRUnichar * * changed,
+        PRUnichar * * values
+    );
+
+    nsresult (*CreateDHCPServer)(
+        IVirtualBox *pThis,
+        PRUnichar * name,
+        IDHCPServer * * server
+    );
+
+    nsresult (*FindDHCPServerByNetworkName)(
+        IVirtualBox *pThis,
+        PRUnichar * name,
+        IDHCPServer * * server
+    );
+
+    nsresult (*RemoveDHCPServer)(
+        IVirtualBox *pThis,
+        IDHCPServer * server
+    );
+
+    nsresult (*CheckFirmwarePresent)(
+        IVirtualBox *pThis,
+        PRUint32 firmwareType,
+        PRUnichar * version,
+        PRUnichar * * url,
+        PRBool * result
+    );
+
+};
+
+struct IVirtualBox
+{
+    struct IVirtualBox_vtbl *vtbl;
+};
+/* End of struct IVirtualBox Declaration */
+
+
+/* Start of struct IVFSExplorer Declaration */
+#define IVFSEXPLORER_IID_STR "2bb864a1-02a3-4474-a1d4-fb5f23b742e1"
+#define IVFSEXPLORER_IID { \
+    0x2bb864a1, 0x02a3, 0x4474, \
+    { 0xa1, 0xd4, 0xfb, 0x5f, 0x23, 0xb7, 0x42, 0xe1 } \
+}
+struct IVFSExplorer_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetPath)(IVFSExplorer *pThis, PRUnichar * *path);
+
+    nsresult (*GetType)(IVFSExplorer *pThis, PRUint32 *type);
+
+    nsresult (*Update)(
+        IVFSExplorer *pThis,
+        IProgress * * aProgress
+    );
+
+    nsresult (*Cd)(
+        IVFSExplorer *pThis,
+        PRUnichar * aDir,
+        IProgress * * aProgress
+    );
+
+    nsresult (*CdUp)(
+        IVFSExplorer *pThis,
+        IProgress * * aProgress
+    );
+
+    nsresult (*EntryList)(
+        IVFSExplorer *pThis,
+        PRUint32 *aNamesSize,
+        PRUnichar *** aNames,
+        PRUint32 *aTypesSize,
+        PRUint32* aTypes
+    );
+
+    nsresult (*Exists)(
+        IVFSExplorer *pThis,
+        PRUint32 aNamesSize,
+        PRUnichar ** aNames,
+        PRUint32 *aExistsSize,
+        PRUnichar *** aExists
+    );
+
+    nsresult (*Remove)(
+        IVFSExplorer *pThis,
+        PRUint32 aNamesSize,
+        PRUnichar ** aNames,
+        IProgress * * aProgress
+    );
+
+};
+
+struct IVFSExplorer
+{
+    struct IVFSExplorer_vtbl *vtbl;
+};
+/* End of struct IVFSExplorer Declaration */
+
+
+/* Start of struct IAppliance Declaration */
+#define IAPPLIANCE_IID_STR "e3ba9ab9-ac2c-4266-8bd2-91c4bf721ceb"
+#define IAPPLIANCE_IID { \
+    0xe3ba9ab9, 0xac2c, 0x4266, \
+    { 0x8b, 0xd2, 0x91, 0xc4, 0xbf, 0x72, 0x1c, 0xeb } \
+}
+struct IAppliance_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetPath)(IAppliance *pThis, PRUnichar * *path);
+
+    nsresult (*GetDisks)(IAppliance *pThis, PRUint32 *disksSize, PRUnichar * **disks);
+
+    nsresult (*GetVirtualSystemDescriptions)(IAppliance *pThis, PRUint32 *virtualSystemDescriptionsSize, IVirtualSystemDescription * **virtualSystemDescriptions);
+
+    nsresult (*Read)(
+        IAppliance *pThis,
+        PRUnichar * file,
+        IProgress * * aProgress
+    );
+
+    nsresult (*Interpret)(IAppliance *pThis );
+
+    nsresult (*ImportMachines)(
+        IAppliance *pThis,
+        IProgress * * aProgress
+    );
+
+    nsresult (*CreateVFSExplorer)(
+        IAppliance *pThis,
+        PRUnichar * aUri,
+        IVFSExplorer * * aExplorer
+    );
+
+    nsresult (*Write)(
+        IAppliance *pThis,
+        PRUnichar * format,
+        PRUnichar * path,
+        IProgress * * aProgress
+    );
+
+    nsresult (*GetWarnings)(
+        IAppliance *pThis,
+        PRUint32 *aWarningsSize,
+        PRUnichar *** aWarnings
+    );
+
+};
+
+struct IAppliance
+{
+    struct IAppliance_vtbl *vtbl;
+};
+/* End of struct IAppliance Declaration */
+
+
+/* Start of struct IVirtualSystemDescription Declaration */
+#define IVIRTUALSYSTEMDESCRIPTION_IID_STR "d7525e6c-531a-4c51-8e04-41235083a3d8"
+#define IVIRTUALSYSTEMDESCRIPTION_IID { \
+    0xd7525e6c, 0x531a, 0x4c51, \
+    { 0x8e, 0x04, 0x41, 0x23, 0x50, 0x83, 0xa3, 0xd8 } \
+}
+struct IVirtualSystemDescription_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetCount)(IVirtualSystemDescription *pThis, PRUint32 *count);
+
+    nsresult (*GetDescription)(
+        IVirtualSystemDescription *pThis,
+        PRUint32 *aTypesSize,
+        PRUint32* aTypes,
+        PRUint32 *aRefsSize,
+        PRUnichar *** aRefs,
+        PRUint32 *aOvfValuesSize,
+        PRUnichar *** aOvfValues,
+        PRUint32 *aVBoxValuesSize,
+        PRUnichar *** aVBoxValues,
+        PRUint32 *aExtraConfigValuesSize,
+        PRUnichar *** aExtraConfigValues
+    );
+
+    nsresult (*GetDescriptionByType)(
+        IVirtualSystemDescription *pThis,
+        PRUint32 aType,
+        PRUint32 *aTypesSize,
+        PRUint32* aTypes,
+        PRUint32 *aRefsSize,
+        PRUnichar *** aRefs,
+        PRUint32 *aOvfValuesSize,
+        PRUnichar *** aOvfValues,
+        PRUint32 *aVBoxValuesSize,
+        PRUnichar *** aVBoxValues,
+        PRUint32 *aExtraConfigValuesSize,
+        PRUnichar *** aExtraConfigValues
+    );
+
+    nsresult (*GetValuesByType)(
+        IVirtualSystemDescription *pThis,
+        PRUint32 aType,
+        PRUint32 aWhich,
+        PRUint32 *aValuesSize,
+        PRUnichar *** aValues
+    );
+
+    nsresult (*SetFinalValues)(
+        IVirtualSystemDescription *pThis,
+        PRUint32 aEnabledSize,
+        PRBool* aEnabled,
+        PRUint32 aVBoxValuesSize,
+        PRUnichar ** aVBoxValues,
+        PRUint32 aExtraConfigValuesSize,
+        PRUnichar ** aExtraConfigValues
+    );
+
+    nsresult (*AddDescription)(
+        IVirtualSystemDescription *pThis,
+        PRUint32 aType,
+        PRUnichar * aVBoxValue,
+        PRUnichar * aExtraConfigValue
+    );
+
+};
+
+struct IVirtualSystemDescription
+{
+    struct IVirtualSystemDescription_vtbl *vtbl;
+};
+/* End of struct IVirtualSystemDescription Declaration */
+
+
+/* Start of struct IInternalMachineControl Declaration */
+#define IINTERNALMACHINECONTROL_IID_STR "35d8d838-d066-447d-927a-fd93afdbec90"
+#define IINTERNALMACHINECONTROL_IID { \
+    0x35d8d838, 0xd066, 0x447d, \
+    { 0x92, 0x7a, 0xfd, 0x93, 0xaf, 0xdb, 0xec, 0x90 } \
+}
+struct IInternalMachineControl_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*SetRemoveSavedState)(
+        IInternalMachineControl *pThis,
+        PRBool aRemove
+    );
+
+    nsresult (*UpdateState)(
+        IInternalMachineControl *pThis,
+        PRUint32 state
+    );
+
+    nsresult (*GetIPCId)(
+        IInternalMachineControl *pThis,
+        PRUnichar * * id
+    );
+
+    nsresult (*RunUSBDeviceFilters)(
+        IInternalMachineControl *pThis,
+        IUSBDevice * device,
+        PRBool * matched,
+        PRUint32 * maskedInterfaces
+    );
+
+    nsresult (*CaptureUSBDevice)(
+        IInternalMachineControl *pThis,
+        PRUnichar * id
+    );
+
+    nsresult (*DetachUSBDevice)(
+        IInternalMachineControl *pThis,
+        PRUnichar * id,
+        PRBool done
+    );
+
+    nsresult (*AutoCaptureUSBDevices)(IInternalMachineControl *pThis );
+
+    nsresult (*DetachAllUSBDevices)(
+        IInternalMachineControl *pThis,
+        PRBool done
+    );
+
+    nsresult (*OnSessionEnd)(
+        IInternalMachineControl *pThis,
+        ISession * session,
+        IProgress * * progress
+    );
+
+    nsresult (*BeginSavingState)(
+        IInternalMachineControl *pThis,
+        IProgress * progress,
+        PRUnichar * * stateFilePath
+    );
+
+    nsresult (*EndSavingState)(
+        IInternalMachineControl *pThis,
+        PRBool success
+    );
+
+    nsresult (*AdoptSavedState)(
+        IInternalMachineControl *pThis,
+        PRUnichar * savedStateFile
+    );
+
+    nsresult (*BeginTakingSnapshot)(
+        IInternalMachineControl *pThis,
+        IConsole * initiator,
+        PRUnichar * name,
+        PRUnichar * description,
+        IProgress * consoleProgress,
+        PRBool fTakingSnapshotOnline,
+        PRUnichar * * stateFilePath
+    );
+
+    nsresult (*EndTakingSnapshot)(
+        IInternalMachineControl *pThis,
+        PRBool success
+    );
+
+    nsresult (*DeleteSnapshot)(
+        IInternalMachineControl *pThis,
+        IConsole * initiator,
+        PRUnichar * id,
+        PRUint32 * machineState,
+        IProgress * * progress
+    );
+
+    nsresult (*RestoreSnapshot)(
+        IInternalMachineControl *pThis,
+        IConsole * initiator,
+        ISnapshot * snapshot,
+        PRUint32 * machineState,
+        IProgress * * progress
+    );
+
+    nsresult (*PullGuestProperties)(
+        IInternalMachineControl *pThis,
+        PRUint32 *nameSize,
+        PRUnichar *** name,
+        PRUint32 *valueSize,
+        PRUnichar *** value,
+        PRUint32 *timestampSize,
+        PRUint64* timestamp,
+        PRUint32 *flagsSize,
+        PRUnichar *** flags
+    );
+
+    nsresult (*PushGuestProperties)(
+        IInternalMachineControl *pThis,
+        PRUint32 nameSize,
+        PRUnichar ** name,
+        PRUint32 valueSize,
+        PRUnichar ** value,
+        PRUint32 timestampSize,
+        PRUint64* timestamp,
+        PRUint32 flagsSize,
+        PRUnichar ** flags
+    );
+
+    nsresult (*PushGuestProperty)(
+        IInternalMachineControl *pThis,
+        PRUnichar * name,
+        PRUnichar * value,
+        PRUint64 timestamp,
+        PRUnichar * flags
+    );
+
+    nsresult (*LockMedia)(IInternalMachineControl *pThis );
+
+    nsresult (*UnlockMedia)(IInternalMachineControl *pThis );
+
+};
+
+struct IInternalMachineControl
+{
+    struct IInternalMachineControl_vtbl *vtbl;
+};
+/* End of struct IInternalMachineControl Declaration */
+
+
+/* Start of struct IBIOSSettings Declaration */
+#define IBIOSSETTINGS_IID_STR "38b54279-dc35-4f5e-a431-835b867c6b5e"
+#define IBIOSSETTINGS_IID { \
+    0x38b54279, 0xdc35, 0x4f5e, \
+    { 0xa4, 0x31, 0x83, 0x5b, 0x86, 0x7c, 0x6b, 0x5e } \
+}
+struct IBIOSSettings_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetLogoFadeIn)(IBIOSSettings *pThis, PRBool *logoFadeIn);
+    nsresult (*SetLogoFadeIn)(IBIOSSettings *pThis, PRBool logoFadeIn);
+
+    nsresult (*GetLogoFadeOut)(IBIOSSettings *pThis, PRBool *logoFadeOut);
+    nsresult (*SetLogoFadeOut)(IBIOSSettings *pThis, PRBool logoFadeOut);
+
+    nsresult (*GetLogoDisplayTime)(IBIOSSettings *pThis, PRUint32 *logoDisplayTime);
+    nsresult (*SetLogoDisplayTime)(IBIOSSettings *pThis, PRUint32 logoDisplayTime);
+
+    nsresult (*GetLogoImagePath)(IBIOSSettings *pThis, PRUnichar * *logoImagePath);
+    nsresult (*SetLogoImagePath)(IBIOSSettings *pThis, PRUnichar * logoImagePath);
+
+    nsresult (*GetBootMenuMode)(IBIOSSettings *pThis, PRUint32 *bootMenuMode);
+    nsresult (*SetBootMenuMode)(IBIOSSettings *pThis, PRUint32 bootMenuMode);
+
+    nsresult (*GetACPIEnabled)(IBIOSSettings *pThis, PRBool *ACPIEnabled);
+    nsresult (*SetACPIEnabled)(IBIOSSettings *pThis, PRBool ACPIEnabled);
+
+    nsresult (*GetIOAPICEnabled)(IBIOSSettings *pThis, PRBool *IOAPICEnabled);
+    nsresult (*SetIOAPICEnabled)(IBIOSSettings *pThis, PRBool IOAPICEnabled);
+
+    nsresult (*GetTimeOffset)(IBIOSSettings *pThis, PRInt64 *timeOffset);
+    nsresult (*SetTimeOffset)(IBIOSSettings *pThis, PRInt64 timeOffset);
+
+    nsresult (*GetPXEDebugEnabled)(IBIOSSettings *pThis, PRBool *PXEDebugEnabled);
+    nsresult (*SetPXEDebugEnabled)(IBIOSSettings *pThis, PRBool PXEDebugEnabled);
+
+};
+
+struct IBIOSSettings
+{
+    struct IBIOSSettings_vtbl *vtbl;
+};
+/* End of struct IBIOSSettings Declaration */
+
+
+/* Start of struct IMachine Declaration */
+#define IMACHINE_IID_STR "99404f50-dd10-40d3-889b-dd2f79f1e95e"
+#define IMACHINE_IID { \
+    0x99404f50, 0xdd10, 0x40d3, \
+    { 0x88, 0x9b, 0xdd, 0x2f, 0x79, 0xf1, 0xe9, 0x5e } \
+}
+struct IMachine_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetParent)(IMachine *pThis, IVirtualBox * *parent);
+
+    nsresult (*GetAccessible)(IMachine *pThis, PRBool *accessible);
+
+    nsresult (*GetAccessError)(IMachine *pThis, IVirtualBoxErrorInfo * *accessError);
+
+    nsresult (*GetName)(IMachine *pThis, PRUnichar * *name);
+    nsresult (*SetName)(IMachine *pThis, PRUnichar * name);
+
+    nsresult (*GetDescription)(IMachine *pThis, PRUnichar * *description);
+    nsresult (*SetDescription)(IMachine *pThis, PRUnichar * description);
+
+    nsresult (*GetId)(IMachine *pThis, PRUnichar * *id);
+
+    nsresult (*GetOSTypeId)(IMachine *pThis, PRUnichar * *OSTypeId);
+    nsresult (*SetOSTypeId)(IMachine *pThis, PRUnichar * OSTypeId);
+
+    nsresult (*GetHardwareVersion)(IMachine *pThis, PRUnichar * *HardwareVersion);
+    nsresult (*SetHardwareVersion)(IMachine *pThis, PRUnichar * HardwareVersion);
+
+    nsresult (*GetHardwareUUID)(IMachine *pThis, PRUnichar * *hardwareUUID);
+    nsresult (*SetHardwareUUID)(IMachine *pThis, PRUnichar * hardwareUUID);
+
+    nsresult (*GetCPUCount)(IMachine *pThis, PRUint32 *CPUCount);
+    nsresult (*SetCPUCount)(IMachine *pThis, PRUint32 CPUCount);
+
+    nsresult (*GetMemorySize)(IMachine *pThis, PRUint32 *memorySize);
+    nsresult (*SetMemorySize)(IMachine *pThis, PRUint32 memorySize);
+
+    nsresult (*GetMemoryBalloonSize)(IMachine *pThis, PRUint32 *memoryBalloonSize);
+    nsresult (*SetMemoryBalloonSize)(IMachine *pThis, PRUint32 memoryBalloonSize);
+
+    nsresult (*GetStatisticsUpdateInterval)(IMachine *pThis, PRUint32 *statisticsUpdateInterval);
+    nsresult (*SetStatisticsUpdateInterval)(IMachine *pThis, PRUint32 statisticsUpdateInterval);
+
+    nsresult (*GetVRAMSize)(IMachine *pThis, PRUint32 *VRAMSize);
+    nsresult (*SetVRAMSize)(IMachine *pThis, PRUint32 VRAMSize);
+
+    nsresult (*GetAccelerate3DEnabled)(IMachine *pThis, PRBool *accelerate3DEnabled);
+    nsresult (*SetAccelerate3DEnabled)(IMachine *pThis, PRBool accelerate3DEnabled);
+
+    nsresult (*GetAccelerate2DVideoEnabled)(IMachine *pThis, PRBool *accelerate2DVideoEnabled);
+    nsresult (*SetAccelerate2DVideoEnabled)(IMachine *pThis, PRBool accelerate2DVideoEnabled);
+
+    nsresult (*GetMonitorCount)(IMachine *pThis, PRUint32 *monitorCount);
+    nsresult (*SetMonitorCount)(IMachine *pThis, PRUint32 monitorCount);
+
+    nsresult (*GetBIOSSettings)(IMachine *pThis, IBIOSSettings * *BIOSSettings);
+
+    nsresult (*GetFirmwareType)(IMachine *pThis, PRUint32 *firmwareType);
+    nsresult (*SetFirmwareType)(IMachine *pThis, PRUint32 firmwareType);
+
+    nsresult (*GetSnapshotFolder)(IMachine *pThis, PRUnichar * *snapshotFolder);
+    nsresult (*SetSnapshotFolder)(IMachine *pThis, PRUnichar * snapshotFolder);
+
+    nsresult (*GetVRDPServer)(IMachine *pThis, IVRDPServer * *VRDPServer);
+
+    nsresult (*GetMediumAttachments)(IMachine *pThis, PRUint32 *mediumAttachmentsSize, IMediumAttachment * **mediumAttachments);
+
+    nsresult (*GetUSBController)(IMachine *pThis, IUSBController * *USBController);
+
+    nsresult (*GetAudioAdapter)(IMachine *pThis, IAudioAdapter * *audioAdapter);
+
+    nsresult (*GetStorageControllers)(IMachine *pThis, PRUint32 *storageControllersSize, IStorageController * **storageControllers);
+
+    nsresult (*GetSettingsFilePath)(IMachine *pThis, PRUnichar * *settingsFilePath);
+
+    nsresult (*GetSettingsModified)(IMachine *pThis, PRBool *settingsModified);
+
+    nsresult (*GetSessionState)(IMachine *pThis, PRUint32 *sessionState);
+
+    nsresult (*GetSessionType)(IMachine *pThis, PRUnichar * *sessionType);
+
+    nsresult (*GetSessionPid)(IMachine *pThis, PRUint32 *sessionPid);
+
+    nsresult (*GetState)(IMachine *pThis, PRUint32 *state);
+
+    nsresult (*GetLastStateChange)(IMachine *pThis, PRInt64 *lastStateChange);
+
+    nsresult (*GetStateFilePath)(IMachine *pThis, PRUnichar * *stateFilePath);
+
+    nsresult (*GetLogFolder)(IMachine *pThis, PRUnichar * *logFolder);
+
+    nsresult (*GetCurrentSnapshot)(IMachine *pThis, ISnapshot * *currentSnapshot);
+
+    nsresult (*GetSnapshotCount)(IMachine *pThis, PRUint32 *snapshotCount);
+
+    nsresult (*GetCurrentStateModified)(IMachine *pThis, PRBool *currentStateModified);
+
+    nsresult (*GetSharedFolders)(IMachine *pThis, PRUint32 *sharedFoldersSize, ISharedFolder * **sharedFolders);
+
+    nsresult (*GetClipboardMode)(IMachine *pThis, PRUint32 *clipboardMode);
+    nsresult (*SetClipboardMode)(IMachine *pThis, PRUint32 clipboardMode);
+
+    nsresult (*GetGuestPropertyNotificationPatterns)(IMachine *pThis, PRUnichar * *guestPropertyNotificationPatterns);
+    nsresult (*SetGuestPropertyNotificationPatterns)(IMachine *pThis, PRUnichar * guestPropertyNotificationPatterns);
+
+    nsresult (*GetTeleporterEnabled)(IMachine *pThis, PRBool *teleporterEnabled);
+    nsresult (*SetTeleporterEnabled)(IMachine *pThis, PRBool teleporterEnabled);
+
+    nsresult (*GetTeleporterPort)(IMachine *pThis, PRUint32 *teleporterPort);
+    nsresult (*SetTeleporterPort)(IMachine *pThis, PRUint32 teleporterPort);
+
+    nsresult (*GetTeleporterAddress)(IMachine *pThis, PRUnichar * *teleporterAddress);
+    nsresult (*SetTeleporterAddress)(IMachine *pThis, PRUnichar * teleporterAddress);
+
+    nsresult (*GetTeleporterPassword)(IMachine *pThis, PRUnichar * *teleporterPassword);
+    nsresult (*SetTeleporterPassword)(IMachine *pThis, PRUnichar * teleporterPassword);
+
+    nsresult (*SetBootOrder)(
+        IMachine *pThis,
+        PRUint32 position,
+        PRUint32 device
+    );
+
+    nsresult (*GetBootOrder)(
+        IMachine *pThis,
+        PRUint32 position,
+        PRUint32 * device
+    );
+
+    nsresult (*AttachDevice)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRInt32 controllerPort,
+        PRInt32 device,
+        PRUint32 type,
+        PRUnichar * id
+    );
+
+    nsresult (*DetachDevice)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRInt32 controllerPort,
+        PRInt32 device
+    );
+
+    nsresult (*PassthroughDevice)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRInt32 controllerPort,
+        PRInt32 device,
+        PRBool passthrough
+    );
+
+    nsresult (*MountMedium)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRInt32 controllerPort,
+        PRInt32 device,
+        PRUnichar * medium,
+        PRBool force
+    );
+
+    nsresult (*GetMedium)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRInt32 controllerPort,
+        PRInt32 device,
+        IMedium * * medium
+    );
+
+    nsresult (*GetMediumAttachmentsOfController)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRUint32 *mediumAttachmentsSize,
+        IMediumAttachment *** mediumAttachments
+    );
+
+    nsresult (*GetMediumAttachment)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRInt32 controllerPort,
+        PRInt32 device,
+        IMediumAttachment * * attachment
+    );
+
+    nsresult (*GetNetworkAdapter)(
+        IMachine *pThis,
+        PRUint32 slot,
+        INetworkAdapter * * adapter
+    );
+
+    nsresult (*AddStorageController)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRUint32 connectionType,
+        IStorageController * * controller
+    );
+
+    nsresult (*GetStorageControllerByName)(
+        IMachine *pThis,
+        PRUnichar * name,
+        IStorageController * * storageController
+    );
+
+    nsresult (*GetStorageControllerByInstance)(
+        IMachine *pThis,
+        PRUint32 instance,
+        IStorageController * * storageController
+    );
+
+    nsresult (*RemoveStorageController)(
+        IMachine *pThis,
+        PRUnichar * name
+    );
+
+    nsresult (*GetSerialPort)(
+        IMachine *pThis,
+        PRUint32 slot,
+        ISerialPort * * port
+    );
+
+    nsresult (*GetParallelPort)(
+        IMachine *pThis,
+        PRUint32 slot,
+        IParallelPort * * port
+    );
+
+    nsresult (*GetExtraDataKeys)(
+        IMachine *pThis,
+        PRUint32 *valueSize,
+        PRUnichar *** value
+    );
+
+    nsresult (*GetExtraData)(
+        IMachine *pThis,
+        PRUnichar * key,
+        PRUnichar * * value
+    );
+
+    nsresult (*SetExtraData)(
+        IMachine *pThis,
+        PRUnichar * key,
+        PRUnichar * value
+    );
+
+    nsresult (*GetCpuProperty)(
+        IMachine *pThis,
+        PRUint32 property,
+        PRBool * value
+    );
+
+    nsresult (*SetCpuProperty)(
+        IMachine *pThis,
+        PRUint32 property,
+        PRBool value
+    );
+
+    nsresult (*GetCpuIdLeaf)(
+        IMachine *pThis,
+        PRUint32 id,
+        PRUint32 * valEax,
+        PRUint32 * valEbx,
+        PRUint32 * valEcx,
+        PRUint32 * valEdx
+    );
+
+    nsresult (*SetCpuIdLeaf)(
+        IMachine *pThis,
+        PRUint32 id,
+        PRUint32 valEax,
+        PRUint32 valEbx,
+        PRUint32 valEcx,
+        PRUint32 valEdx
+    );
+
+    nsresult (*RemoveCpuIdLeaf)(
+        IMachine *pThis,
+        PRUint32 id
+    );
+
+    nsresult (*RemoveAllCpuIdLeafs)(IMachine *pThis );
+
+    nsresult (*GetHWVirtExProperty)(
+        IMachine *pThis,
+        PRUint32 property,
+        PRBool * value
+    );
+
+    nsresult (*SetHWVirtExProperty)(
+        IMachine *pThis,
+        PRUint32 property,
+        PRBool value
+    );
+
+    nsresult (*SaveSettings)(IMachine *pThis );
+
+    nsresult (*DiscardSettings)(IMachine *pThis );
+
+    nsresult (*DeleteSettings)(IMachine *pThis );
+
+    nsresult (*Export)(
+        IMachine *pThis,
+        IAppliance * aAppliance,
+        IVirtualSystemDescription * * aDescription
+    );
+
+    nsresult (*GetSnapshot)(
+        IMachine *pThis,
+        PRUnichar * id,
+        ISnapshot * * snapshot
+    );
+
+    nsresult (*FindSnapshot)(
+        IMachine *pThis,
+        PRUnichar * name,
+        ISnapshot * * snapshot
+    );
+
+    nsresult (*SetCurrentSnapshot)(
+        IMachine *pThis,
+        PRUnichar * id
+    );
+
+    nsresult (*CreateSharedFolder)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRUnichar * hostPath,
+        PRBool writable
+    );
+
+    nsresult (*RemoveSharedFolder)(
+        IMachine *pThis,
+        PRUnichar * name
+    );
+
+    nsresult (*CanShowConsoleWindow)(
+        IMachine *pThis,
+        PRBool * canShow
+    );
+
+    nsresult (*ShowConsoleWindow)(
+        IMachine *pThis,
+        PRUint64 * winId
+    );
+
+    nsresult (*GetGuestProperty)(
+        IMachine *pThis,
+        PRUnichar * name,
+        PRUnichar * * value,
+        PRUint64 * timestamp,
+        PRUnichar * * flags
+    );
+
+    nsresult (*GetGuestPropertyValue)(
+        IMachine *pThis,
+        PRUnichar * property,
+        PRUnichar * * value
+    );
+
+    nsresult (*GetGuestPropertyTimestamp)(
+        IMachine *pThis,
+        PRUnichar * property,
+        PRUint64 * value
+    );
+
+    nsresult (*SetGuestProperty)(
+        IMachine *pThis,
+        PRUnichar * property,
+        PRUnichar * value,
+        PRUnichar * flags
+    );
+
+    nsresult (*SetGuestPropertyValue)(
+        IMachine *pThis,
+        PRUnichar * property,
+        PRUnichar * value
+    );
+
+    nsresult (*EnumerateGuestProperties)(
+        IMachine *pThis,
+        PRUnichar * patterns,
+        PRUint32 *nameSize,
+        PRUnichar *** name,
+        PRUint32 *valueSize,
+        PRUnichar *** value,
+        PRUint32 *timestampSize,
+        PRUint64* timestamp,
+        PRUint32 *flagsSize,
+        PRUnichar *** flags
+    );
+
+    nsresult (*QuerySavedThumbnailSize)(
+        IMachine *pThis,
+        PRUint32 * size,
+        PRUint32 * width,
+        PRUint32 * height
+    );
+
+    nsresult (*ReadSavedThumbnailToArray)(
+        IMachine *pThis,
+        PRBool BGR,
+        PRUint32 * width,
+        PRUint32 * height,
+        PRUint32 *dataSize,
+        PRUint8** data
+    );
+
+    nsresult (*QuerySavedScreenshotPNGSize)(
+        IMachine *pThis,
+        PRUint32 * size,
+        PRUint32 * width,
+        PRUint32 * height
+    );
+
+    nsresult (*ReadSavedScreenshotPNGToArray)(
+        IMachine *pThis,
+        PRUint32 * width,
+        PRUint32 * height,
+        PRUint32 *dataSize,
+        PRUint8** data
+    );
+
+};
+
+struct IMachine
+{
+    struct IMachine_vtbl *vtbl;
+};
+/* End of struct IMachine Declaration */
+
+
+/* Start of struct IConsoleCallback Declaration */
+#define ICONSOLECALLBACK_IID_STR "d6239535-bda2-4ef7-83f4-f4722e4a3b2c"
+#define ICONSOLECALLBACK_IID { \
+    0xd6239535, 0xbda2, 0x4ef7, \
+    { 0x83, 0xf4, 0xf4, 0x72, 0x2e, 0x4a, 0x3b, 0x2c } \
+}
+struct IConsoleCallback_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*OnMousePointerShapeChange)(
+        IConsoleCallback *pThis,
+        PRBool visible,
+        PRBool alpha,
+        PRUint32 xHot,
+        PRUint32 yHot,
+        PRUint32 width,
+        PRUint32 height,
+        PRUint8 * shape
+    );
+
+    nsresult (*OnMouseCapabilityChange)(
+        IConsoleCallback *pThis,
+        PRBool supportsAbsolute,
+        PRBool needsHostCursor
+    );
+
+    nsresult (*OnKeyboardLedsChange)(
+        IConsoleCallback *pThis,
+        PRBool numLock,
+        PRBool capsLock,
+        PRBool scrollLock
+    );
+
+    nsresult (*OnStateChange)(
+        IConsoleCallback *pThis,
+        PRUint32 state
+    );
+
+    nsresult (*OnAdditionsStateChange)(IConsoleCallback *pThis );
+
+    nsresult (*OnNetworkAdapterChange)(
+        IConsoleCallback *pThis,
+        INetworkAdapter * networkAdapter
+    );
+
+    nsresult (*OnSerialPortChange)(
+        IConsoleCallback *pThis,
+        ISerialPort * serialPort
+    );
+
+    nsresult (*OnParallelPortChange)(
+        IConsoleCallback *pThis,
+        IParallelPort * parallelPort
+    );
+
+    nsresult (*OnStorageControllerChange)(IConsoleCallback *pThis );
+
+    nsresult (*OnMediumChange)(
+        IConsoleCallback *pThis,
+        IMediumAttachment * mediumAttachment
+    );
+
+    nsresult (*OnVRDPServerChange)(IConsoleCallback *pThis );
+
+    nsresult (*OnRemoteDisplayInfoChange)(IConsoleCallback *pThis );
+
+    nsresult (*OnUSBControllerChange)(IConsoleCallback *pThis );
+
+    nsresult (*OnUSBDeviceStateChange)(
+        IConsoleCallback *pThis,
+        IUSBDevice * device,
+        PRBool attached,
+        IVirtualBoxErrorInfo * error
+    );
+
+    nsresult (*OnSharedFolderChange)(
+        IConsoleCallback *pThis,
+        PRUint32 scope
+    );
+
+    nsresult (*OnRuntimeError)(
+        IConsoleCallback *pThis,
+        PRBool fatal,
+        PRUnichar * id,
+        PRUnichar * message
+    );
+
+    nsresult (*OnCanShowWindow)(
+        IConsoleCallback *pThis,
+        PRBool * canShow
+    );
+
+    nsresult (*OnShowWindow)(
+        IConsoleCallback *pThis,
+        PRUint64 * winId
+    );
+
+};
+
+struct IConsoleCallback
+{
+    struct IConsoleCallback_vtbl *vtbl;
+};
+/* End of struct IConsoleCallback Declaration */
+
+
+/* Start of struct IRemoteDisplayInfo Declaration */
+#define IREMOTEDISPLAYINFO_IID_STR "b3741084-806f-4c3b-8c42-ebad1a81e45a"
+#define IREMOTEDISPLAYINFO_IID { \
+    0xb3741084, 0x806f, 0x4c3b, \
+    { 0x8c, 0x42, 0xeb, 0xad, 0x1a, 0x81, 0xe4, 0x5a } \
+}
+struct IRemoteDisplayInfo_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetActive)(IRemoteDisplayInfo *pThis, PRBool *active);
+
+    nsresult (*GetPort)(IRemoteDisplayInfo *pThis, PRInt32 *port);
+
+    nsresult (*GetNumberOfClients)(IRemoteDisplayInfo *pThis, PRUint32 *numberOfClients);
+
+    nsresult (*GetBeginTime)(IRemoteDisplayInfo *pThis, PRInt64 *beginTime);
+
+    nsresult (*GetEndTime)(IRemoteDisplayInfo *pThis, PRInt64 *endTime);
+
+    nsresult (*GetBytesSent)(IRemoteDisplayInfo *pThis, PRUint64 *bytesSent);
+
+    nsresult (*GetBytesSentTotal)(IRemoteDisplayInfo *pThis, PRUint64 *bytesSentTotal);
+
+    nsresult (*GetBytesReceived)(IRemoteDisplayInfo *pThis, PRUint64 *bytesReceived);
+
+    nsresult (*GetBytesReceivedTotal)(IRemoteDisplayInfo *pThis, PRUint64 *bytesReceivedTotal);
+
+    nsresult (*GetUser)(IRemoteDisplayInfo *pThis, PRUnichar * *user);
+
+    nsresult (*GetDomain)(IRemoteDisplayInfo *pThis, PRUnichar * *domain);
+
+    nsresult (*GetClientName)(IRemoteDisplayInfo *pThis, PRUnichar * *clientName);
+
+    nsresult (*GetClientIP)(IRemoteDisplayInfo *pThis, PRUnichar * *clientIP);
+
+    nsresult (*GetClientVersion)(IRemoteDisplayInfo *pThis, PRUint32 *clientVersion);
+
+    nsresult (*GetEncryptionStyle)(IRemoteDisplayInfo *pThis, PRUint32 *encryptionStyle);
+
+};
+
+struct IRemoteDisplayInfo
+{
+    struct IRemoteDisplayInfo_vtbl *vtbl;
+};
+/* End of struct IRemoteDisplayInfo Declaration */
+
+
+/* Start of struct IConsole Declaration */
+#define ICONSOLE_IID_STR "55dd56a5-1d1d-4d81-b742-b082b9571be6"
+#define ICONSOLE_IID { \
+    0x55dd56a5, 0x1d1d, 0x4d81, \
+    { 0xb7, 0x42, 0xb0, 0x82, 0xb9, 0x57, 0x1b, 0xe6 } \
+}
+struct IConsole_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetMachine)(IConsole *pThis, IMachine * *machine);
+
+    nsresult (*GetState)(IConsole *pThis, PRUint32 *state);
+
+    nsresult (*GetGuest)(IConsole *pThis, IGuest * *guest);
+
+    nsresult (*GetKeyboard)(IConsole *pThis, IKeyboard * *keyboard);
+
+    nsresult (*GetMouse)(IConsole *pThis, IMouse * *mouse);
+
+    nsresult (*GetDisplay)(IConsole *pThis, IDisplay * *display);
+
+    nsresult (*GetDebugger)(IConsole *pThis, IMachineDebugger * *debugger);
+
+    nsresult (*GetUSBDevices)(IConsole *pThis, PRUint32 *USBDevicesSize, IUSBDevice * **USBDevices);
+
+    nsresult (*GetRemoteUSBDevices)(IConsole *pThis, PRUint32 *remoteUSBDevicesSize, IHostUSBDevice * **remoteUSBDevices);
+
+    nsresult (*GetSharedFolders)(IConsole *pThis, PRUint32 *sharedFoldersSize, ISharedFolder * **sharedFolders);
+
+    nsresult (*GetRemoteDisplayInfo)(IConsole *pThis, IRemoteDisplayInfo * *remoteDisplayInfo);
+
+    nsresult (*PowerUp)(
+        IConsole *pThis,
+        IProgress * * progress
+    );
+
+    nsresult (*PowerUpPaused)(
+        IConsole *pThis,
+        IProgress * * progress
+    );
+
+    nsresult (*PowerDown)(
+        IConsole *pThis,
+        IProgress * * progress
+    );
+
+    nsresult (*Reset)(IConsole *pThis );
+
+    nsresult (*Pause)(IConsole *pThis );
+
+    nsresult (*Resume)(IConsole *pThis );
+
+    nsresult (*PowerButton)(IConsole *pThis );
+
+    nsresult (*SleepButton)(IConsole *pThis );
+
+    nsresult (*GetPowerButtonHandled)(
+        IConsole *pThis,
+        PRBool * handled
+    );
+
+    nsresult (*GetGuestEnteredACPIMode)(
+        IConsole *pThis,
+        PRBool * entered
+    );
+
+    nsresult (*SaveState)(
+        IConsole *pThis,
+        IProgress * * progress
+    );
+
+    nsresult (*AdoptSavedState)(
+        IConsole *pThis,
+        PRUnichar * savedStateFile
+    );
+
+    nsresult (*ForgetSavedState)(
+        IConsole *pThis,
+        PRBool remove
+    );
+
+    nsresult (*GetDeviceActivity)(
+        IConsole *pThis,
+        PRUint32 type,
+        PRUint32 * activity
+    );
+
+    nsresult (*AttachUSBDevice)(
+        IConsole *pThis,
+        PRUnichar * id
+    );
+
+    nsresult (*DetachUSBDevice)(
+        IConsole *pThis,
+        PRUnichar * id,
+        IUSBDevice * * device
+    );
+
+    nsresult (*FindUSBDeviceByAddress)(
+        IConsole *pThis,
+        PRUnichar * name,
+        IUSBDevice * * device
+    );
+
+    nsresult (*FindUSBDeviceById)(
+        IConsole *pThis,
+        PRUnichar * id,
+        IUSBDevice * * device
+    );
+
+    nsresult (*CreateSharedFolder)(
+        IConsole *pThis,
+        PRUnichar * name,
+        PRUnichar * hostPath,
+        PRBool writable
+    );
+
+    nsresult (*RemoveSharedFolder)(
+        IConsole *pThis,
+        PRUnichar * name
+    );
+
+    nsresult (*TakeSnapshot)(
+        IConsole *pThis,
+        PRUnichar * name,
+        PRUnichar * description,
+        IProgress * * progress
+    );
+
+    nsresult (*DeleteSnapshot)(
+        IConsole *pThis,
+        PRUnichar * id,
+        IProgress * * progress
+    );
+
+    nsresult (*RestoreSnapshot)(
+        IConsole *pThis,
+        ISnapshot * snapshot,
+        IProgress * * progress
+    );
+
+    nsresult (*Teleport)(
+        IConsole *pThis,
+        PRUnichar * hostname,
+        PRUint32 tcpport,
+        PRUnichar * password,
+        IProgress * * progress
+    );
+
+    nsresult (*RegisterCallback)(
+        IConsole *pThis,
+        IConsoleCallback * callback
+    );
+
+    nsresult (*UnregisterCallback)(
+        IConsole *pThis,
+        IConsoleCallback * callback
+    );
+
+};
+
+struct IConsole
+{
+    struct IConsole_vtbl *vtbl;
+};
+/* End of struct IConsole Declaration */
+
+
+/* Start of struct IHostNetworkInterface Declaration */
+#define IHOSTNETWORKINTERFACE_IID_STR "ce6fae58-7642-4102-b5db-c9005c2320a8"
+#define IHOSTNETWORKINTERFACE_IID { \
+    0xce6fae58, 0x7642, 0x4102, \
+    { 0xb5, 0xdb, 0xc9, 0x00, 0x5c, 0x23, 0x20, 0xa8 } \
+}
+struct IHostNetworkInterface_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetName)(IHostNetworkInterface *pThis, PRUnichar * *name);
+
+    nsresult (*GetId)(IHostNetworkInterface *pThis, PRUnichar * *id);
+
+    nsresult (*GetNetworkName)(IHostNetworkInterface *pThis, PRUnichar * *networkName);
+
+    nsresult (*GetDhcpEnabled)(IHostNetworkInterface *pThis, PRBool *dhcpEnabled);
+
+    nsresult (*GetIPAddress)(IHostNetworkInterface *pThis, PRUnichar * *IPAddress);
+
+    nsresult (*GetNetworkMask)(IHostNetworkInterface *pThis, PRUnichar * *networkMask);
+
+    nsresult (*GetIPV6Supported)(IHostNetworkInterface *pThis, PRBool *IPV6Supported);
+
+    nsresult (*GetIPV6Address)(IHostNetworkInterface *pThis, PRUnichar * *IPV6Address);
+
+    nsresult (*GetIPV6NetworkMaskPrefixLength)(IHostNetworkInterface *pThis, PRUint32 *IPV6NetworkMaskPrefixLength);
+
+    nsresult (*GetHardwareAddress)(IHostNetworkInterface *pThis, PRUnichar * *hardwareAddress);
+
+    nsresult (*GetMediumType)(IHostNetworkInterface *pThis, PRUint32 *mediumType);
+
+    nsresult (*GetStatus)(IHostNetworkInterface *pThis, PRUint32 *status);
+
+    nsresult (*GetInterfaceType)(IHostNetworkInterface *pThis, PRUint32 *interfaceType);
+
+    nsresult (*EnableStaticIpConfig)(
+        IHostNetworkInterface *pThis,
+        PRUnichar * IPAddress,
+        PRUnichar * networkMask
+    );
+
+    nsresult (*EnableStaticIpConfigV6)(
+        IHostNetworkInterface *pThis,
+        PRUnichar * IPV6Address,
+        PRUint32 IPV6NetworkMaskPrefixLength
+    );
+
+    nsresult (*EnableDynamicIpConfig)(IHostNetworkInterface *pThis );
+
+    nsresult (*DhcpRediscover)(IHostNetworkInterface *pThis );
+
+};
+
+struct IHostNetworkInterface
+{
+    struct IHostNetworkInterface_vtbl *vtbl;
+};
+/* End of struct IHostNetworkInterface Declaration */
+
+
+/* Start of struct IHost Declaration */
+#define IHOST_IID_STR "e380cbfc-ae65-4fa6-899e-45ded6b3132a"
+#define IHOST_IID { \
+    0xe380cbfc, 0xae65, 0x4fa6, \
+    { 0x89, 0x9e, 0x45, 0xde, 0xd6, 0xb3, 0x13, 0x2a } \
+}
+struct IHost_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetDVDDrives)(IHost *pThis, PRUint32 *DVDDrivesSize, IMedium * **DVDDrives);
+
+    nsresult (*GetFloppyDrives)(IHost *pThis, PRUint32 *floppyDrivesSize, IMedium * **floppyDrives);
+
+    nsresult (*GetUSBDevices)(IHost *pThis, PRUint32 *USBDevicesSize, IHostUSBDevice * **USBDevices);
+
+    nsresult (*GetUSBDeviceFilters)(IHost *pThis, PRUint32 *USBDeviceFiltersSize, IHostUSBDeviceFilter * **USBDeviceFilters);
+
+    nsresult (*GetNetworkInterfaces)(IHost *pThis, PRUint32 *networkInterfacesSize, IHostNetworkInterface * **networkInterfaces);
+
+    nsresult (*GetProcessorCount)(IHost *pThis, PRUint32 *processorCount);
+
+    nsresult (*GetProcessorOnlineCount)(IHost *pThis, PRUint32 *processorOnlineCount);
+
+    nsresult (*GetMemorySize)(IHost *pThis, PRUint32 *memorySize);
+
+    nsresult (*GetMemoryAvailable)(IHost *pThis, PRUint32 *memoryAvailable);
+
+    nsresult (*GetOperatingSystem)(IHost *pThis, PRUnichar * *operatingSystem);
+
+    nsresult (*GetOSVersion)(IHost *pThis, PRUnichar * *OSVersion);
+
+    nsresult (*GetUTCTime)(IHost *pThis, PRInt64 *UTCTime);
+
+    nsresult (*GetAcceleration3DAvailable)(IHost *pThis, PRBool *Acceleration3DAvailable);
+
+    nsresult (*GetProcessorSpeed)(
+        IHost *pThis,
+        PRUint32 cpuId,
+        PRUint32 * speed
+    );
+
+    nsresult (*GetProcessorFeature)(
+        IHost *pThis,
+        PRUint32 feature,
+        PRBool * supported
+    );
+
+    nsresult (*GetProcessorDescription)(
+        IHost *pThis,
+        PRUint32 cpuId,
+        PRUnichar * * description
+    );
+
+    nsresult (*GetProcessorCpuIdLeaf)(
+        IHost *pThis,
+        PRUint32 cpuId,
+        PRUint32 leaf,
+        PRUint32 subLeaf,
+        PRUint32 * valEax,
+        PRUint32 * valEbx,
+        PRUint32 * valEcx,
+        PRUint32 * valEdx
+    );
+
+    nsresult (*CreateHostOnlyNetworkInterface)(
+        IHost *pThis,
+        IHostNetworkInterface * * hostInterface,
+        IProgress * * progress
+    );
+
+    nsresult (*RemoveHostOnlyNetworkInterface)(
+        IHost *pThis,
+        PRUnichar * id,
+        IProgress * * progress
+    );
+
+    nsresult (*CreateUSBDeviceFilter)(
+        IHost *pThis,
+        PRUnichar * name,
+        IHostUSBDeviceFilter * * filter
+    );
+
+    nsresult (*InsertUSBDeviceFilter)(
+        IHost *pThis,
+        PRUint32 position,
+        IHostUSBDeviceFilter * filter
+    );
+
+    nsresult (*RemoveUSBDeviceFilter)(
+        IHost *pThis,
+        PRUint32 position
+    );
+
+    nsresult (*FindHostDVDDrive)(
+        IHost *pThis,
+        PRUnichar * name,
+        IMedium * * drive
+    );
+
+    nsresult (*FindHostFloppyDrive)(
+        IHost *pThis,
+        PRUnichar * name,
+        IMedium * * drive
+    );
+
+    nsresult (*FindHostNetworkInterfaceByName)(
+        IHost *pThis,
+        PRUnichar * name,
+        IHostNetworkInterface * * networkInterface
+    );
+
+    nsresult (*FindHostNetworkInterfaceById)(
+        IHost *pThis,
+        PRUnichar * id,
+        IHostNetworkInterface * * networkInterface
+    );
+
+    nsresult (*FindHostNetworkInterfacesOfType)(
+        IHost *pThis,
+        PRUint32 type,
+        PRUint32 *networkInterfacesSize,
+        IHostNetworkInterface *** networkInterfaces
+    );
+
+    nsresult (*FindUSBDeviceById)(
+        IHost *pThis,
+        PRUnichar * id,
+        IHostUSBDevice * * device
+    );
+
+    nsresult (*FindUSBDeviceByAddress)(
+        IHost *pThis,
+        PRUnichar * name,
+        IHostUSBDevice * * device
+    );
+
+};
+
+struct IHost
+{
+    struct IHost_vtbl *vtbl;
+};
+/* End of struct IHost Declaration */
+
+
+/* Start of struct ISystemProperties Declaration */
+#define ISYSTEMPROPERTIES_IID_STR "8030645c-8fef-4320-bb7b-c829f00069dc"
+#define ISYSTEMPROPERTIES_IID { \
+    0x8030645c, 0x8fef, 0x4320, \
+    { 0xbb, 0x7b, 0xc8, 0x29, 0xf0, 0x00, 0x69, 0xdc } \
+}
+struct ISystemProperties_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetMinGuestRAM)(ISystemProperties *pThis, PRUint32 *minGuestRAM);
+
+    nsresult (*GetMaxGuestRAM)(ISystemProperties *pThis, PRUint32 *maxGuestRAM);
+
+    nsresult (*GetMinGuestVRAM)(ISystemProperties *pThis, PRUint32 *minGuestVRAM);
+
+    nsresult (*GetMaxGuestVRAM)(ISystemProperties *pThis, PRUint32 *maxGuestVRAM);
+
+    nsresult (*GetMinGuestCPUCount)(ISystemProperties *pThis, PRUint32 *minGuestCPUCount);
+
+    nsresult (*GetMaxGuestCPUCount)(ISystemProperties *pThis, PRUint32 *maxGuestCPUCount);
+
+    nsresult (*GetMaxVDISize)(ISystemProperties *pThis, PRUint64 *maxVDISize);
+
+    nsresult (*GetNetworkAdapterCount)(ISystemProperties *pThis, PRUint32 *networkAdapterCount);
+
+    nsresult (*GetSerialPortCount)(ISystemProperties *pThis, PRUint32 *serialPortCount);
+
+    nsresult (*GetParallelPortCount)(ISystemProperties *pThis, PRUint32 *parallelPortCount);
+
+    nsresult (*GetMaxBootPosition)(ISystemProperties *pThis, PRUint32 *maxBootPosition);
+
+    nsresult (*GetDefaultMachineFolder)(ISystemProperties *pThis, PRUnichar * *defaultMachineFolder);
+    nsresult (*SetDefaultMachineFolder)(ISystemProperties *pThis, PRUnichar * defaultMachineFolder);
+
+    nsresult (*GetDefaultHardDiskFolder)(ISystemProperties *pThis, PRUnichar * *defaultHardDiskFolder);
+    nsresult (*SetDefaultHardDiskFolder)(ISystemProperties *pThis, PRUnichar * defaultHardDiskFolder);
+
+    nsresult (*GetMediumFormats)(ISystemProperties *pThis, PRUint32 *mediumFormatsSize, IMediumFormat * **mediumFormats);
+
+    nsresult (*GetDefaultHardDiskFormat)(ISystemProperties *pThis, PRUnichar * *defaultHardDiskFormat);
+    nsresult (*SetDefaultHardDiskFormat)(ISystemProperties *pThis, PRUnichar * defaultHardDiskFormat);
+
+    nsresult (*GetRemoteDisplayAuthLibrary)(ISystemProperties *pThis, PRUnichar * *remoteDisplayAuthLibrary);
+    nsresult (*SetRemoteDisplayAuthLibrary)(ISystemProperties *pThis, PRUnichar * remoteDisplayAuthLibrary);
+
+    nsresult (*GetWebServiceAuthLibrary)(ISystemProperties *pThis, PRUnichar * *webServiceAuthLibrary);
+    nsresult (*SetWebServiceAuthLibrary)(ISystemProperties *pThis, PRUnichar * webServiceAuthLibrary);
+
+    nsresult (*GetLogHistoryCount)(ISystemProperties *pThis, PRUint32 *LogHistoryCount);
+    nsresult (*SetLogHistoryCount)(ISystemProperties *pThis, PRUint32 LogHistoryCount);
+
+    nsresult (*GetDefaultAudioDriver)(ISystemProperties *pThis, PRUint32 *defaultAudioDriver);
+
+    nsresult (*GetMaxDevicesPerPortForStorageBus)(
+        ISystemProperties *pThis,
+        PRUint32 bus,
+        PRUint32 * maxDevicesPerPort
+    );
+
+    nsresult (*GetMinPortCountForStorageBus)(
+        ISystemProperties *pThis,
+        PRUint32 bus,
+        PRUint32 * minPortCount
+    );
+
+    nsresult (*GetMaxPortCountForStorageBus)(
+        ISystemProperties *pThis,
+        PRUint32 bus,
+        PRUint32 * maxPortCount
+    );
+
+    nsresult (*GetMaxInstancesOfStorageBus)(
+        ISystemProperties *pThis,
+        PRUint32 bus,
+        PRUint32 * maxInstances
+    );
+
+    nsresult (*GetDeviceTypesForStorageBus)(
+        ISystemProperties *pThis,
+        PRUint32 bus,
+        PRUint32 *deviceTypesSize,
+        PRUint32** deviceTypes
+    );
+
+};
+
+struct ISystemProperties
+{
+    struct ISystemProperties_vtbl *vtbl;
+};
+/* End of struct ISystemProperties Declaration */
+
+
+/* Start of struct IGuestOSType Declaration */
+#define IGUESTOSTYPE_IID_STR "cfe9e64c-4430-435b-9e7c-e3d8e417bd58"
+#define IGUESTOSTYPE_IID { \
+    0xcfe9e64c, 0x4430, 0x435b, \
+    { 0x9e, 0x7c, 0xe3, 0xd8, 0xe4, 0x17, 0xbd, 0x58 } \
+}
+struct IGuestOSType_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetFamilyId)(IGuestOSType *pThis, PRUnichar * *familyId);
+
+    nsresult (*GetFamilyDescription)(IGuestOSType *pThis, PRUnichar * *familyDescription);
+
+    nsresult (*GetId)(IGuestOSType *pThis, PRUnichar * *id);
+
+    nsresult (*GetDescription)(IGuestOSType *pThis, PRUnichar * *description);
+
+    nsresult (*GetIs64Bit)(IGuestOSType *pThis, PRBool *is64Bit);
+
+    nsresult (*GetRecommendedIOAPIC)(IGuestOSType *pThis, PRBool *recommendedIOAPIC);
+
+    nsresult (*GetRecommendedVirtEx)(IGuestOSType *pThis, PRBool *recommendedVirtEx);
+
+    nsresult (*GetRecommendedRAM)(IGuestOSType *pThis, PRUint32 *recommendedRAM);
+
+    nsresult (*GetRecommendedVRAM)(IGuestOSType *pThis, PRUint32 *recommendedVRAM);
+
+    nsresult (*GetRecommendedHDD)(IGuestOSType *pThis, PRUint32 *recommendedHDD);
+
+    nsresult (*GetAdapterType)(IGuestOSType *pThis, PRUint32 *adapterType);
+
+};
+
+struct IGuestOSType
+{
+    struct IGuestOSType_vtbl *vtbl;
+};
+/* End of struct IGuestOSType Declaration */
+
+
+/* Start of struct IGuest Declaration */
+#define IGUEST_IID_STR "d8556fca-81bc-12af-fca3-365528fa38ca"
+#define IGUEST_IID { \
+    0xd8556fca, 0x81bc, 0x12af, \
+    { 0xfc, 0xa3, 0x36, 0x55, 0x28, 0xfa, 0x38, 0xca } \
+}
+struct IGuest_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetOSTypeId)(IGuest *pThis, PRUnichar * *OSTypeId);
+
+    nsresult (*GetAdditionsActive)(IGuest *pThis, PRBool *additionsActive);
+
+    nsresult (*GetAdditionsVersion)(IGuest *pThis, PRUnichar * *additionsVersion);
+
+    nsresult (*GetSupportsSeamless)(IGuest *pThis, PRBool *supportsSeamless);
+
+    nsresult (*GetSupportsGraphics)(IGuest *pThis, PRBool *supportsGraphics);
+
+    nsresult (*GetMemoryBalloonSize)(IGuest *pThis, PRUint32 *memoryBalloonSize);
+    nsresult (*SetMemoryBalloonSize)(IGuest *pThis, PRUint32 memoryBalloonSize);
+
+    nsresult (*GetStatisticsUpdateInterval)(IGuest *pThis, PRUint32 *statisticsUpdateInterval);
+    nsresult (*SetStatisticsUpdateInterval)(IGuest *pThis, PRUint32 statisticsUpdateInterval);
+
+    nsresult (*SetCredentials)(
+        IGuest *pThis,
+        PRUnichar * userName,
+        PRUnichar * password,
+        PRUnichar * domain,
+        PRBool allowInteractiveLogon
+    );
+
+    nsresult (*GetStatistic)(
+        IGuest *pThis,
+        PRUint32 cpuId,
+        PRUint32 statistic,
+        PRUint32 * statVal
+    );
+
+};
+
+struct IGuest
+{
+    struct IGuest_vtbl *vtbl;
+};
+/* End of struct IGuest Declaration */
+
+
+/* Start of struct IProgress Declaration */
+#define IPROGRESS_IID_STR "62827ef1-c098-40eb-be96-36d0508489a6"
+#define IPROGRESS_IID { \
+    0x62827ef1, 0xc098, 0x40eb, \
+    { 0xbe, 0x96, 0x36, 0xd0, 0x50, 0x84, 0x89, 0xa6 } \
+}
+struct IProgress_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetId)(IProgress *pThis, PRUnichar * *id);
+
+    nsresult (*GetDescription)(IProgress *pThis, PRUnichar * *description);
+
+    nsresult (*GetInitiator)(IProgress *pThis, nsISupports * *initiator);
+
+    nsresult (*GetCancelable)(IProgress *pThis, PRBool *cancelable);
+
+    nsresult (*GetPercent)(IProgress *pThis, PRUint32 *percent);
+
+    nsresult (*GetTimeRemaining)(IProgress *pThis, PRInt32 *timeRemaining);
+
+    nsresult (*GetCompleted)(IProgress *pThis, PRBool *completed);
+
+    nsresult (*GetCanceled)(IProgress *pThis, PRBool *canceled);
+
+    nsresult (*GetResultCode)(IProgress *pThis, PRInt32 *resultCode);
+
+    nsresult (*GetErrorInfo)(IProgress *pThis, IVirtualBoxErrorInfo * *errorInfo);
+
+    nsresult (*GetOperationCount)(IProgress *pThis, PRUint32 *operationCount);
+
+    nsresult (*GetOperation)(IProgress *pThis, PRUint32 *operation);
+
+    nsresult (*GetOperationDescription)(IProgress *pThis, PRUnichar * *operationDescription);
+
+    nsresult (*GetOperationPercent)(IProgress *pThis, PRUint32 *operationPercent);
+
+    nsresult (*SetCurrentOperationProgress)(
+        IProgress *pThis,
+        PRUint32 percent
+    );
+
+    nsresult (*SetNextOperation)(
+        IProgress *pThis,
+        PRUnichar * nextOperationDescription,
+        PRUint32 nextOperationsWeight
+    );
+
+    nsresult (*WaitForCompletion)(
+        IProgress *pThis,
+        PRInt32 timeout
+    );
+
+    nsresult (*WaitForOperationCompletion)(
+        IProgress *pThis,
+        PRUint32 operation,
+        PRInt32 timeout
+    );
+
+    nsresult (*Cancel)(IProgress *pThis );
+
+};
+
+struct IProgress
+{
+    struct IProgress_vtbl *vtbl;
+};
+/* End of struct IProgress Declaration */
+
+
+/* Start of struct ISnapshot Declaration */
+#define ISNAPSHOT_IID_STR "1a2d0551-58a4-4107-857e-ef414fc42ffc"
+#define ISNAPSHOT_IID { \
+    0x1a2d0551, 0x58a4, 0x4107, \
+    { 0x85, 0x7e, 0xef, 0x41, 0x4f, 0xc4, 0x2f, 0xfc } \
+}
+struct ISnapshot_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetId)(ISnapshot *pThis, PRUnichar * *id);
+
+    nsresult (*GetName)(ISnapshot *pThis, PRUnichar * *name);
+    nsresult (*SetName)(ISnapshot *pThis, PRUnichar * name);
+
+    nsresult (*GetDescription)(ISnapshot *pThis, PRUnichar * *description);
+    nsresult (*SetDescription)(ISnapshot *pThis, PRUnichar * description);
+
+    nsresult (*GetTimeStamp)(ISnapshot *pThis, PRInt64 *timeStamp);
+
+    nsresult (*GetOnline)(ISnapshot *pThis, PRBool *online);
+
+    nsresult (*GetMachine)(ISnapshot *pThis, IMachine * *machine);
+
+    nsresult (*GetParent)(ISnapshot *pThis, ISnapshot * *parent);
+
+    nsresult (*GetChildren)(ISnapshot *pThis, PRUint32 *childrenSize, ISnapshot * **children);
+
+};
+
+struct ISnapshot
+{
+    struct ISnapshot_vtbl *vtbl;
+};
+/* End of struct ISnapshot Declaration */
+
+
+/* Start of struct IMediumAttachment Declaration */
+#define IMEDIUMATTACHMENT_IID_STR "e58eb3eb-8627-428b-bdf8-34487c848de5"
+#define IMEDIUMATTACHMENT_IID { \
+    0xe58eb3eb, 0x8627, 0x428b, \
+    { 0xbd, 0xf8, 0x34, 0x48, 0x7c, 0x84, 0x8d, 0xe5 } \
+}
+struct IMediumAttachment_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetMedium)(IMediumAttachment *pThis, IMedium * *medium);
+
+    nsresult (*GetController)(IMediumAttachment *pThis, PRUnichar * *controller);
+
+    nsresult (*GetPort)(IMediumAttachment *pThis, PRInt32 *port);
+
+    nsresult (*GetDevice)(IMediumAttachment *pThis, PRInt32 *device);
+
+    nsresult (*GetType)(IMediumAttachment *pThis, PRUint32 *type);
+
+    nsresult (*GetPassthrough)(IMediumAttachment *pThis, PRBool *passthrough);
+
+};
+
+struct IMediumAttachment
+{
+    struct IMediumAttachment_vtbl *vtbl;
+};
+/* End of struct IMediumAttachment Declaration */
+
+
+/* Start of struct IMedium Declaration */
+#define IMEDIUM_IID_STR "aa8167ba-df72-4738-b740-9b84377ba9f1"
+#define IMEDIUM_IID { \
+    0xaa8167ba, 0xdf72, 0x4738, \
+    { 0xb7, 0x40, 0x9b, 0x84, 0x37, 0x7b, 0xa9, 0xf1 } \
+}
+struct IMedium_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetId)(IMedium *pThis, PRUnichar * *id);
+
+    nsresult (*GetDescription)(IMedium *pThis, PRUnichar * *description);
+    nsresult (*SetDescription)(IMedium *pThis, PRUnichar * description);
+
+    nsresult (*GetState)(IMedium *pThis, PRUint32 *state);
+
+    nsresult (*GetLocation)(IMedium *pThis, PRUnichar * *location);
+    nsresult (*SetLocation)(IMedium *pThis, PRUnichar * location);
+
+    nsresult (*GetName)(IMedium *pThis, PRUnichar * *name);
+
+    nsresult (*GetDeviceType)(IMedium *pThis, PRUint32 *deviceType);
+
+    nsresult (*GetHostDrive)(IMedium *pThis, PRBool *hostDrive);
+
+    nsresult (*GetSize)(IMedium *pThis, PRUint64 *size);
+
+    nsresult (*GetFormat)(IMedium *pThis, PRUnichar * *format);
+
+    nsresult (*GetType)(IMedium *pThis, PRUint32 *type);
+    nsresult (*SetType)(IMedium *pThis, PRUint32 type);
+
+    nsresult (*GetParent)(IMedium *pThis, IMedium * *parent);
+
+    nsresult (*GetChildren)(IMedium *pThis, PRUint32 *childrenSize, IMedium * **children);
+
+    nsresult (*GetBase)(IMedium *pThis, IMedium * *base);
+
+    nsresult (*GetReadOnly)(IMedium *pThis, PRBool *readOnly);
+
+    nsresult (*GetLogicalSize)(IMedium *pThis, PRUint64 *logicalSize);
+
+    nsresult (*GetAutoReset)(IMedium *pThis, PRBool *autoReset);
+    nsresult (*SetAutoReset)(IMedium *pThis, PRBool autoReset);
+
+    nsresult (*GetLastAccessError)(IMedium *pThis, PRUnichar * *lastAccessError);
+
+    nsresult (*GetMachineIds)(IMedium *pThis, PRUint32 *machineIdsSize, PRUnichar * **machineIds);
+
+    nsresult (*RefreshState)(
+        IMedium *pThis,
+        PRUint32 * state
+    );
+
+    nsresult (*GetSnapshotIds)(
+        IMedium *pThis,
+        PRUnichar * machineId,
+        PRUint32 *snapshotIdsSize,
+        PRUnichar *** snapshotIds
+    );
+
+    nsresult (*LockRead)(
+        IMedium *pThis,
+        PRUint32 * state
+    );
+
+    nsresult (*UnlockRead)(
+        IMedium *pThis,
+        PRUint32 * state
+    );
+
+    nsresult (*LockWrite)(
+        IMedium *pThis,
+        PRUint32 * state
+    );
+
+    nsresult (*UnlockWrite)(
+        IMedium *pThis,
+        PRUint32 * state
+    );
+
+    nsresult (*Close)(IMedium *pThis );
+
+    nsresult (*GetProperty)(
+        IMedium *pThis,
+        PRUnichar * name,
+        PRUnichar * * value
+    );
+
+    nsresult (*SetProperty)(
+        IMedium *pThis,
+        PRUnichar * name,
+        PRUnichar * value
+    );
+
+    nsresult (*GetProperties)(
+        IMedium *pThis,
+        PRUnichar * names,
+        PRUint32 *returnNamesSize,
+        PRUnichar *** returnNames,
+        PRUint32 *returnValuesSize,
+        PRUnichar *** returnValues
+    );
+
+    nsresult (*SetProperties)(
+        IMedium *pThis,
+        PRUint32 namesSize,
+        PRUnichar ** names,
+        PRUint32 valuesSize,
+        PRUnichar ** values
+    );
+
+    nsresult (*CreateBaseStorage)(
+        IMedium *pThis,
+        PRUint64 logicalSize,
+        PRUint32 variant,
+        IProgress * * progress
+    );
+
+    nsresult (*DeleteStorage)(
+        IMedium *pThis,
+        IProgress * * progress
+    );
+
+    nsresult (*CreateDiffStorage)(
+        IMedium *pThis,
+        IMedium * target,
+        PRUint32 variant,
+        IProgress * * progress
+    );
+
+    nsresult (*MergeTo)(
+        IMedium *pThis,
+        PRUnichar * targetId,
+        IProgress * * progress
+    );
+
+    nsresult (*CloneTo)(
+        IMedium *pThis,
+        IMedium * target,
+        PRUint32 variant,
+        IMedium * parent,
+        IProgress * * progress
+    );
+
+    nsresult (*Compact)(
+        IMedium *pThis,
+        IProgress * * progress
+    );
+
+    nsresult (*Resize)(
+        IMedium *pThis,
+        PRUint64 logicalSize,
+        IProgress * * progress
+    );
+
+    nsresult (*Reset)(
+        IMedium *pThis,
+        IProgress * * progress
+    );
+
+};
+
+struct IMedium
+{
+    struct IMedium_vtbl *vtbl;
+};
+/* End of struct IMedium Declaration */
+
+
+/* Start of struct IMediumFormat Declaration */
+#define IMEDIUMFORMAT_IID_STR "89f52554-d469-4799-9fad-1705e86a08b1"
+#define IMEDIUMFORMAT_IID { \
+    0x89f52554, 0xd469, 0x4799, \
+    { 0x9f, 0xad, 0x17, 0x05, 0xe8, 0x6a, 0x08, 0xb1 } \
+}
+struct IMediumFormat_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetId)(IMediumFormat *pThis, PRUnichar * *id);
+
+    nsresult (*GetName)(IMediumFormat *pThis, PRUnichar * *name);
+
+    nsresult (*GetFileExtensions)(IMediumFormat *pThis, PRUint32 *fileExtensionsSize, PRUnichar * **fileExtensions);
+
+    nsresult (*GetCapabilities)(IMediumFormat *pThis, PRUint32 *capabilities);
+
+    nsresult (*DescribeProperties)(
+        IMediumFormat *pThis,
+        PRUint32 *namesSize,
+        PRUnichar *** names,
+        PRUint32 *descriptionSize,
+        PRUnichar *** description,
+        PRUint32 *typesSize,
+        PRUint32* types,
+        PRUint32 *flagsSize,
+        PRUint32* flags,
+        PRUint32 *defaultsSize,
+        PRUnichar *** defaults
+    );
+
+};
+
+struct IMediumFormat
+{
+    struct IMediumFormat_vtbl *vtbl;
+};
+/* End of struct IMediumFormat Declaration */
+
+
+/* Start of struct IKeyboard Declaration */
+#define IKEYBOARD_IID_STR "2d1a531b-4c6e-49cc-8af6-5c857b78b5d7"
+#define IKEYBOARD_IID { \
+    0x2d1a531b, 0x4c6e, 0x49cc, \
+    { 0x8a, 0xf6, 0x5c, 0x85, 0x7b, 0x78, 0xb5, 0xd7 } \
+}
+struct IKeyboard_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*PutScancode)(
+        IKeyboard *pThis,
+        PRInt32 scancode
+    );
+
+    nsresult (*PutScancodes)(
+        IKeyboard *pThis,
+        PRUint32 scancodesSize,
+        PRInt32* scancodes,
+        PRUint32 * codesStored
+    );
+
+    nsresult (*PutCAD)(IKeyboard *pThis );
+
+};
+
+struct IKeyboard
+{
+    struct IKeyboard_vtbl *vtbl;
+};
+/* End of struct IKeyboard Declaration */
+
+
+/* Start of struct IMouse Declaration */
+#define IMOUSE_IID_STR "7c0f2eae-f92d-498c-b802-e1a3763774dc"
+#define IMOUSE_IID { \
+    0x7c0f2eae, 0xf92d, 0x498c, \
+    { 0xb8, 0x02, 0xe1, 0xa3, 0x76, 0x37, 0x74, 0xdc } \
+}
+struct IMouse_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetAbsoluteSupported)(IMouse *pThis, PRBool *absoluteSupported);
+
+    nsresult (*PutMouseEvent)(
+        IMouse *pThis,
+        PRInt32 dx,
+        PRInt32 dy,
+        PRInt32 dz,
+        PRInt32 dw,
+        PRInt32 buttonState
+    );
+
+    nsresult (*PutMouseEventAbsolute)(
+        IMouse *pThis,
+        PRInt32 x,
+        PRInt32 y,
+        PRInt32 dz,
+        PRInt32 dw,
+        PRInt32 buttonState
+    );
+
+};
+
+struct IMouse
+{
+    struct IMouse_vtbl *vtbl;
+};
+/* End of struct IMouse Declaration */
+
+
+/* Start of struct IFramebuffer Declaration */
+#define IFRAMEBUFFER_IID_STR "b7ed347a-5765-40a0-ae1c-f543eb4ddeaf"
+#define IFRAMEBUFFER_IID { \
+    0xb7ed347a, 0x5765, 0x40a0, \
+    { 0xae, 0x1c, 0xf5, 0x43, 0xeb, 0x4d, 0xde, 0xaf } \
+}
+struct IFramebuffer_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetAddress)(IFramebuffer *pThis, PRUint8 * *address);
+
+    nsresult (*GetWidth)(IFramebuffer *pThis, PRUint32 *width);
+
+    nsresult (*GetHeight)(IFramebuffer *pThis, PRUint32 *height);
+
+    nsresult (*GetBitsPerPixel)(IFramebuffer *pThis, PRUint32 *bitsPerPixel);
+
+    nsresult (*GetBytesPerLine)(IFramebuffer *pThis, PRUint32 *bytesPerLine);
+
+    nsresult (*GetPixelFormat)(IFramebuffer *pThis, PRUint32 *pixelFormat);
+
+    nsresult (*GetUsesGuestVRAM)(IFramebuffer *pThis, PRBool *usesGuestVRAM);
+
+    nsresult (*GetHeightReduction)(IFramebuffer *pThis, PRUint32 *heightReduction);
+
+    nsresult (*GetOverlay)(IFramebuffer *pThis, IFramebufferOverlay * *overlay);
+
+    nsresult (*GetWinId)(IFramebuffer *pThis, PRUint64 *winId);
+
+    nsresult (*Lock)(IFramebuffer *pThis );
+
+    nsresult (*Unlock)(IFramebuffer *pThis );
+
+    nsresult (*NotifyUpdate)(
+        IFramebuffer *pThis,
+        PRUint32 x,
+        PRUint32 y,
+        PRUint32 width,
+        PRUint32 height
+    );
+
+    nsresult (*RequestResize)(
+        IFramebuffer *pThis,
+        PRUint32 screenId,
+        PRUint32 pixelFormat,
+        PRUint8 * VRAM,
+        PRUint32 bitsPerPixel,
+        PRUint32 bytesPerLine,
+        PRUint32 width,
+        PRUint32 height,
+        PRBool * finished
+    );
+
+    nsresult (*VideoModeSupported)(
+        IFramebuffer *pThis,
+        PRUint32 width,
+        PRUint32 height,
+        PRUint32 bpp,
+        PRBool * supported
+    );
+
+    nsresult (*GetVisibleRegion)(
+        IFramebuffer *pThis,
+        PRUint8 * rectangles,
+        PRUint32 count,
+        PRUint32 * countCopied
+    );
+
+    nsresult (*SetVisibleRegion)(
+        IFramebuffer *pThis,
+        PRUint8 * rectangles,
+        PRUint32 count
+    );
+
+    nsresult (*ProcessVHWACommand)(
+        IFramebuffer *pThis,
+        PRUint8 * command
+    );
+
+};
+
+struct IFramebuffer
+{
+    struct IFramebuffer_vtbl *vtbl;
+};
+/* End of struct IFramebuffer Declaration */
+
+
+/* Start of struct IFramebufferOverlay Declaration */
+#define IFRAMEBUFFEROVERLAY_IID_STR "0bcc1c7e-e415-47d2-bfdb-e4c705fb0f47"
+#define IFRAMEBUFFEROVERLAY_IID { \
+    0x0bcc1c7e, 0xe415, 0x47d2, \
+    { 0xbf, 0xdb, 0xe4, 0xc7, 0x05, 0xfb, 0x0f, 0x47 } \
+}
+struct IFramebufferOverlay_vtbl
+{
+    struct IFramebuffer_vtbl iframebuffer;
+
+    nsresult (*GetX)(IFramebufferOverlay *pThis, PRUint32 *x);
+
+    nsresult (*GetY)(IFramebufferOverlay *pThis, PRUint32 *y);
+
+    nsresult (*GetVisible)(IFramebufferOverlay *pThis, PRBool *visible);
+    nsresult (*SetVisible)(IFramebufferOverlay *pThis, PRBool visible);
+
+    nsresult (*GetAlpha)(IFramebufferOverlay *pThis, PRUint32 *alpha);
+    nsresult (*SetAlpha)(IFramebufferOverlay *pThis, PRUint32 alpha);
+
+    nsresult (*Move)(
+        IFramebufferOverlay *pThis,
+        PRUint32 x,
+        PRUint32 y
+    );
+
+};
+
+struct IFramebufferOverlay
+{
+    struct IFramebufferOverlay_vtbl *vtbl;
+};
+/* End of struct IFramebufferOverlay Declaration */
+
+
+/* Start of struct IDisplay Declaration */
+#define IDISPLAY_IID_STR "e2a38ebc-d854-4a3e-bc2e-fdf5ac4a0000"
+#define IDISPLAY_IID { \
+    0xe2a38ebc, 0xd854, 0x4a3e, \
+    { 0xbc, 0x2e, 0xfd, 0xf5, 0xac, 0x4a, 0x00, 0x00 } \
+}
+struct IDisplay_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetWidth)(IDisplay *pThis, PRUint32 *width);
+
+    nsresult (*GetHeight)(IDisplay *pThis, PRUint32 *height);
+
+    nsresult (*GetBitsPerPixel)(IDisplay *pThis, PRUint32 *bitsPerPixel);
+
+    nsresult (*SetFramebuffer)(
+        IDisplay *pThis,
+        PRUint32 screenId,
+        IFramebuffer * framebuffer
+    );
+
+    nsresult (*GetFramebuffer)(
+        IDisplay *pThis,
+        PRUint32 screenId,
+        IFramebuffer * * framebuffer,
+        PRInt32 * xOrigin,
+        PRInt32 * yOrigin
+    );
+
+    nsresult (*SetVideoModeHint)(
+        IDisplay *pThis,
+        PRUint32 width,
+        PRUint32 height,
+        PRUint32 bitsPerPixel,
+        PRUint32 display
+    );
+
+    nsresult (*SetSeamlessMode)(
+        IDisplay *pThis,
+        PRBool enabled
+    );
+
+    nsresult (*TakeScreenShot)(
+        IDisplay *pThis,
+        PRUint8 * address,
+        PRUint32 width,
+        PRUint32 height
+    );
+
+    nsresult (*TakeScreenShotSlow)(
+        IDisplay *pThis,
+        PRUint32 width,
+        PRUint32 height,
+        PRUint32 *screenDataSize,
+        PRUint8** screenData
+    );
+
+    nsresult (*DrawToScreen)(
+        IDisplay *pThis,
+        PRUint8 * address,
+        PRUint32 x,
+        PRUint32 y,
+        PRUint32 width,
+        PRUint32 height
+    );
+
+    nsresult (*InvalidateAndUpdate)(IDisplay *pThis );
+
+    nsresult (*ResizeCompleted)(
+        IDisplay *pThis,
+        PRUint32 screenId
+    );
+
+    nsresult (*UpdateCompleted)(IDisplay *pThis );
+
+    nsresult (*CompleteVHWACommand)(
+        IDisplay *pThis,
+        PRUint8 * command
+    );
+
+};
+
+struct IDisplay
+{
+    struct IDisplay_vtbl *vtbl;
+};
+/* End of struct IDisplay Declaration */
+
+
+/* Start of struct INetworkAdapter Declaration */
+#define INETWORKADAPTER_IID_STR "65607a27-2b73-4d43-b4cc-0ba2c817fbde"
+#define INETWORKADAPTER_IID { \
+    0x65607a27, 0x2b73, 0x4d43, \
+    { 0xb4, 0xcc, 0x0b, 0xa2, 0xc8, 0x17, 0xfb, 0xde } \
+}
+struct INetworkAdapter_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetAdapterType)(INetworkAdapter *pThis, PRUint32 *adapterType);
+    nsresult (*SetAdapterType)(INetworkAdapter *pThis, PRUint32 adapterType);
+
+    nsresult (*GetSlot)(INetworkAdapter *pThis, PRUint32 *slot);
+
+    nsresult (*GetEnabled)(INetworkAdapter *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(INetworkAdapter *pThis, PRBool enabled);
+
+    nsresult (*GetMACAddress)(INetworkAdapter *pThis, PRUnichar * *MACAddress);
+    nsresult (*SetMACAddress)(INetworkAdapter *pThis, PRUnichar * MACAddress);
+
+    nsresult (*GetAttachmentType)(INetworkAdapter *pThis, PRUint32 *attachmentType);
+
+    nsresult (*GetHostInterface)(INetworkAdapter *pThis, PRUnichar * *hostInterface);
+    nsresult (*SetHostInterface)(INetworkAdapter *pThis, PRUnichar * hostInterface);
+
+    nsresult (*GetInternalNetwork)(INetworkAdapter *pThis, PRUnichar * *internalNetwork);
+    nsresult (*SetInternalNetwork)(INetworkAdapter *pThis, PRUnichar * internalNetwork);
+
+    nsresult (*GetNATNetwork)(INetworkAdapter *pThis, PRUnichar * *NATNetwork);
+    nsresult (*SetNATNetwork)(INetworkAdapter *pThis, PRUnichar * NATNetwork);
+
+    nsresult (*GetCableConnected)(INetworkAdapter *pThis, PRBool *cableConnected);
+    nsresult (*SetCableConnected)(INetworkAdapter *pThis, PRBool cableConnected);
+
+    nsresult (*GetLineSpeed)(INetworkAdapter *pThis, PRUint32 *lineSpeed);
+    nsresult (*SetLineSpeed)(INetworkAdapter *pThis, PRUint32 lineSpeed);
+
+    nsresult (*GetTraceEnabled)(INetworkAdapter *pThis, PRBool *traceEnabled);
+    nsresult (*SetTraceEnabled)(INetworkAdapter *pThis, PRBool traceEnabled);
+
+    nsresult (*GetTraceFile)(INetworkAdapter *pThis, PRUnichar * *traceFile);
+    nsresult (*SetTraceFile)(INetworkAdapter *pThis, PRUnichar * traceFile);
+
+    nsresult (*AttachToNAT)(INetworkAdapter *pThis );
+
+    nsresult (*AttachToBridgedInterface)(INetworkAdapter *pThis );
+
+    nsresult (*AttachToInternalNetwork)(INetworkAdapter *pThis );
+
+    nsresult (*AttachToHostOnlyInterface)(INetworkAdapter *pThis );
+
+    nsresult (*Detach)(INetworkAdapter *pThis );
+
+};
+
+struct INetworkAdapter
+{
+    struct INetworkAdapter_vtbl *vtbl;
+};
+/* End of struct INetworkAdapter Declaration */
+
+
+/* Start of struct ISerialPort Declaration */
+#define ISERIALPORT_IID_STR "937f6970-5103-4745-b78e-d28dcf1479a8"
+#define ISERIALPORT_IID { \
+    0x937f6970, 0x5103, 0x4745, \
+    { 0xb7, 0x8e, 0xd2, 0x8d, 0xcf, 0x14, 0x79, 0xa8 } \
+}
+struct ISerialPort_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetSlot)(ISerialPort *pThis, PRUint32 *slot);
+
+    nsresult (*GetEnabled)(ISerialPort *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(ISerialPort *pThis, PRBool enabled);
+
+    nsresult (*GetIOBase)(ISerialPort *pThis, PRUint32 *IOBase);
+    nsresult (*SetIOBase)(ISerialPort *pThis, PRUint32 IOBase);
+
+    nsresult (*GetIRQ)(ISerialPort *pThis, PRUint32 *IRQ);
+    nsresult (*SetIRQ)(ISerialPort *pThis, PRUint32 IRQ);
+
+    nsresult (*GetHostMode)(ISerialPort *pThis, PRUint32 *hostMode);
+    nsresult (*SetHostMode)(ISerialPort *pThis, PRUint32 hostMode);
+
+    nsresult (*GetServer)(ISerialPort *pThis, PRBool *server);
+    nsresult (*SetServer)(ISerialPort *pThis, PRBool server);
+
+    nsresult (*GetPath)(ISerialPort *pThis, PRUnichar * *path);
+    nsresult (*SetPath)(ISerialPort *pThis, PRUnichar * path);
+
+};
+
+struct ISerialPort
+{
+    struct ISerialPort_vtbl *vtbl;
+};
+/* End of struct ISerialPort Declaration */
+
+
+/* Start of struct IParallelPort Declaration */
+#define IPARALLELPORT_IID_STR "0c925f06-dd10-4b77-8de8-294d738c3214"
+#define IPARALLELPORT_IID { \
+    0x0c925f06, 0xdd10, 0x4b77, \
+    { 0x8d, 0xe8, 0x29, 0x4d, 0x73, 0x8c, 0x32, 0x14 } \
+}
+struct IParallelPort_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetSlot)(IParallelPort *pThis, PRUint32 *slot);
+
+    nsresult (*GetEnabled)(IParallelPort *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(IParallelPort *pThis, PRBool enabled);
+
+    nsresult (*GetIOBase)(IParallelPort *pThis, PRUint32 *IOBase);
+    nsresult (*SetIOBase)(IParallelPort *pThis, PRUint32 IOBase);
+
+    nsresult (*GetIRQ)(IParallelPort *pThis, PRUint32 *IRQ);
+    nsresult (*SetIRQ)(IParallelPort *pThis, PRUint32 IRQ);
+
+    nsresult (*GetPath)(IParallelPort *pThis, PRUnichar * *path);
+    nsresult (*SetPath)(IParallelPort *pThis, PRUnichar * path);
+
+};
+
+struct IParallelPort
+{
+    struct IParallelPort_vtbl *vtbl;
+};
+/* End of struct IParallelPort Declaration */
+
+
+/* Start of struct IMachineDebugger Declaration */
+#define IMACHINEDEBUGGER_IID_STR "b0b2a2dd-0627-4502-91c2-ddc5e77609e0"
+#define IMACHINEDEBUGGER_IID { \
+    0xb0b2a2dd, 0x0627, 0x4502, \
+    { 0x91, 0xc2, 0xdd, 0xc5, 0xe7, 0x76, 0x09, 0xe0 } \
+}
+struct IMachineDebugger_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetSinglestep)(IMachineDebugger *pThis, PRBool *singlestep);
+    nsresult (*SetSinglestep)(IMachineDebugger *pThis, PRBool singlestep);
+
+    nsresult (*GetRecompileUser)(IMachineDebugger *pThis, PRBool *recompileUser);
+    nsresult (*SetRecompileUser)(IMachineDebugger *pThis, PRBool recompileUser);
+
+    nsresult (*GetRecompileSupervisor)(IMachineDebugger *pThis, PRBool *recompileSupervisor);
+    nsresult (*SetRecompileSupervisor)(IMachineDebugger *pThis, PRBool recompileSupervisor);
+
+    nsresult (*GetPATMEnabled)(IMachineDebugger *pThis, PRBool *PATMEnabled);
+    nsresult (*SetPATMEnabled)(IMachineDebugger *pThis, PRBool PATMEnabled);
+
+    nsresult (*GetCSAMEnabled)(IMachineDebugger *pThis, PRBool *CSAMEnabled);
+    nsresult (*SetCSAMEnabled)(IMachineDebugger *pThis, PRBool CSAMEnabled);
+
+    nsresult (*GetLogEnabled)(IMachineDebugger *pThis, PRBool *logEnabled);
+    nsresult (*SetLogEnabled)(IMachineDebugger *pThis, PRBool logEnabled);
+
+    nsresult (*GetHWVirtExEnabled)(IMachineDebugger *pThis, PRBool *HWVirtExEnabled);
+
+    nsresult (*GetHWVirtExNestedPagingEnabled)(IMachineDebugger *pThis, PRBool *HWVirtExNestedPagingEnabled);
+
+    nsresult (*GetHWVirtExVPIDEnabled)(IMachineDebugger *pThis, PRBool *HWVirtExVPIDEnabled);
+
+    nsresult (*GetPAEEnabled)(IMachineDebugger *pThis, PRBool *PAEEnabled);
+
+    nsresult (*GetVirtualTimeRate)(IMachineDebugger *pThis, PRUint32 *virtualTimeRate);
+    nsresult (*SetVirtualTimeRate)(IMachineDebugger *pThis, PRUint32 virtualTimeRate);
+
+    nsresult (*GetVM)(IMachineDebugger *pThis, PRUint64 *VM);
+
+    nsresult (*ResetStats)(
+        IMachineDebugger *pThis,
+        PRUnichar * pattern
+    );
+
+    nsresult (*DumpStats)(
+        IMachineDebugger *pThis,
+        PRUnichar * pattern
+    );
+
+    nsresult (*GetStats)(
+        IMachineDebugger *pThis,
+        PRUnichar * pattern,
+        PRBool withDescriptions,
+        PRUnichar * * stats
+    );
+
+    nsresult (*InjectNMI)(IMachineDebugger *pThis );
+
+};
+
+struct IMachineDebugger
+{
+    struct IMachineDebugger_vtbl *vtbl;
+};
+/* End of struct IMachineDebugger Declaration */
+
+
+/* Start of struct IUSBController Declaration */
+#define IUSBCONTROLLER_IID_STR "238540fa-4b73-435a-a38e-4e1d9eab5c17"
+#define IUSBCONTROLLER_IID { \
+    0x238540fa, 0x4b73, 0x435a, \
+    { 0xa3, 0x8e, 0x4e, 0x1d, 0x9e, 0xab, 0x5c, 0x17 } \
+}
+struct IUSBController_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetEnabled)(IUSBController *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(IUSBController *pThis, PRBool enabled);
+
+    nsresult (*GetEnabledEhci)(IUSBController *pThis, PRBool *enabledEhci);
+    nsresult (*SetEnabledEhci)(IUSBController *pThis, PRBool enabledEhci);
+
+    nsresult (*GetUSBStandard)(IUSBController *pThis, PRUint16 *USBStandard);
+
+    nsresult (*GetDeviceFilters)(IUSBController *pThis, PRUint32 *deviceFiltersSize, IUSBDeviceFilter * **deviceFilters);
+
+    nsresult (*CreateDeviceFilter)(
+        IUSBController *pThis,
+        PRUnichar * name,
+        IUSBDeviceFilter * * filter
+    );
+
+    nsresult (*InsertDeviceFilter)(
+        IUSBController *pThis,
+        PRUint32 position,
+        IUSBDeviceFilter * filter
+    );
+
+    nsresult (*RemoveDeviceFilter)(
+        IUSBController *pThis,
+        PRUint32 position,
+        IUSBDeviceFilter * * filter
+    );
+
+};
+
+struct IUSBController
+{
+    struct IUSBController_vtbl *vtbl;
+};
+/* End of struct IUSBController Declaration */
+
+
+/* Start of struct IUSBDevice Declaration */
+#define IUSBDEVICE_IID_STR "f8967b0b-4483-400f-92b5-8b675d98a85b"
+#define IUSBDEVICE_IID { \
+    0xf8967b0b, 0x4483, 0x400f, \
+    { 0x92, 0xb5, 0x8b, 0x67, 0x5d, 0x98, 0xa8, 0x5b } \
+}
+struct IUSBDevice_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetId)(IUSBDevice *pThis, PRUnichar * *id);
+
+    nsresult (*GetVendorId)(IUSBDevice *pThis, PRUint16 *vendorId);
+
+    nsresult (*GetProductId)(IUSBDevice *pThis, PRUint16 *productId);
+
+    nsresult (*GetRevision)(IUSBDevice *pThis, PRUint16 *revision);
+
+    nsresult (*GetManufacturer)(IUSBDevice *pThis, PRUnichar * *manufacturer);
+
+    nsresult (*GetProduct)(IUSBDevice *pThis, PRUnichar * *product);
+
+    nsresult (*GetSerialNumber)(IUSBDevice *pThis, PRUnichar * *serialNumber);
+
+    nsresult (*GetAddress)(IUSBDevice *pThis, PRUnichar * *address);
+
+    nsresult (*GetPort)(IUSBDevice *pThis, PRUint16 *port);
+
+    nsresult (*GetVersion)(IUSBDevice *pThis, PRUint16 *version);
+
+    nsresult (*GetPortVersion)(IUSBDevice *pThis, PRUint16 *portVersion);
+
+    nsresult (*GetRemote)(IUSBDevice *pThis, PRBool *remote);
+
+};
+
+struct IUSBDevice
+{
+    struct IUSBDevice_vtbl *vtbl;
+};
+/* End of struct IUSBDevice Declaration */
+
+
+/* Start of struct IUSBDeviceFilter Declaration */
+#define IUSBDEVICEFILTER_IID_STR "d6831fb4-1a94-4c2c-96ef-8d0d6192066d"
+#define IUSBDEVICEFILTER_IID { \
+    0xd6831fb4, 0x1a94, 0x4c2c, \
+    { 0x96, 0xef, 0x8d, 0x0d, 0x61, 0x92, 0x06, 0x6d } \
+}
+struct IUSBDeviceFilter_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetName)(IUSBDeviceFilter *pThis, PRUnichar * *name);
+    nsresult (*SetName)(IUSBDeviceFilter *pThis, PRUnichar * name);
+
+    nsresult (*GetActive)(IUSBDeviceFilter *pThis, PRBool *active);
+    nsresult (*SetActive)(IUSBDeviceFilter *pThis, PRBool active);
+
+    nsresult (*GetVendorId)(IUSBDeviceFilter *pThis, PRUnichar * *vendorId);
+    nsresult (*SetVendorId)(IUSBDeviceFilter *pThis, PRUnichar * vendorId);
+
+    nsresult (*GetProductId)(IUSBDeviceFilter *pThis, PRUnichar * *productId);
+    nsresult (*SetProductId)(IUSBDeviceFilter *pThis, PRUnichar * productId);
+
+    nsresult (*GetRevision)(IUSBDeviceFilter *pThis, PRUnichar * *revision);
+    nsresult (*SetRevision)(IUSBDeviceFilter *pThis, PRUnichar * revision);
+
+    nsresult (*GetManufacturer)(IUSBDeviceFilter *pThis, PRUnichar * *manufacturer);
+    nsresult (*SetManufacturer)(IUSBDeviceFilter *pThis, PRUnichar * manufacturer);
+
+    nsresult (*GetProduct)(IUSBDeviceFilter *pThis, PRUnichar * *product);
+    nsresult (*SetProduct)(IUSBDeviceFilter *pThis, PRUnichar * product);
+
+    nsresult (*GetSerialNumber)(IUSBDeviceFilter *pThis, PRUnichar * *serialNumber);
+    nsresult (*SetSerialNumber)(IUSBDeviceFilter *pThis, PRUnichar * serialNumber);
+
+    nsresult (*GetPort)(IUSBDeviceFilter *pThis, PRUnichar * *port);
+    nsresult (*SetPort)(IUSBDeviceFilter *pThis, PRUnichar * port);
+
+    nsresult (*GetRemote)(IUSBDeviceFilter *pThis, PRUnichar * *remote);
+    nsresult (*SetRemote)(IUSBDeviceFilter *pThis, PRUnichar * remote);
+
+    nsresult (*GetMaskedInterfaces)(IUSBDeviceFilter *pThis, PRUint32 *maskedInterfaces);
+    nsresult (*SetMaskedInterfaces)(IUSBDeviceFilter *pThis, PRUint32 maskedInterfaces);
+
+};
+
+struct IUSBDeviceFilter
+{
+    struct IUSBDeviceFilter_vtbl *vtbl;
+};
+/* End of struct IUSBDeviceFilter Declaration */
+
+
+/* Start of struct IHostUSBDevice Declaration */
+#define IHOSTUSBDEVICE_IID_STR "173b4b44-d268-4334-a00d-b6521c9a740a"
+#define IHOSTUSBDEVICE_IID { \
+    0x173b4b44, 0xd268, 0x4334, \
+    { 0xa0, 0x0d, 0xb6, 0x52, 0x1c, 0x9a, 0x74, 0x0a } \
+}
+struct IHostUSBDevice_vtbl
+{
+    struct IUSBDevice_vtbl iusbdevice;
+
+    nsresult (*GetState)(IHostUSBDevice *pThis, PRUint32 *state);
+
+};
+
+struct IHostUSBDevice
+{
+    struct IHostUSBDevice_vtbl *vtbl;
+};
+/* End of struct IHostUSBDevice Declaration */
+
+
+/* Start of struct IHostUSBDeviceFilter Declaration */
+#define IHOSTUSBDEVICEFILTER_IID_STR "4cc70246-d74a-400f-8222-3900489c0374"
+#define IHOSTUSBDEVICEFILTER_IID { \
+    0x4cc70246, 0xd74a, 0x400f, \
+    { 0x82, 0x22, 0x39, 0x00, 0x48, 0x9c, 0x03, 0x74 } \
+}
+struct IHostUSBDeviceFilter_vtbl
+{
+    struct IUSBDeviceFilter_vtbl iusbdevicefilter;
+
+    nsresult (*GetAction)(IHostUSBDeviceFilter *pThis, PRUint32 *action);
+    nsresult (*SetAction)(IHostUSBDeviceFilter *pThis, PRUint32 action);
+
+};
+
+struct IHostUSBDeviceFilter
+{
+    struct IHostUSBDeviceFilter_vtbl *vtbl;
+};
+/* End of struct IHostUSBDeviceFilter Declaration */
+
+
+/* Start of struct IAudioAdapter Declaration */
+#define IAUDIOADAPTER_IID_STR "921873db-5f3f-4b69-91f9-7be9e535a2cb"
+#define IAUDIOADAPTER_IID { \
+    0x921873db, 0x5f3f, 0x4b69, \
+    { 0x91, 0xf9, 0x7b, 0xe9, 0xe5, 0x35, 0xa2, 0xcb } \
+}
+struct IAudioAdapter_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetEnabled)(IAudioAdapter *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(IAudioAdapter *pThis, PRBool enabled);
+
+    nsresult (*GetAudioController)(IAudioAdapter *pThis, PRUint32 *audioController);
+    nsresult (*SetAudioController)(IAudioAdapter *pThis, PRUint32 audioController);
+
+    nsresult (*GetAudioDriver)(IAudioAdapter *pThis, PRUint32 *audioDriver);
+    nsresult (*SetAudioDriver)(IAudioAdapter *pThis, PRUint32 audioDriver);
+
+};
+
+struct IAudioAdapter
+{
+    struct IAudioAdapter_vtbl *vtbl;
+};
+/* End of struct IAudioAdapter Declaration */
+
+
+/* Start of struct IVRDPServer Declaration */
+#define IVRDPSERVER_IID_STR "72e671bc-1712-4052-ad6b-e45e76d9d3e4"
+#define IVRDPSERVER_IID { \
+    0x72e671bc, 0x1712, 0x4052, \
+    { 0xad, 0x6b, 0xe4, 0x5e, 0x76, 0xd9, 0xd3, 0xe4 } \
+}
+struct IVRDPServer_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetEnabled)(IVRDPServer *pThis, PRBool *enabled);
+    nsresult (*SetEnabled)(IVRDPServer *pThis, PRBool enabled);
+
+    nsresult (*GetPorts)(IVRDPServer *pThis, PRUnichar * *ports);
+    nsresult (*SetPorts)(IVRDPServer *pThis, PRUnichar * ports);
+
+    nsresult (*GetNetAddress)(IVRDPServer *pThis, PRUnichar * *netAddress);
+    nsresult (*SetNetAddress)(IVRDPServer *pThis, PRUnichar * netAddress);
+
+    nsresult (*GetAuthType)(IVRDPServer *pThis, PRUint32 *authType);
+    nsresult (*SetAuthType)(IVRDPServer *pThis, PRUint32 authType);
+
+    nsresult (*GetAuthTimeout)(IVRDPServer *pThis, PRUint32 *authTimeout);
+    nsresult (*SetAuthTimeout)(IVRDPServer *pThis, PRUint32 authTimeout);
+
+    nsresult (*GetAllowMultiConnection)(IVRDPServer *pThis, PRBool *allowMultiConnection);
+    nsresult (*SetAllowMultiConnection)(IVRDPServer *pThis, PRBool allowMultiConnection);
+
+    nsresult (*GetReuseSingleConnection)(IVRDPServer *pThis, PRBool *reuseSingleConnection);
+    nsresult (*SetReuseSingleConnection)(IVRDPServer *pThis, PRBool reuseSingleConnection);
+
+};
+
+struct IVRDPServer
+{
+    struct IVRDPServer_vtbl *vtbl;
+};
+/* End of struct IVRDPServer Declaration */
+
+
+/* Start of struct ISharedFolder Declaration */
+#define ISHAREDFOLDER_IID_STR "64637bb2-9e17-471c-b8f3-f8968dd9884e"
+#define ISHAREDFOLDER_IID { \
+    0x64637bb2, 0x9e17, 0x471c, \
+    { 0xb8, 0xf3, 0xf8, 0x96, 0x8d, 0xd9, 0x88, 0x4e } \
+}
+struct ISharedFolder_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetName)(ISharedFolder *pThis, PRUnichar * *name);
+
+    nsresult (*GetHostPath)(ISharedFolder *pThis, PRUnichar * *hostPath);
+
+    nsresult (*GetAccessible)(ISharedFolder *pThis, PRBool *accessible);
+
+    nsresult (*GetWritable)(ISharedFolder *pThis, PRBool *writable);
+
+    nsresult (*GetLastAccessError)(ISharedFolder *pThis, PRUnichar * *lastAccessError);
+
+};
+
+struct ISharedFolder
+{
+    struct ISharedFolder_vtbl *vtbl;
+};
+/* End of struct ISharedFolder Declaration */
+
+
+/* Start of struct IInternalSessionControl Declaration */
+#define IINTERNALSESSIONCONTROL_IID_STR "f9aac6d0-41b3-46b7-bea4-6370b4036de6"
+#define IINTERNALSESSIONCONTROL_IID { \
+    0xf9aac6d0, 0x41b3, 0x46b7, \
+    { 0xbe, 0xa4, 0x63, 0x70, 0xb4, 0x03, 0x6d, 0xe6 } \
+}
+struct IInternalSessionControl_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetPID)(
+        IInternalSessionControl *pThis,
+        PRUint32 * pid
+    );
+
+    nsresult (*GetRemoteConsole)(
+        IInternalSessionControl *pThis,
+        IConsole * * console
+    );
+
+    nsresult (*AssignMachine)(
+        IInternalSessionControl *pThis,
+        IMachine * machine
+    );
+
+    nsresult (*AssignRemoteMachine)(
+        IInternalSessionControl *pThis,
+        IMachine * machine,
+        IConsole * console
+    );
+
+    nsresult (*UpdateMachineState)(
+        IInternalSessionControl *pThis,
+        PRUint32 aMachineState
+    );
+
+    nsresult (*Uninitialize)(IInternalSessionControl *pThis );
+
+    nsresult (*OnNetworkAdapterChange)(
+        IInternalSessionControl *pThis,
+        INetworkAdapter * networkAdapter,
+        PRBool changeAdapter
+    );
+
+    nsresult (*OnSerialPortChange)(
+        IInternalSessionControl *pThis,
+        ISerialPort * serialPort
+    );
+
+    nsresult (*OnParallelPortChange)(
+        IInternalSessionControl *pThis,
+        IParallelPort * parallelPort
+    );
+
+    nsresult (*OnStorageControllerChange)(IInternalSessionControl *pThis );
+
+    nsresult (*OnMediumChange)(
+        IInternalSessionControl *pThis,
+        IMediumAttachment * mediumAttachment,
+        PRBool force
+    );
+
+    nsresult (*OnVRDPServerChange)(IInternalSessionControl *pThis );
+
+    nsresult (*OnUSBControllerChange)(IInternalSessionControl *pThis );
+
+    nsresult (*OnSharedFolderChange)(
+        IInternalSessionControl *pThis,
+        PRBool global
+    );
+
+    nsresult (*OnUSBDeviceAttach)(
+        IInternalSessionControl *pThis,
+        IUSBDevice * device,
+        IVirtualBoxErrorInfo * error,
+        PRUint32 maskedInterfaces
+    );
+
+    nsresult (*OnUSBDeviceDetach)(
+        IInternalSessionControl *pThis,
+        PRUnichar * id,
+        IVirtualBoxErrorInfo * error
+    );
+
+    nsresult (*OnShowWindow)(
+        IInternalSessionControl *pThis,
+        PRBool check,
+        PRBool * canShow,
+        PRUint64 * winId
+    );
+
+    nsresult (*AccessGuestProperty)(
+        IInternalSessionControl *pThis,
+        PRUnichar * name,
+        PRUnichar * value,
+        PRUnichar * flags,
+        PRBool isSetter,
+        PRUnichar * * retValue,
+        PRUint64 * retTimestamp,
+        PRUnichar * * retFlags
+    );
+
+    nsresult (*EnumerateGuestProperties)(
+        IInternalSessionControl *pThis,
+        PRUnichar * patterns,
+        PRUint32 *keySize,
+        PRUnichar *** key,
+        PRUint32 *valueSize,
+        PRUnichar *** value,
+        PRUint32 *timestampSize,
+        PRUint64* timestamp,
+        PRUint32 *flagsSize,
+        PRUnichar *** flags
+    );
+
+};
+
+struct IInternalSessionControl
+{
+    struct IInternalSessionControl_vtbl *vtbl;
+};
+/* End of struct IInternalSessionControl Declaration */
+
+
+/* Start of struct ISession Declaration */
+#define ISESSION_IID_STR "12F4DCDB-12B2-4EC1-B7CD-DDD9F6C5BF4D"
+#define ISESSION_IID { \
+    0x12F4DCDB, 0x12B2, 0x4EC1, \
+    { 0xB7, 0xCD, 0xDD, 0xD9, 0xF6, 0xC5, 0xBF, 0x4D } \
+}
+struct ISession_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetState)(ISession *pThis, PRUint32 *state);
+
+    nsresult (*GetType)(ISession *pThis, PRUint32 *type);
+
+    nsresult (*GetMachine)(ISession *pThis, IMachine * *machine);
+
+    nsresult (*GetConsole)(ISession *pThis, IConsole * *console);
+
+    nsresult (*Close)(ISession *pThis );
+
+};
+
+struct ISession
+{
+    struct ISession_vtbl *vtbl;
+};
+/* End of struct ISession Declaration */
+
+
+/* Start of struct IStorageController Declaration */
+#define ISTORAGECONTROLLER_IID_STR "6bf8335b-d14a-44a5-9b45-ddc49ce7d5b2"
+#define ISTORAGECONTROLLER_IID { \
+    0x6bf8335b, 0xd14a, 0x44a5, \
+    { 0x9b, 0x45, 0xdd, 0xc4, 0x9c, 0xe7, 0xd5, 0xb2 } \
+}
+struct IStorageController_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetName)(IStorageController *pThis, PRUnichar * *name);
+
+    nsresult (*GetMaxDevicesPerPortCount)(IStorageController *pThis, PRUint32 *maxDevicesPerPortCount);
+
+    nsresult (*GetMinPortCount)(IStorageController *pThis, PRUint32 *minPortCount);
+
+    nsresult (*GetMaxPortCount)(IStorageController *pThis, PRUint32 *maxPortCount);
+
+    nsresult (*GetInstance)(IStorageController *pThis, PRUint32 *instance);
+    nsresult (*SetInstance)(IStorageController *pThis, PRUint32 instance);
+
+    nsresult (*GetPortCount)(IStorageController *pThis, PRUint32 *portCount);
+    nsresult (*SetPortCount)(IStorageController *pThis, PRUint32 portCount);
+
+    nsresult (*GetBus)(IStorageController *pThis, PRUint32 *bus);
+
+    nsresult (*GetControllerType)(IStorageController *pThis, PRUint32 *controllerType);
+    nsresult (*SetControllerType)(IStorageController *pThis, PRUint32 controllerType);
+
+    nsresult (*GetIDEEmulationPort)(
+        IStorageController *pThis,
+        PRInt32 devicePosition,
+        PRInt32 * portNumber
+    );
+
+    nsresult (*SetIDEEmulationPort)(
+        IStorageController *pThis,
+        PRInt32 devicePosition,
+        PRInt32 portNumber
+    );
+
+};
+
+struct IStorageController
+{
+    struct IStorageController_vtbl *vtbl;
+};
+/* End of struct IStorageController Declaration */
+
+
+/* Start of struct IPerformanceMetric Declaration */
+#define IPERFORMANCEMETRIC_IID_STR "2a1a60ae-9345-4019-ad53-d34ba41cbfe9"
+#define IPERFORMANCEMETRIC_IID { \
+    0x2a1a60ae, 0x9345, 0x4019, \
+    { 0xad, 0x53, 0xd3, 0x4b, 0xa4, 0x1c, 0xbf, 0xe9 } \
+}
+struct IPerformanceMetric_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetMetricName)(IPerformanceMetric *pThis, PRUnichar * *metricName);
+
+    nsresult (*GetObject)(IPerformanceMetric *pThis, nsISupports * *object);
+
+    nsresult (*GetDescription)(IPerformanceMetric *pThis, PRUnichar * *description);
+
+    nsresult (*GetPeriod)(IPerformanceMetric *pThis, PRUint32 *period);
+
+    nsresult (*GetCount)(IPerformanceMetric *pThis, PRUint32 *count);
+
+    nsresult (*GetUnit)(IPerformanceMetric *pThis, PRUnichar * *unit);
+
+    nsresult (*GetMinimumValue)(IPerformanceMetric *pThis, PRInt32 *minimumValue);
+
+    nsresult (*GetMaximumValue)(IPerformanceMetric *pThis, PRInt32 *maximumValue);
+
+};
+
+struct IPerformanceMetric
+{
+    struct IPerformanceMetric_vtbl *vtbl;
+};
+/* End of struct IPerformanceMetric Declaration */
+
+
+/* Start of struct IPerformanceCollector Declaration */
+#define IPERFORMANCECOLLECTOR_IID_STR "e22e1acb-ac4a-43bb-a31c-17321659b0c6"
+#define IPERFORMANCECOLLECTOR_IID { \
+    0xe22e1acb, 0xac4a, 0x43bb, \
+    { 0xa3, 0x1c, 0x17, 0x32, 0x16, 0x59, 0xb0, 0xc6 } \
+}
+struct IPerformanceCollector_vtbl
+{
+    struct nsISupports_vtbl nsisupports;
+
+    nsresult (*GetMetricNames)(IPerformanceCollector *pThis, PRUint32 *metricNamesSize, PRUnichar * **metricNames);
+
+    nsresult (*GetMetrics)(
+        IPerformanceCollector *pThis,
+        PRUint32 metricNamesSize,
+        PRUnichar ** metricNames,
+        PRUint32 objectsSize,
+        nsISupports ** objects,
+        PRUint32 *metricsSize,
+        IPerformanceMetric *** metrics
+    );
+
+    nsresult (*SetupMetrics)(
+        IPerformanceCollector *pThis,
+        PRUint32 metricNamesSize,
+        PRUnichar ** metricNames,
+        PRUint32 objectsSize,
+        nsISupports ** objects,
+        PRUint32 period,
+        PRUint32 count,
+        PRUint32 *affectedMetricsSize,
+        IPerformanceMetric *** affectedMetrics
+    );
+
+    nsresult (*EnableMetrics)(
+        IPerformanceCollector *pThis,
+        PRUint32 metricNamesSize,
+        PRUnichar ** metricNames,
+        PRUint32 objectsSize,
+        nsISupports ** objects,
+        PRUint32 *affectedMetricsSize,
+        IPerformanceMetric *** affectedMetrics
+    );
+
+    nsresult (*DisableMetrics)(
+        IPerformanceCollector *pThis,
+        PRUint32 metricNamesSize,
+        PRUnichar ** metricNames,
+        PRUint32 objectsSize,
+        nsISupports ** objects,
+        PRUint32 *affectedMetricsSize,
+        IPerformanceMetric *** affectedMetrics
+    );
+
+    nsresult (*QueryMetricsData)(
+        IPerformanceCollector *pThis,
+        PRUint32 metricNamesSize,
+        PRUnichar ** metricNames,
+        PRUint32 objectsSize,
+        nsISupports ** objects,
+        PRUint32 *returnMetricNamesSize,
+        PRUnichar *** returnMetricNames,
+        PRUint32 *returnObjectsSize,
+        nsISupports ** returnObjects,
+        PRUint32 *returnUnitsSize,
+        PRUnichar *** returnUnits,
+        PRUint32 *returnScalesSize,
+        PRUint32* returnScales,
+        PRUint32 *returnSequenceNumbersSize,
+        PRUint32* returnSequenceNumbers,
+        PRUint32 *returnDataIndicesSize,
+        PRUint32* returnDataIndices,
+        PRUint32 *returnDataLengthsSize,
+        PRUint32* returnDataLengths,
+        PRUint32 *returnDataSize,
+        PRInt32** returnData
+    );
+
+};
+
+struct IPerformanceCollector
+{
+    struct IPerformanceCollector_vtbl *vtbl;
+};
+/* End of struct IPerformanceCollector Declaration */
+
+
+
+#define NS_VIRTUALBOX_CID { \
+    0xB1A7A4F2, 0x47B9, 0x4A1E, \
+    { 0x82, 0xB2, 0x07, 0xCC, 0xD5, 0x32, 0x3C, 0x3F } \
+}
+#define NS_VIRTUALBOX_CONTRACTID "@virtualbox.org/VirtualBox;1"
+/* for compatibility with Win32 */
+#define CLSID_VirtualBox (nsCID) NS_VIRTUALBOX_CID
+
+
+
+#define NS_SESSION_CID { \
+    0x3C02F46D, 0xC9D2, 0x4F11, \
+    { 0xA3, 0x84, 0x53, 0xF0, 0xCF, 0x91, 0x72, 0x14 } \
+}
+#define NS_SESSION_CONTRACTID "@virtualbox.org/Session;1"
+/* for compatibility with Win32 */
+#define CLSID_Session (nsCID) NS_SESSION_CID
+
+
+
+#define NS_CALLBACKWRAPPER_CID { \
+    0x49EE8561, 0x5563, 0x4715, \
+    { 0xB1, 0x8C, 0xA4, 0xB1, 0xA4, 0x90, 0xDA, 0xFE } \
+}
+#define NS_CALLBACKWRAPPER_CONTRACTID "@virtualbox.org/CallbackWrapper;1"
+/* for compatibility with Win32 */
+#define CLSID_CallbackWrapper (nsCID) NS_CALLBACKWRAPPER_CID
+
+
+
+#endif /* !__cplusplus */
+
+#ifdef IN_VBOXXPCOMC
+# define VBOXXPCOMC_DECL(type)  PR_EXPORT(type)
+#else
+# define VBOXXPCOMC_DECL(type)  PR_IMPORT(type)
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/**
+ * Function table for dynamic linking.
+ * Use VBoxGetFunctions() to obtain the pointer to it.
+ */
+typedef struct VBOXXPCOMC
+{
+    /** The size of the structure. */
+    unsigned cb;
+    /** The structure version. */
+    unsigned uVersion;
+
+    unsigned int (*pfnGetVersion)(void);
+
+    void  (*pfnComInitialize)(const char *pszVirtualBoxIID,
+                              IVirtualBox **ppVirtualBox,
+                              const char *pszSessionIID,
+                              ISession **ppSession);
+    void (*pfnComUninitialize)(void);
+
+    void  (*pfnComUnallocMem)(void *pv);
+    void  (*pfnUtf16Free)(PRUnichar *pwszString);
+    void  (*pfnUtf8Free)(char *pszString);
+
+    int   (*pfnUtf16ToUtf8)(const PRUnichar *pwszString, char **ppszString);
+    int   (*pfnUtf8ToUtf16)(const char *pszString, PRUnichar **ppwszString);
+
+    void  (*pfnGetEventQueue)(nsIEventQueue **eventQueue);
+
+    /** Tail version, same as uVersion. */
+    unsigned uEndVersion;
+} VBOXXPCOMC;
+/** Pointer to a const VBoxXPCOMC function table. */
+typedef VBOXXPCOMC const *PCVBOXXPCOM;
+
+/** The current interface version.
+ * For use with VBoxGetXPCOMCFunctions and to be found in
+ * VBOXXPCOMC::uVersion. */
+#define VBOX_XPCOMC_VERSION     0x00020000U
+
+VBOXXPCOMC_DECL(PCVBOXXPCOM) VBoxGetXPCOMCFunctions(unsigned uVersion);
+/** Typedef for VBoxGetXPCOMCFunctions. */
+typedef PCVBOXXPCOM (*PFNVBOXGETXPCOMCFUNCTIONS)(unsigned uVersion);
+
+/** The symbol name of VBoxGetXPCOMCFunctions. */
+#if defined(__OS2__)
+# define VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME   "_VBoxGetXPCOMCFunctions"
+#else
+# define VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME   "VBoxGetXPCOMCFunctions"
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* !___VirtualBox_CXPCOM_h */
diff --git a/src/vbox/vbox_V3_1.c b/src/vbox/vbox_V3_1.c
new file mode 100644
index 0000000..4c5b289
--- /dev/null
+++ b/src/vbox/vbox_V3_1.c
@@ -0,0 +1,37 @@
+/** @file vbox_V3_1.c
+ * C file to include support for multiple versions of VirtualBox
+ * at runtime.
+ */
+
+/*
+ * Copyright (C) 2008-2009 Sun Microsystems, Inc.
+ *
+ * This file is part of a free software library; you can redistribute
+ * it and/or modify it under the terms of the GNU Lesser General
+ * Public License version 2.1 as published by the Free Software
+ * Foundation and shipped in the "COPYING" file with this library.
+ * The library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY of any kind.
+ *
+ * Sun LGPL Disclaimer: For the avoidance of doubt, except that if
+ * any license choice other than GPL or LGPL is available it will
+ * apply instead, Sun elects to use only the Lesser General Public
+ * License version 2.1 (LGPLv2) at this time for any software where
+ * a choice of LGPL license versions is made available with the
+ * language indicating that LGPLv2 or any later version may be used,
+ * or where a choice of which version of the LGPL is applied is
+ * otherwise unspecified.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
+ * Clara, CA 95054 USA or visit http://www.sun.com if you need
+ * additional information or have any questions.
+ */
+
+#include <config.h>
+
+/** The API Version */
+#define VBOX_API_VERSION    3001
+/** Version specific prefix. */
+#define NAME(name)  vbox31##name
+
+#include "vbox_tmpl.c"
diff --git a/src/vbox/vbox_driver.c b/src/vbox/vbox_driver.c
index 450f306..ad5f239 100644
--- a/src/vbox/vbox_driver.c
+++ b/src/vbox/vbox_driver.c
@@ -45,6 +45,9 @@ extern virStorageDriver vbox22StorageDriver;
 extern virDriver vbox30Driver;
 extern virNetworkDriver vbox30NetworkDriver;
 extern virStorageDriver vbox30StorageDriver;
+extern virDriver vbox31Driver;
+extern virNetworkDriver vbox31NetworkDriver;
+extern virStorageDriver vbox31StorageDriver;
 
 static virDriver vboxDriverDummy;
 
@@ -93,6 +96,11 @@ int vboxRegister(void) {
             driver        = &vbox30Driver;
             networkDriver = &vbox30NetworkDriver;
             storageDriver = &vbox30StorageDriver;
+        } else if (uVersion >= 3000051 && uVersion < 3001051) {
+            DEBUG0("VirtualBox API version: 3.1");
+            driver        = &vbox31Driver;
+            networkDriver = &vbox31NetworkDriver;
+            storageDriver = &vbox31StorageDriver;
         } else {
             DEBUG0("Unsupport VirtualBox API version");
         }
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index cd60b72..ed4406f 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -58,6 +58,8 @@
 # include "vbox_CAPI_v2_2.h"
 #elif VBOX_API_VERSION == 3000
 # include "vbox_CAPI_v3_0.h"
+#elif VBOX_API_VERSION == 3001
+# include "vbox_CAPI_v3_1.h"
 #else
 # error "Unsupport VBOX_API_VERSION"
 #endif
@@ -66,7 +68,60 @@
 #include "vbox_XPCOMCGlue.h"
 
 
-#define VIR_FROM_THIS VIR_FROM_VBOX
+#define VIR_FROM_THIS                   VIR_FROM_VBOX
+#define VBOX_UTF16_FREE(arg)            data->pFuncs->pfnUtf16Free(arg)
+#define VBOX_UTF8_FREE(arg)             data->pFuncs->pfnUtf8Free(arg)
+#define VBOX_COM_UNALLOC_MEM(arg)       data->pFuncs->pfnComUnallocMem(arg)
+#define VBOX_UTF16_TO_UTF8(arg1, arg2)  data->pFuncs->pfnUtf16ToUtf8(arg1, arg2)
+#define VBOX_UTF8_TO_UTF16(arg1, arg2)  data->pFuncs->pfnUtf8ToUtf16(arg1, arg2)
+
+#define VBOX_RELEASE(arg) \
+if(arg)\
+    (arg)->vtbl->nsisupports.Release((nsISupports *)(arg))
+
+#define VBOX_OBJECT_CHECK(conn, type, value) \
+vboxGlobalData *data = conn->privateData;\
+type ret = value;\
+if(!data->vboxObj) {\
+    return ret;\
+}
+
+#define VBOX_OBJECT_HOST_CHECK(conn, type, value) \
+vboxGlobalData *data = conn->privateData;\
+type ret = value;\
+IHost *host = NULL;\
+if(!data->vboxObj) {\
+    return ret;\
+}\
+data->vboxObj->vtbl->GetHost(data->vboxObj, &host);\
+if (!host) {\
+    return ret;\
+}
+
+#if VBOX_API_VERSION < 3001
+
+#define VBOX_MEDIUM_RELEASE(arg) \
+if(arg)\
+    (arg)->vtbl->imedium.nsisupports.Release((nsISupports *)(arg))
+#define VBOX_MEDIUM_FUNC_ARG1(object, func, arg1) \
+    (object)->vtbl->imedium.func((IMedium *)(object), arg1)
+#define VBOX_MEDIUM_FUNC_ARG2(object, func, arg1, arg2) \
+    (object)->vtbl->imedium.func((IMedium *)(object), arg1, arg2)
+
+#else  /* VBOX_API_VERSION >= 3001 */
+
+typedef IMedium IHardDisk;
+typedef IMediumAttachment IHardDiskAttachment;
+#define MediaState_Inaccessible     MediumState_Inaccessible
+#define HardDiskVariant_Standard    MediumVariant_Standard
+#define HardDiskVariant_Fixed       MediumVariant_Fixed
+#define VBOX_MEDIUM_RELEASE(arg) VBOX_RELEASE(arg)
+#define VBOX_MEDIUM_FUNC_ARG1(object, func, arg1) \
+    (object)->vtbl->func(object, arg1)
+#define VBOX_MEDIUM_FUNC_ARG2(object, func, arg1, arg2) \
+    (object)->vtbl->func(object, arg1, arg2)
+
+#endif /* VBOX_API_VERSION >= 3001 */
 
 #define vboxError(conn, code, fmt...) \
         virReportErrorHelper(conn, VIR_FROM_VBOX, code, __FILE__, \
@@ -143,7 +198,7 @@ typedef struct {
  * them that way
  */
 
-vboxGlobalData *g_pVBoxGlobalData = NULL;
+static vboxGlobalData *g_pVBoxGlobalData = NULL;
 
 #endif /* !(VBOX_API_VERSION == 2002) */
 
@@ -282,7 +337,7 @@ static void vboxUtf8toIID(virConnectPtr conn, char *uuidstr, vboxIID **iid) {
     vboxIIDFromUUID(hddUUID, *iid);
 }
 
-#else /* !(VBOX_API_VERSION == 2002) */
+#else /* VBOX_API_VERSION != 2002 */
 
 #define vboxIIDFromUUID(uuid, iid)\
 {\
@@ -351,6 +406,265 @@ static void vboxUtf8toIID(virConnectPtr conn, char *uuidstr, vboxIID **iid) {
         virReportOOMError(conn);
 }
 
+#if VBOX_API_VERSION >= 3001
+
+/**
+ * function to generate the name for medium,
+ * for e.g: hda, sda, etc
+ *
+ * Limitation: 1) max (26+(26*26)+(26*26*26)) i.e
+ *                18,278 names for now
+ *
+ * @returns     null terminated string with device name or NULL
+ *              for failures
+ * @param       conn            Input Connection Pointer
+ * @param       storageBus      Input storage bus type
+ * @param       deviceInst      Input device instance number
+ * @param       devicePort      Input port number
+ * @param       deviceSlot      Input slot number
+ * @param       aMaxPortPerInst Input array of max port per device instance
+ * @param       aMaxSlotPerPort Input array of max slot per device port
+ *
+ */
+static char *vboxGenerateMediumName(virConnectPtr conn,
+                                    PRUint32  storageBus,
+                                    PRInt32   deviceInst,
+                                    PRInt32   devicePort,
+                                    PRInt32   deviceSlot,
+                                    PRUint32 *aMaxPortPerInst,
+                                    PRUint32 *aMaxSlotPerPort) {
+    char *name  = NULL;
+    int   len   = 4;
+    int   total = 0;
+    PRUint32 maxPortPerInst = 0;
+    PRUint32 maxSlotPerPort = 0;
+
+    if (   !aMaxPortPerInst
+        || !aMaxSlotPerPort)
+        return NULL;
+
+    maxPortPerInst = aMaxPortPerInst[storageBus];
+    maxSlotPerPort = aMaxSlotPerPort[storageBus];
+    total =   (deviceInst * maxPortPerInst * maxSlotPerPort)
+            + (devicePort * maxSlotPerPort)
+            + deviceSlot;
+
+    if ((total >= 26) && (total < 26*26 + 26))
+        len = 5;
+    else if ((total >= 26*26 + 26) && (total < 26*26*26 + 26*26 + 26))
+        len = 6;
+
+    if (VIR_ALLOC_N(name, len) < 0) {
+        virReportOOMError(conn);
+        return NULL;
+    }
+
+    if (storageBus == StorageBus_IDE) {
+        name[0] = 'h';
+        name[1] = 'd';
+    } else if (   (storageBus == StorageBus_SATA)
+               || (storageBus == StorageBus_SCSI)) {
+        name[0] = 's';
+        name[1] = 'd';
+    } else if (storageBus == StorageBus_Floppy) {
+        name[0] = 'f';
+        name[1] = 'd';
+    }
+
+    if (len == 4) {
+        name[2] = (char)(97 + total);
+    } else if (len == 5) {
+        name[2] = (char)(96 + (total / 26));
+        name[3] = (char)(97 + (total % 26));
+    } else if (len == 6) {
+        name[2] = (char)(96 + (total / 26*26));
+        name[3] = (char)(96 + ((total % (26*26)) / 26));
+        name[4] = (char)(97 + ((total % (26*26)) % 26));
+    }
+
+    name[len - 1] = '\0';
+    DEBUG("name=%s, len=%d, total=%d, storageBus=%u, deviceInst=%d, "
+          "devicePort=%d deviceSlot=%d, maxPortPerInst=%u maxSlotPerPort=%u",
+          name, len, total, storageBus, deviceInst, devicePort,
+          deviceSlot, maxPortPerInst, maxSlotPerPort);
+    return name;
+}
+
+/**
+ * function to get the StorageBus, Port number
+ * and Device number for the given devicename
+ * e.g: hda has StorageBus = IDE, port = 0,
+ *      device = 0
+ *
+ * Limitation: only name till 4 chars supported
+ *             i.e from hda till hdzz
+ *
+ * @returns     true on Success, false on failure.
+ * @param       deviceName      Input device name
+ * @param       aMaxPortPerInst Input array of max port per device instance
+ * @param       aMaxSlotPerPort Input array of max slot per device port
+ * @param       storageBus      Input storage bus type
+ * @param       deviceInst      Output device instance number
+ * @param       devicePort      Output port number
+ * @param       deviceSlot      Output slot number
+ *
+ */
+static bool vboxGetDeviceDetails(const char *deviceName,
+                                 PRUint32   *aMaxPortPerInst,
+                                 PRUint32   *aMaxSlotPerPort,
+                                 PRUint32    storageBus,
+                                 PRInt32    *deviceInst,
+                                 PRInt32    *devicePort,
+                                 PRInt32    *deviceSlot) {
+    int len   = 0;
+    int total = 0;
+    PRUint32 maxPortPerInst = 0;
+    PRUint32 maxSlotPerPort = 0;
+
+    if (   !deviceName
+        || !storageBus
+        || !deviceInst
+        || !devicePort
+        || !deviceSlot
+        || !aMaxPortPerInst
+        || !aMaxSlotPerPort)
+        return false;
+
+    len = strlen(deviceName);
+
+    /* support till hdzz only so 4 chars */
+    if (len > 4)
+        return false;
+
+    maxPortPerInst = aMaxPortPerInst[storageBus];
+    maxSlotPerPort = aMaxSlotPerPort[storageBus];
+
+    if (   !maxPortPerInst
+        || !maxSlotPerPort)
+        return false;
+
+    if (len == 3) {
+        total = (deviceName[2] - 97);
+    } else if (len == 4) {
+        total = ((deviceName[2] - 96) * 26) + (deviceName[3] - 97);
+    }
+
+    *deviceInst = total / (maxPortPerInst * maxSlotPerPort);
+    *devicePort = (total % (maxPortPerInst * maxSlotPerPort)) / maxSlotPerPort;
+    *deviceSlot = (total % (maxPortPerInst * maxSlotPerPort)) % maxSlotPerPort;
+
+    DEBUG("name=%s, len=%d, total=%d, storageBus=%u, deviceInst=%d, "
+          "devicePort=%d deviceSlot=%d, maxPortPerInst=%u maxSlotPerPort=%u",
+          deviceName, len, total, storageBus, *deviceInst, *devicePort,
+          *deviceSlot, maxPortPerInst, maxSlotPerPort);
+
+    return true;
+}
+
+/**
+ * function to get the values for max port per
+ * instance and max slots per port for the devices
+ *
+ * @returns     true on Success, false on failure.
+ * @param       vbox            Input IVirtualBox pointer
+ * @param       maxPortPerInst  Output array of max port per instance
+ * @param       maxSlotPerPort  Output array of max slot per port
+ *
+ */
+
+static bool vboxGetMaxPortSlotValues(IVirtualBox *vbox,
+                                     PRUint32 *maxPortPerInst,
+                                     PRUint32 *maxSlotPerPort) {
+    ISystemProperties *sysProps = NULL;
+
+    if (!vbox)
+        return false;
+
+    vbox->vtbl->GetSystemProperties(vbox, &sysProps);
+
+    if (!sysProps)
+        return false;
+
+    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
+                                                 StorageBus_IDE,
+                                                 &maxPortPerInst[StorageBus_IDE]);
+    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
+                                                 StorageBus_SATA,
+                                                 &maxPortPerInst[StorageBus_SATA]);
+    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
+                                                 StorageBus_SCSI,
+                                                 &maxPortPerInst[StorageBus_SCSI]);
+    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
+                                                 StorageBus_Floppy,
+                                                 &maxPortPerInst[StorageBus_Floppy]);
+
+    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
+                                                      StorageBus_IDE,
+                                                      &maxSlotPerPort[StorageBus_IDE]);
+    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
+                                                      StorageBus_SATA,
+                                                      &maxSlotPerPort[StorageBus_SATA]);
+    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
+                                                      StorageBus_SCSI,
+                                                      &maxSlotPerPort[StorageBus_SCSI]);
+    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
+                                                      StorageBus_Floppy,
+                                                      &maxSlotPerPort[StorageBus_Floppy]);
+
+    VBOX_RELEASE(sysProps);
+
+    return true;
+}
+
+/**
+ * Converts Utf-16 string to int
+ */
+static int PRUnicharToInt(PRUnichar *strUtf16) {
+    char *strUtf8 = NULL;
+    int ret = 0;
+
+    if (!strUtf16)
+        return -1;
+
+    g_pVBoxGlobalData->pFuncs->pfnUtf16ToUtf8(strUtf16, &strUtf8);
+    if (!strUtf8)
+        return -1;
+
+    ret = atoi(strUtf8);
+    g_pVBoxGlobalData->pFuncs->pfnUtf8Free(strUtf8);
+
+    return ret;
+}
+
+/**
+ * Converts int to Utf-16 string
+ */
+static PRUnichar *PRUnicharFromInt(int n) {
+    PRUnichar *strUtf16 = NULL;
+    char c, s[24] = {};
+    int sign, i = 0, j = 0;
+
+    if ((sign = n) < 0)
+        n = -n;
+    do {
+        s[i++] = n % 10 + '0';
+    } while ((n /= 10) > 0);
+    if (sign < 0)
+        s[i++] = '-';
+    s[i] = '\0';
+    for (j = 0; j < i; j++, i--) {
+        c = s[j];
+        s[j] = s[i];
+        s[i] = c;
+    }
+
+    g_pVBoxGlobalData->pFuncs->pfnUtf8ToUtf16(s, &strUtf16);
+
+    return strUtf16;
+}
+
+#endif /* VBOX_API_VERSION >= 3001 */
+
 #endif /* !(VBOX_API_VERSION == 2002) */
 
 static virCapsPtr vboxCapsInit(void) {
@@ -458,13 +772,13 @@ static int vboxExtractVersion(virConnectPtr conn, vboxGlobalData *data) {
     if (NS_SUCCEEDED(rc)) {
         char *vboxVersion = NULL;
 
-        data->pFuncs->pfnUtf16ToUtf8(versionUtf16, &vboxVersion);
+        VBOX_UTF16_TO_UTF8(versionUtf16, &vboxVersion);
 
         if (sscanf(vboxVersion, "%u.%u.%u", &major, &minor, &micro) == 3)
             ret = 0;
 
-        data->pFuncs->pfnUtf8Free(vboxVersion);
-        data->pFuncs->pfnComUnallocMem(versionUtf16);
+        VBOX_UTF8_FREE(vboxVersion);
+        VBOX_COM_UNALLOC_MEM(versionUtf16);
     } else {
         ret = -1;
     }
@@ -602,21 +916,18 @@ static int vboxIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED) {
 }
 
 static int vboxGetMaxVcpus(virConnectPtr conn, const char *type ATTRIBUTE_UNUSED) {
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, int, -1);
     PRUint32 maxCPUCount = 0;
-    int ret = -1;
 
     /* VirtualBox Supports only hvm and thus the type passed to it
      * has no meaning, setting it to ATTRIBUTE_UNUSED
      */
-    if(data->vboxObj) {
-        ISystemProperties *systemProperties = NULL;
+    ISystemProperties *systemProperties = NULL;
 
-        data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-        if (systemProperties) {
-            systemProperties->vtbl->GetMaxGuestCPUCount(systemProperties, &maxCPUCount);
-            systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-        }
+    data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+    if (systemProperties) {
+        systemProperties->vtbl->GetMaxGuestCPUCount(systemProperties, &maxCPUCount);
+        VBOX_RELEASE(systemProperties);
     }
 
     if (maxCPUCount > 0)
@@ -627,8 +938,7 @@ static int vboxGetMaxVcpus(virConnectPtr conn, const char *type ATTRIBUTE_UNUSED
 
 
 static char *vboxGetCapabilities(virConnectPtr conn) {
-    vboxGlobalData *data = conn->privateData;
-    char *ret;
+    VBOX_OBJECT_CHECK(conn, char *, NULL);
 
     vboxDriverLock(data);
     ret = virCapabilitiesFormatXML(data->caps);
@@ -638,110 +948,89 @@ static char *vboxGetCapabilities(virConnectPtr conn) {
 }
 
 static int vboxListDomains(virConnectPtr conn, int *ids, int nids) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, int, -1);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
     PRUint32 state;
-    int ret = -1;
+    nsresult rc;
     int i, j;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of Domains",(unsigned)rc);
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of Domains",(unsigned)rc);
+        goto cleanup;
+    }
 
-        if (machineCnt == 0) {
-            ret = 0;
-            goto cleanup;
-        }
+    if (machineCnt == 0) {
+        ret = 0;
+        goto cleanup;
+    }
 
-        for (i = 0,j = 0; (i < machineCnt) && (j < nids); ++i) {
-            IMachine *machine = machines[i];
-
-            if (machine) {
-                PRBool isAccessible = PR_FALSE;
-                machine->vtbl->GetAccessible(machine, &isAccessible);
-                if (isAccessible) {
-                    machine->vtbl->GetState(machine, &state);
-                    if ((state == MachineState_Starting)   ||
-                        (state == MachineState_Running)    ||
-                        (state == MachineState_Stuck)      ||
-                        (state == MachineState_Stopping)   ||
-                        (state == MachineState_Saving)     ||
-                        (state == MachineState_Restoring)  ||
-                        (state == MachineState_Discarding) ||
-                        (state == MachineState_Paused) ) {
-                        ret++;
-                        ids[j++] = i + 1;
-                    }
+    for (i = 0,j = 0; (i < machineCnt) && (j < nids); ++i) {
+        IMachine *machine = machines[i];
+
+        if (machine) {
+            PRBool isAccessible = PR_FALSE;
+            machine->vtbl->GetAccessible(machine, &isAccessible);
+            if (isAccessible) {
+                machine->vtbl->GetState(machine, &state);
+                if (   (state >= MachineState_FirstOnline)
+                    && (state <= MachineState_LastOnline) ) {
+                    ret++;
+                    ids[j++] = i + 1;
                 }
             }
         }
-        ret++;
     }
+    ret++;
 
 cleanup:
     for (i = 0; i < machineCnt; ++i)
-        if (machines[i])
-            machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+        VBOX_RELEASE(machines[i]);
     return ret;
 }
 
 static int vboxNumOfDomains(virConnectPtr conn) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, int, -1);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
     PRUint32 state;
-    int ret = -1;
+    nsresult rc;
     int i;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get number of Domains",(unsigned)rc);
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get number of Domains",(unsigned)rc);
+        goto cleanup;
+    }
 
-        if (machineCnt == 0) {
-            ret = 0;
-            goto cleanup;
-        }
+    if (machineCnt == 0) {
+        ret = 0;
+        goto cleanup;
+    }
 
-        /* Do the cleanup as required by GetMachines() */
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-
-            if (machine) {
-                PRBool isAccessible = PR_FALSE;
-                machine->vtbl->GetAccessible(machine, &isAccessible);
-                if (isAccessible) {
-                    machine->vtbl->GetState(machine, &state);
-                    if ((state == MachineState_Starting)   ||
-                        (state == MachineState_Running)    ||
-                        (state == MachineState_Stuck)      ||
-                        (state == MachineState_Stopping)   ||
-                        (state == MachineState_Saving)     ||
-                        (state == MachineState_Restoring)  ||
-                        (state == MachineState_Discarding) ||
-                        (state == MachineState_Paused) ) {
-                        ret++;
-                    }
-                }
+    /* Do the cleanup as required by GetMachines() */
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+
+        if (machine) {
+            PRBool isAccessible = PR_FALSE;
+            machine->vtbl->GetAccessible(machine, &isAccessible);
+            if (isAccessible) {
+                machine->vtbl->GetState(machine, &state);
+                if (   (state >= MachineState_FirstOnline)
+                    && (state <= MachineState_LastOnline) )
+                    ret++;
             }
         }
-        ret++;
     }
+    ret++;
 
 cleanup:
     for (i = 0; i < machineCnt; ++i)
-        if (machines[i])
-            machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+        VBOX_RELEASE(machines[i]);
     return ret;
 }
 
@@ -773,14 +1062,13 @@ cleanup:
 }
 
 static virDomainPtr vboxDomainLookupByID(virConnectPtr conn, int id) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, virDomainPtr, NULL);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
-    virDomainPtr dom     = NULL;
     vboxIID *iid         = NULL;
     unsigned char iidl[VIR_UUID_BUFLEN];
     PRUint32 state;
+    nsresult rc;
     int i;
 
     /* Internal vbox IDs start from 0, the public libvirt ID
@@ -792,242 +1080,207 @@ static virDomainPtr vboxDomainLookupByID(virConnectPtr conn, int id) {
     }
     id = id - 1;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of machines",(unsigned)rc);
-            return NULL;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of machines",(unsigned)rc);
+        return NULL;
+    }
 
-        if (id < machineCnt) {
-            if (machines[id]) {
-                PRBool isAccessible = PR_FALSE;
-                machines[id]->vtbl->GetAccessible(machines[id], &isAccessible);
-                if (isAccessible) {
-                    machines[id]->vtbl->GetState(machines[id], &state);
-                    if ((state == MachineState_Starting)   ||
-                        (state == MachineState_Running)    ||
-                        (state == MachineState_Stuck)      ||
-                        (state == MachineState_Stopping)   ||
-                        (state == MachineState_Saving)     ||
-                        (state == MachineState_Restoring)  ||
-                        (state == MachineState_Discarding) ||
-                        (state == MachineState_Paused) ) {
-                        PRUnichar *machineNameUtf16 = NULL;
-                        char      *machineNameUtf8  = NULL;
-
-                        machines[id]->vtbl->GetName(machines[id], &machineNameUtf16);
-                        data->pFuncs->pfnUtf16ToUtf8(machineNameUtf16, &machineNameUtf8);
-
-                        machines[id]->vtbl->GetId(machines[id], &iid);
-                        vboxIIDToUUID(iidl, iid);
-                        vboxIIDUnalloc(iid);
-
-                        /* get a new domain pointer from virGetDomain, if it fails
-                         * then no need to assign the id, else assign the id, cause
-                         * it is -1 by default. rest is taken care by virGetDomain
-                         * itself, so need not worry.
-                         */
+    if (id < machineCnt) {
+        if (machines[id]) {
+            PRBool isAccessible = PR_FALSE;
+            machines[id]->vtbl->GetAccessible(machines[id], &isAccessible);
+            if (isAccessible) {
+                machines[id]->vtbl->GetState(machines[id], &state);
+                if (   (state >= MachineState_FirstOnline)
+                    && (state <= MachineState_LastOnline) ) {
+                    PRUnichar *machineNameUtf16 = NULL;
+                    char      *machineNameUtf8  = NULL;
 
-                        dom = virGetDomain(conn, machineNameUtf8, iidl);
-                        if (dom)
-                            dom->id = id + 1;
+                    machines[id]->vtbl->GetName(machines[id], &machineNameUtf16);
+                    VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineNameUtf8);
 
-                        /* Cleanup all the XPCOM allocated stuff here */
-                        data->pFuncs->pfnUtf8Free(machineNameUtf8);
-                        data->pFuncs->pfnUtf16Free(machineNameUtf16);
-                    }
+                    machines[id]->vtbl->GetId(machines[id], &iid);
+                    vboxIIDToUUID(iidl, iid);
+                    vboxIIDUnalloc(iid);
+
+                    /* get a new domain pointer from virGetDomain, if it fails
+                     * then no need to assign the id, else assign the id, cause
+                     * it is -1 by default. rest is taken care by virGetDomain
+                     * itself, so need not worry.
+                     */
+
+                    ret = virGetDomain(conn, machineNameUtf8, iidl);
+                    if (ret)
+                        ret->id = id + 1;
+
+                    /* Cleanup all the XPCOM allocated stuff here */
+                    VBOX_UTF8_FREE(machineNameUtf8);
+                    VBOX_UTF16_FREE(machineNameUtf16);
                 }
             }
         }
-
-        /* Do the cleanup as required by GetMachines() */
-        for (i = 0; i < machineCnt; ++i) {
-            if (machines[i])
-                machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
-        }
     }
 
-    return dom;
+    /* Do the cleanup as required by GetMachines() */
+    for (i = 0; i < machineCnt; ++i)
+        VBOX_RELEASE(machines[i]);
+
+    return ret;
 }
 
 static virDomainPtr vboxDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, virDomainPtr, NULL);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
-    virDomainPtr dom     = NULL;
     vboxIID *iid         = NULL;
     char      *machineNameUtf8  = NULL;
     PRUnichar *machineNameUtf16 = NULL;
     unsigned char iidl[VIR_UUID_BUFLEN];
     int i, matched = 0;
+    nsresult rc;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of machines",(unsigned)rc);
-            return NULL;
-        }
-
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-            PRBool isAccessible = PR_FALSE;
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of machines",(unsigned)rc);
+        return NULL;
+    }
 
-            if (!machine)
-                continue;
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+        PRBool isAccessible = PR_FALSE;
 
-            machine->vtbl->GetAccessible(machine, &isAccessible);
-            if (isAccessible) {
+        if (!machine)
+            continue;
 
-                machine->vtbl->GetId(machine, &iid);
-                if (!iid)
-                    continue;
-                vboxIIDToUUID(iidl, iid);
-                vboxIIDUnalloc(iid);
+        machine->vtbl->GetAccessible(machine, &isAccessible);
+        if (isAccessible) {
 
-                if (memcmp(uuid, iidl, VIR_UUID_BUFLEN) == 0) {
+            machine->vtbl->GetId(machine, &iid);
+            if (!iid)
+                continue;
+            vboxIIDToUUID(iidl, iid);
+            vboxIIDUnalloc(iid);
 
-                    PRUint32 state;
+            if (memcmp(uuid, iidl, VIR_UUID_BUFLEN) == 0) {
 
-                    matched = 1;
+                PRUint32 state;
 
-                    machine->vtbl->GetName(machine, &machineNameUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(machineNameUtf16, &machineNameUtf8);
+                matched = 1;
 
-                    machine->vtbl->GetState(machine, &state);
+                machine->vtbl->GetName(machine, &machineNameUtf16);
+                VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineNameUtf8);
 
-                    /* get a new domain pointer from virGetDomain, if it fails
-                     * then no need to assign the id, else assign the id, cause
-                     * it is -1 by default. rest is taken care by virGetDomain
-                     * itself, so need not worry.
-                     */
+                machine->vtbl->GetState(machine, &state);
 
-                    dom = virGetDomain(conn, machineNameUtf8, iidl);
-                    if (dom)
-                        if ((state == MachineState_Starting)   ||
-                            (state == MachineState_Running)    ||
-                            (state == MachineState_Stuck)      ||
-                            (state == MachineState_Stopping)   ||
-                            (state == MachineState_Saving)     ||
-                            (state == MachineState_Restoring)  ||
-                            (state == MachineState_Discarding) ||
-                            (state == MachineState_Paused) )
-                            dom->id = i + 1;
-                }
+                /* get a new domain pointer from virGetDomain, if it fails
+                 * then no need to assign the id, else assign the id, cause
+                 * it is -1 by default. rest is taken care by virGetDomain
+                 * itself, so need not worry.
+                 */
 
-                if (matched == 1)
-                    break;
+                ret = virGetDomain(conn, machineNameUtf8, iidl);
+                if (   ret
+                    && (state >= MachineState_FirstOnline)
+                    && (state <= MachineState_LastOnline) )
+                    ret->id = i + 1;
             }
-        }
 
-        /* Do the cleanup and take care you dont leak any memory */
-        if (machineNameUtf8)
-            data->pFuncs->pfnUtf8Free(machineNameUtf8);
-        if (machineNameUtf16)
-            data->pFuncs->pfnComUnallocMem(machineNameUtf16);
-        for (i = 0; i < machineCnt; ++i) {
-            if (machines[i])
-                machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+            if (matched == 1)
+                break;
         }
     }
 
-    return dom;
+    /* Do the cleanup and take care you dont leak any memory */
+    VBOX_UTF8_FREE(machineNameUtf8);
+    VBOX_COM_UNALLOC_MEM(machineNameUtf16);
+    for (i = 0; i < machineCnt; ++i)
+        VBOX_RELEASE(machines[i]);
+
+    return ret;
 }
 
 static virDomainPtr vboxDomainLookupByName(virConnectPtr conn, const char *name) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, virDomainPtr, NULL);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
-    virDomainPtr dom     = NULL;
     vboxIID *iid         = NULL;
     char      *machineNameUtf8  = NULL;
     PRUnichar *machineNameUtf16 = NULL;
     unsigned char iidl[VIR_UUID_BUFLEN];
     int i, matched = 0;
+    nsresult rc;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of machines",(unsigned)rc);
-            return NULL;
-        }
-
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-            PRBool isAccessible = PR_FALSE;
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of machines",(unsigned)rc);
+        return NULL;
+    }
 
-            if (!machine)
-                continue;
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+        PRBool isAccessible = PR_FALSE;
 
-            machine->vtbl->GetAccessible(machine, &isAccessible);
-            if (isAccessible) {
+        if (!machine)
+            continue;
 
-                machine->vtbl->GetName(machine, &machineNameUtf16);
-                data->pFuncs->pfnUtf16ToUtf8(machineNameUtf16, &machineNameUtf8);
+        machine->vtbl->GetAccessible(machine, &isAccessible);
+        if (isAccessible) {
 
-                if (STREQ(name, machineNameUtf8)) {
+            machine->vtbl->GetName(machine, &machineNameUtf16);
+            VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineNameUtf8);
 
-                    PRUint32 state;
+            if (STREQ(name, machineNameUtf8)) {
 
-                    matched = 1;
+                PRUint32 state;
 
-                    machine->vtbl->GetId(machine, &iid);
-                    vboxIIDToUUID(iidl, iid);
-                    vboxIIDUnalloc(iid);
+                matched = 1;
 
-                    machine->vtbl->GetState(machine, &state);
+                machine->vtbl->GetId(machine, &iid);
+                vboxIIDToUUID(iidl, iid);
+                vboxIIDUnalloc(iid);
 
-                    /* get a new domain pointer from virGetDomain, if it fails
-                     * then no need to assign the id, else assign the id, cause
-                     * it is -1 by default. rest is taken care by virGetDomain
-                     * itself, so need not worry.
-                     */
+                machine->vtbl->GetState(machine, &state);
 
-                    dom = virGetDomain(conn, machineNameUtf8, iidl);
-                    if (dom)
-                        if ((state == MachineState_Starting)   ||
-                            (state == MachineState_Running)    ||
-                            (state == MachineState_Stuck)      ||
-                            (state == MachineState_Stopping)   ||
-                            (state == MachineState_Saving)     ||
-                            (state == MachineState_Restoring)  ||
-                            (state == MachineState_Discarding) ||
-                            (state == MachineState_Paused) )
-                            dom->id = i + 1;
-                }
+                /* get a new domain pointer from virGetDomain, if it fails
+                 * then no need to assign the id, else assign the id, cause
+                 * it is -1 by default. rest is taken care by virGetDomain
+                 * itself, so need not worry.
+                 */
 
-                if (machineNameUtf8) {
-                    data->pFuncs->pfnUtf8Free(machineNameUtf8);
-                    machineNameUtf8 = NULL;
-                }
-                if (machineNameUtf16) {
-                    data->pFuncs->pfnComUnallocMem(machineNameUtf16);
-                    machineNameUtf16 = NULL;
-                }
-                if (matched == 1)
-                    break;
+                ret = virGetDomain(conn, machineNameUtf8, iidl);
+                if (   ret
+                    && (state >= MachineState_FirstOnline)
+                    && (state <= MachineState_LastOnline) )
+                    ret->id = i + 1;
             }
-        }
 
-        /* Do the cleanup and take care you dont leak any memory */
-        for (i = 0; i < machineCnt; ++i) {
-            if (machines[i])
-                machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+            if (machineNameUtf8) {
+                VBOX_UTF8_FREE(machineNameUtf8);
+                machineNameUtf8 = NULL;
+            }
+            if (machineNameUtf16) {
+                VBOX_COM_UNALLOC_MEM(machineNameUtf16);
+                machineNameUtf16 = NULL;
+            }
+            if (matched == 1)
+                break;
         }
     }
 
-    return dom;
+    /* Do the cleanup and take care you dont leak any memory */
+    for (i = 0; i < machineCnt; ++i)
+        VBOX_RELEASE(machines[i]);
+
+    return ret;
 }
 
 
 static int vboxDomainIsActive(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
     vboxIID *iid         = NULL;
@@ -1035,90 +1288,78 @@ static int vboxDomainIsActive(virDomainPtr dom) {
     PRUnichar *machineNameUtf16 = NULL;
     unsigned char iidl[VIR_UUID_BUFLEN];
     int i, matched = 0;
-    int ret = -1;
+    nsresult rc;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of machines",(unsigned)rc);
-            return -1;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of machines",(unsigned)rc);
+        return ret;
+    }
 
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-            PRBool isAccessible = PR_FALSE;
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+        PRBool isAccessible = PR_FALSE;
 
-            if (!machine)
-                continue;
+        if (!machine)
+            continue;
 
-            machine->vtbl->GetAccessible(machine, &isAccessible);
-            if (isAccessible) {
+        machine->vtbl->GetAccessible(machine, &isAccessible);
+        if (isAccessible) {
 
-                machine->vtbl->GetId(machine, &iid);
-                if (!iid)
-                    continue;
-                vboxIIDToUUID(iidl, iid);
-                vboxIIDUnalloc(iid);
+            machine->vtbl->GetId(machine, &iid);
+            if (!iid)
+                continue;
+            vboxIIDToUUID(iidl, iid);
+            vboxIIDUnalloc(iid);
 
-                if (memcmp(dom->uuid, iidl, VIR_UUID_BUFLEN) == 0) {
+            if (memcmp(dom->uuid, iidl, VIR_UUID_BUFLEN) == 0) {
 
-                    PRUint32 state;
+                PRUint32 state;
 
-                    matched = 1;
+                matched = 1;
 
-                    machine->vtbl->GetName(machine, &machineNameUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(machineNameUtf16, &machineNameUtf8);
-
-                    machine->vtbl->GetState(machine, &state);
-
-                    if ((state == MachineState_Starting)   ||
-                        (state == MachineState_Running)    ||
-                        (state == MachineState_Stuck)      ||
-                        (state == MachineState_Stopping)   ||
-                        (state == MachineState_Saving)     ||
-                        (state == MachineState_Restoring)  ||
-                        (state == MachineState_Discarding) ||
-                        (state == MachineState_Paused) )
-                        ret = 1;
-                    else
-                        ret = 0;
-                }
+                machine->vtbl->GetName(machine, &machineNameUtf16);
+                VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineNameUtf8);
 
-                if (matched == 1)
-                    break;
+                machine->vtbl->GetState(machine, &state);
+
+                if (   (state >= MachineState_FirstOnline)
+                    && (state <= MachineState_LastOnline) )
+                    ret = 1;
+                else
+                    ret = 0;
             }
-        }
 
-        /* Do the cleanup and take care you dont leak any memory */
-        if (machineNameUtf8)
-            data->pFuncs->pfnUtf8Free(machineNameUtf8);
-        if (machineNameUtf16)
-            data->pFuncs->pfnComUnallocMem(machineNameUtf16);
-        for (i = 0; i < machineCnt; ++i) {
-            if (machines[i])
-                machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+            if (matched == 1)
+                break;
         }
     }
 
+    /* Do the cleanup and take care you dont leak any memory */
+    VBOX_UTF8_FREE(machineNameUtf8);
+    VBOX_COM_UNALLOC_MEM(machineNameUtf16);
+    for (i = 0; i < machineCnt; ++i)
+        VBOX_RELEASE(machines[i]);
+
     return ret;
 }
 
 
 static int vboxDomainIsPersistent(virDomainPtr dom ATTRIBUTE_UNUSED) {
-    /* XXX All domains appear to be persistent. Is this true ? */
+    /* All domains are persistent. */
     return 1;
 }
 
 
 static int vboxDomainSuspend(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     IConsole *console    = NULL;
+    PRBool isAccessible  = PR_FALSE;
     PRUint32 state;
-    int ret = -1;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1127,62 +1368,55 @@ static int vboxDomainSuspend(virDomainPtr dom) {
     }
 #endif
 
-    if (data->vboxObj) {
-        PRBool isAccessible = PR_FALSE;
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                        "no domain with matching id %d", dom->id);
+        goto cleanup;
+    }
 
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                            "no domain with matching id %d", dom->id);
-            goto cleanup;
-        }
+    if (!machine)
+        goto cleanup;
 
-        if (!machine)
-            goto cleanup;
+    machine->vtbl->GetAccessible(machine, &isAccessible);
+    if (isAccessible) {
+        machine->vtbl->GetState(machine, &state);
 
-        machine->vtbl->GetAccessible(machine, &isAccessible);
-        if (isAccessible) {
-            machine->vtbl->GetState(machine, &state);
-
-            if (state == MachineState_Running) {
-                 /* set state pause */
-                data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-                data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-                if (console) {
-                    console->vtbl->Pause(console);
-                    console->vtbl->nsisupports.Release((nsISupports *)console);
-                    ret = 0;
-                } else {
-                    vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                              "%s", "error while suspend the domain");
-                    goto cleanup;
-                }
-                data->vboxSession->vtbl->Close(data->vboxSession);
+        if (state == MachineState_Running) {
+             /* set state pause */
+            data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+            data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
+            if (console) {
+                console->vtbl->Pause(console);
+                VBOX_RELEASE(console);
+                ret = 0;
             } else {
                 vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "machine not in running state to suspend it");
+                          "%s", "error while suspend the domain");
                 goto cleanup;
             }
+            data->vboxSession->vtbl->Close(data->vboxSession);
+        } else {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "machine not in running state to suspend it");
+            goto cleanup;
         }
     }
 
 cleanup:
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
-
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     return ret;
 }
 
 static int vboxDomainResume(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     IConsole *console    = NULL;
     PRUint32 state       = MachineState_Null;
-    int ret = -1;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1191,62 +1425,58 @@ static int vboxDomainResume(virDomainPtr dom) {
     }
 #endif
 
-    if (data->vboxObj) {
-        PRBool isAccessible = PR_FALSE;
+    PRBool isAccessible = PR_FALSE;
 
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                            "no domain with matching id %d", dom->id);
-            goto cleanup;
-        }
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                        "no domain with matching id %d", dom->id);
+        goto cleanup;
+    }
 
-        if (!machine)
-            goto cleanup;
+    if (!machine)
+        goto cleanup;
 
-        machine->vtbl->GetAccessible(machine, &isAccessible);
-        if (isAccessible) {
-            machine->vtbl->GetState(machine, &state);
-
-            if (state == MachineState_Paused) {
-                 /* resume the machine here */
-                data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-                data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-                if (console) {
-                    console->vtbl->Resume(console);
-                    console->vtbl->nsisupports.Release((nsISupports *)console);
-                    ret = 0;
-                } else {
-                    vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                              "%s", "error while resuming the domain");
-                    goto cleanup;
-                }
-                data->vboxSession->vtbl->Close(data->vboxSession);
+    machine->vtbl->GetAccessible(machine, &isAccessible);
+    if (isAccessible) {
+        machine->vtbl->GetState(machine, &state);
+
+        if (state == MachineState_Paused) {
+             /* resume the machine here */
+            data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+            data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
+            if (console) {
+                console->vtbl->Resume(console);
+                VBOX_RELEASE(console);
+                ret = 0;
             } else {
                 vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "machine not paused, so can't resume it");
+                          "%s", "error while resuming the domain");
                 goto cleanup;
             }
+            data->vboxSession->vtbl->Close(data->vboxSession);
+        } else {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "machine not paused, so can't resume it");
+            goto cleanup;
         }
     }
 
 cleanup:
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
-
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     return ret;
 }
 
 static int vboxDomainShutdown(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     IConsole *console    = NULL;
     PRUint32 state       = MachineState_Null;
-    int ret = -1;
+    PRBool isAccessible  = PR_FALSE;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1255,61 +1485,55 @@ static int vboxDomainShutdown(virDomainPtr dom) {
     }
 #endif
 
-    if(data->vboxObj) {
-        PRBool isAccessible = PR_FALSE;
-
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                            "no domain with matching id %d", dom->id);
-            goto cleanup;
-        }
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                        "no domain with matching id %d", dom->id);
+        goto cleanup;
+    }
 
-        if (!machine)
-            goto cleanup;
+    if (!machine)
+        goto cleanup;
 
-        machine->vtbl->GetAccessible(machine, &isAccessible);
-        if (isAccessible) {
-            machine->vtbl->GetState(machine, &state);
+    machine->vtbl->GetAccessible(machine, &isAccessible);
+    if (isAccessible) {
+        machine->vtbl->GetState(machine, &state);
 
-            if (state == MachineState_Paused) {
-                vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "machine paused, so can't power it down");
-                goto cleanup;
-            } else if (state == MachineState_PoweredOff) {
-                vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "machine already powered down");
-                goto cleanup;
-            }
+        if (state == MachineState_Paused) {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "machine paused, so can't power it down");
+            goto cleanup;
+        } else if (state == MachineState_PoweredOff) {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "machine already powered down");
+            goto cleanup;
+        }
 
-            data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-            data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-            if (console) {
-                console->vtbl->PowerButton(console);
-                console->vtbl->nsisupports.Release((nsISupports *)console);
-                ret = 0;
-            }
-            data->vboxSession->vtbl->Close(data->vboxSession);
+        data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+        data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
+        if (console) {
+            console->vtbl->PowerButton(console);
+            VBOX_RELEASE(console);
+            ret = 0;
         }
+        data->vboxSession->vtbl->Close(data->vboxSession);
     }
 
 cleanup:
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
-
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     return ret;
 }
 
 static int vboxDomainReboot(virDomainPtr dom, unsigned int flags ATTRIBUTE_UNUSED) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     IConsole *console    = NULL;
     PRUint32 state       = MachineState_Null;
-    int ret = -1;
+    PRBool isAccessible  = PR_FALSE;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1318,57 +1542,51 @@ static int vboxDomainReboot(virDomainPtr dom, unsigned int flags ATTRIBUTE_UNUSE
     }
 #endif
 
-    if(data->vboxObj) {
-        PRBool isAccessible = PR_FALSE;
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                        "no domain with matching id %d", dom->id);
+        goto cleanup;
+    }
 
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                            "no domain with matching id %d", dom->id);
-            goto cleanup;
-        }
+    if (!machine)
+        goto cleanup;
 
-        if (!machine)
-            goto cleanup;
+    machine->vtbl->GetAccessible(machine, &isAccessible);
+    if (isAccessible) {
+        machine->vtbl->GetState(machine, &state);
 
-        machine->vtbl->GetAccessible(machine, &isAccessible);
-        if (isAccessible) {
-            machine->vtbl->GetState(machine, &state);
-
-            if (state == MachineState_Running) {
-                data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-                data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-                if (console) {
-                    console->vtbl->Reset(console);
-                    console->vtbl->nsisupports.Release((nsISupports *)console);
-                    ret = 0;
-                }
-                data->vboxSession->vtbl->Close(data->vboxSession);
-            } else {
-                vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "machine not running, so can't reboot it");
-                goto cleanup;
+        if (state == MachineState_Running) {
+            data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+            data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
+            if (console) {
+                console->vtbl->Reset(console);
+                VBOX_RELEASE(console);
+                ret = 0;
             }
+            data->vboxSession->vtbl->Close(data->vboxSession);
+        } else {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "machine not running, so can't reboot it");
+            goto cleanup;
         }
     }
 
 cleanup:
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
-
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     return ret;
 }
 
 static int vboxDomainDestroy(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     IConsole *console    = NULL;
     PRUint32 state       = MachineState_Null;
-    int ret = -1;
+    PRBool isAccessible  = PR_FALSE;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1377,55 +1595,49 @@ static int vboxDomainDestroy(virDomainPtr dom) {
     }
 #endif
 
-    if(data->vboxObj) {
-        PRBool isAccessible = PR_FALSE;
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                        "no domain with matching id %d", dom->id);
+        goto cleanup;
+    }
 
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                            "no domain with matching id %d", dom->id);
-            goto cleanup;
-        }
+    if (!machine)
+        goto cleanup;
 
-        if (!machine)
-            goto cleanup;
+    machine->vtbl->GetAccessible(machine, &isAccessible);
+    if (isAccessible) {
+        machine->vtbl->GetState(machine, &state);
 
-        machine->vtbl->GetAccessible(machine, &isAccessible);
-        if (isAccessible) {
-            machine->vtbl->GetState(machine, &state);
-
-            if (state == MachineState_PoweredOff) {
-                vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "machine already powered down");
-                goto cleanup;
-            }
+        if (state == MachineState_PoweredOff) {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "machine already powered down");
+            goto cleanup;
+        }
 
-            data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-            data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-            if (console) {
+        data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+        data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
+        if (console) {
 
 #if VBOX_API_VERSION == 2002
-                console->vtbl->PowerDown(console);
+            console->vtbl->PowerDown(console);
 #else
-                IProgress *progress;
-                console->vtbl->PowerDown(console, &progress);
-                if (progress) {
-                    progress->vtbl->WaitForCompletion(progress, -1);
-                    progress->vtbl->nsisupports.Release((nsISupports *)progress);
-                }
-#endif
-                console->vtbl->nsisupports.Release((nsISupports *)console);
-                ret = 0;
+            IProgress *progress;
+            console->vtbl->PowerDown(console, &progress);
+            if (progress) {
+                progress->vtbl->WaitForCompletion(progress, -1);
+                VBOX_RELEASE(progress);
             }
-            data->vboxSession->vtbl->Close(data->vboxSession);
+#endif
+            VBOX_RELEASE(console);
+            ret = 0;
         }
+        data->vboxSession->vtbl->Close(data->vboxSession);
     }
 
 cleanup:
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
-
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     return ret;
 }
@@ -1445,12 +1657,12 @@ static char *vboxDomainGetOSType(virDomainPtr dom) {
 }
 
 static int vboxDomainSetMemory(virDomainPtr dom, unsigned long memory) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     PRUint32 state       = MachineState_Null;
-    int ret = -1;
+    PRBool isAccessible  = PR_FALSE;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1459,176 +1671,164 @@ static int vboxDomainSetMemory(virDomainPtr dom, unsigned long memory) {
     }
 #endif
 
-    if(data->vboxObj) {
-        PRBool isAccessible = PR_FALSE;
-
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                            "no domain with matching id %d", dom->id);
-            goto cleanup;
-        }
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                        "no domain with matching id %d", dom->id);
+        goto cleanup;
+    }
 
-        if (!machine)
-            goto cleanup;
+    if (!machine)
+        goto cleanup;
 
-        machine->vtbl->GetAccessible(machine, &isAccessible);
-        if (isAccessible) {
-            machine->vtbl->GetState(machine, &state);
+    machine->vtbl->GetAccessible(machine, &isAccessible);
+    if (isAccessible) {
+        machine->vtbl->GetState(machine, &state);
 
-            if (state != MachineState_PoweredOff) {
-                vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                          "%s", "memory size can't be changed unless domain is powered down");
-                goto cleanup;
-            }
+        if (state != MachineState_PoweredOff) {
+            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                      "%s", "memory size can't be changed unless domain is powered down");
+            goto cleanup;
+        }
 
-            rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
-            if (NS_SUCCEEDED(rc)) {
-                rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-                if (NS_SUCCEEDED(rc) && machine) {
+        rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
+        if (NS_SUCCEEDED(rc)) {
+            rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
+            if (NS_SUCCEEDED(rc) && machine) {
 
-                    rc = machine->vtbl->SetMemorySize(machine, memory / 1024);
-                    if (NS_SUCCEEDED(rc)) {
-                        machine->vtbl->SaveSettings(machine);
-                        ret = 0;
-                    } else {
-                        vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%lu Kb, rc=%08x",
-                                  "could not set the memory size of the domain to",
-                                  memory, (unsigned)rc);
-                    }
+                rc = machine->vtbl->SetMemorySize(machine, memory / 1024);
+                if (NS_SUCCEEDED(rc)) {
+                    machine->vtbl->SaveSettings(machine);
+                    ret = 0;
+                } else {
+                    vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%lu Kb, rc=%08x",
+                              "could not set the memory size of the domain to",
+                              memory, (unsigned)rc);
                 }
-                data->vboxSession->vtbl->Close(data->vboxSession);
             }
+            data->vboxSession->vtbl->Close(data->vboxSession);
         }
     }
 
 cleanup:
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
-
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     return ret;
 }
 
 static int vboxDomainGetInfo(virDomainPtr dom, virDomainInfoPtr info) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
     char *machineName    = NULL;
     PRUnichar *machineNameUtf16 = NULL;
-    int i, ret = -1;
+    nsresult rc;
+    int i = 0;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(NULL, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of machines",(unsigned)rc);
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(NULL, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of machines",(unsigned)rc);
+        goto cleanup;
+    }
 
-        info->nrVirtCpu = 0;
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-            PRBool isAccessible = PR_FALSE;
+    info->nrVirtCpu = 0;
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+        PRBool isAccessible = PR_FALSE;
 
-            if (!machine)
-                continue;
+        if (!machine)
+            continue;
 
-            machine->vtbl->GetAccessible(machine, &isAccessible);
-            if (isAccessible) {
+        machine->vtbl->GetAccessible(machine, &isAccessible);
+        if (isAccessible) {
 
-                machine->vtbl->GetName(machine, &machineNameUtf16);
-                data->pFuncs->pfnUtf16ToUtf8(machineNameUtf16, &machineName);
-
-                if (STREQ(dom->name, machineName)) {
-                    /* Get the Machine State (also match it with
-                    * virDomainState). Get the Machine memory and
-                    * for time being set maxmem and memory to same
-                    * Also since there is no direct way of checking
-                    * the cputime required (one condition being the
-                    * VM is remote), return zero for cputime. Get the
-                    * number of CPU (This is 1 for current
-                    * VirtualBox builds).
-                    */
-                    PRUint32 CPUCount   = 0;
-                    PRUint32 memorySize = 0;
-                    PRUint32 state      = MachineState_Null;
-                    PRUint32 maxMemorySize = 4 * 1024;
-                    ISystemProperties *systemProperties = NULL;
-
-                    data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-                    if (systemProperties) {
-                        systemProperties->vtbl->GetMaxGuestRAM(systemProperties, &maxMemorySize);
-                        systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-                        systemProperties = NULL;
-                    }
+            machine->vtbl->GetName(machine, &machineNameUtf16);
+            VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName);
+
+            if (STREQ(dom->name, machineName)) {
+                /* Get the Machine State (also match it with
+                * virDomainState). Get the Machine memory and
+                * for time being set maxmem and memory to same
+                * Also since there is no direct way of checking
+                * the cputime required (one condition being the
+                * VM is remote), return zero for cputime. Get the
+                * number of CPU.
+                */
+                PRUint32 CPUCount   = 0;
+                PRUint32 memorySize = 0;
+                PRUint32 state      = MachineState_Null;
+                PRUint32 maxMemorySize = 4 * 1024;
+                ISystemProperties *systemProperties = NULL;
+
+                data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+                if (systemProperties) {
+                    systemProperties->vtbl->GetMaxGuestRAM(systemProperties, &maxMemorySize);
+                    VBOX_RELEASE(systemProperties);
+                    systemProperties = NULL;
+                }
 
 
-                    machine->vtbl->GetCPUCount(machine, &CPUCount);
-                    machine->vtbl->GetMemorySize(machine, &memorySize);
-                    machine->vtbl->GetState(machine, &state);
-
-                    info->cpuTime = 0;
-                    info->nrVirtCpu = CPUCount;
-                    info->memory = memorySize * 1024;
-                    info->maxMem = maxMemorySize * 1024;
-                    switch(state) {
-                        case MachineState_Running:
-                            info->state = VIR_DOMAIN_RUNNING;
-                            break;
-                        case MachineState_Stuck:
-                            info->state = VIR_DOMAIN_BLOCKED;
-                            break;
-                        case MachineState_Paused:
-                            info->state = VIR_DOMAIN_PAUSED;
-                            break;
-                        case MachineState_Stopping:
-                            info->state = VIR_DOMAIN_SHUTDOWN;
-                            break;
-                        case MachineState_PoweredOff:
-                            info->state = VIR_DOMAIN_SHUTOFF;
-                            break;
-                        case MachineState_Aborted:
-                            info->state = VIR_DOMAIN_CRASHED;
-                            break;
-                        case MachineState_Null:
-                        default:
-                            info->state = VIR_DOMAIN_NOSTATE;
-                            break;
-                    }
+                machine->vtbl->GetCPUCount(machine, &CPUCount);
+                machine->vtbl->GetMemorySize(machine, &memorySize);
+                machine->vtbl->GetState(machine, &state);
+
+                info->cpuTime = 0;
+                info->nrVirtCpu = CPUCount;
+                info->memory = memorySize * 1024;
+                info->maxMem = maxMemorySize * 1024;
+                switch(state) {
+                    case MachineState_Running:
+                        info->state = VIR_DOMAIN_RUNNING;
+                        break;
+                    case MachineState_Stuck:
+                        info->state = VIR_DOMAIN_BLOCKED;
+                        break;
+                    case MachineState_Paused:
+                        info->state = VIR_DOMAIN_PAUSED;
+                        break;
+                    case MachineState_Stopping:
+                        info->state = VIR_DOMAIN_SHUTDOWN;
+                        break;
+                    case MachineState_PoweredOff:
+                        info->state = VIR_DOMAIN_SHUTOFF;
+                        break;
+                    case MachineState_Aborted:
+                        info->state = VIR_DOMAIN_CRASHED;
+                        break;
+                    case MachineState_Null:
+                    default:
+                        info->state = VIR_DOMAIN_NOSTATE;
+                        break;
                 }
 
-                if (machineName)
-                    data->pFuncs->pfnUtf8Free(machineName);
-                if (machineNameUtf16)
-                    data->pFuncs->pfnComUnallocMem(machineNameUtf16);
-                if (info->nrVirtCpu)
-                    break;
+                ret = 0;
             }
 
+            if (machineName)
+                VBOX_UTF8_FREE(machineName);
+            if (machineNameUtf16)
+                VBOX_COM_UNALLOC_MEM(machineNameUtf16);
+            if (info->nrVirtCpu)
+                break;
         }
 
-        /* Do the cleanup and take care you dont leak any memory */
-        for (i = 0; i < machineCnt; ++i) {
-            if (machines[i])
-                machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
-        }
     }
 
-    ret = 0;
+    /* Do the cleanup and take care you dont leak any memory */
+    for (i = 0; i < machineCnt; ++i)
+        VBOX_RELEASE(machines[i]);
 
 cleanup:
     return ret;
 }
 
 static int vboxDomainSave(virDomainPtr dom, const char *path ATTRIBUTE_UNUSED) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IConsole *console    = NULL;
     vboxIID  *iid        = NULL;
-    int ret = -1;
+    nsresult rc;
 
     /* VirtualBox currently doesn't support saving to a file
      * at a location other then the machine folder and thus
@@ -1643,40 +1843,37 @@ static int vboxDomainSave(virDomainPtr dom, const char *path ATTRIBUTE_UNUSED) {
     }
 #endif
 
-    if(data->vboxObj) {
-
-        /* Open a Session for the machine */
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-        if (NS_SUCCEEDED(rc)) {
-            rc = data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-            if (NS_SUCCEEDED(rc) && console) {
-                IProgress *progress = NULL;
+    /* Open a Session for the machine */
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+    if (NS_SUCCEEDED(rc)) {
+        rc = data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
+        if (NS_SUCCEEDED(rc) && console) {
+            IProgress *progress = NULL;
 
-                console->vtbl->SaveState(console, &progress);
+            console->vtbl->SaveState(console, &progress);
 
-                if (progress) {
+            if (progress) {
 #if VBOX_API_VERSION == 2002
-                    nsresult resultCode;
+                nsresult resultCode;
 #else
-                    PRInt32 resultCode;
+                PRInt32 resultCode;
 #endif
 
-                    progress->vtbl->WaitForCompletion(progress, -1);
-                    progress->vtbl->GetResultCode(progress, &resultCode);
-                    if (NS_SUCCEEDED(resultCode)) {
-                        ret = 0;
-                    }
-                    progress->vtbl->nsisupports.Release((nsISupports *)progress);
+                progress->vtbl->WaitForCompletion(progress, -1);
+                progress->vtbl->GetResultCode(progress, &resultCode);
+                if (NS_SUCCEEDED(resultCode)) {
+                    ret = 0;
                 }
-                console->vtbl->nsisupports.Release((nsISupports *)console);
+                VBOX_RELEASE(progress);
             }
-            data->vboxSession->vtbl->Close(data->vboxSession);
+            VBOX_RELEASE(console);
         }
-
-        DEBUGIID("UUID of machine being saved:", iid);
+        data->vboxSession->vtbl->Close(data->vboxSession);
     }
 
+    DEBUGIID("UUID of machine being saved:", iid);
+
 #if VBOX_API_VERSION == 2002
 cleanup:
 #endif
@@ -1685,12 +1882,11 @@ cleanup:
 }
 
 static int vboxDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     PRUint32  CPUCount   = nvcpus;
-    int       ret        = -1;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1699,34 +1895,31 @@ static int vboxDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
     }
 #endif
 
-    if(data->vboxObj) {
-
-        vboxIIDFromUUID(dom->uuid, iid);
+    vboxIIDFromUUID(dom->uuid, iid);
 
-        rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
-        if (NS_SUCCEEDED(rc)) {
-            data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-            if (machine) {
-                rc = machine->vtbl->SetCPUCount(machine, CPUCount);
-                if (NS_SUCCEEDED(rc)) {
-                    machine->vtbl->SaveSettings(machine);
-                    ret = 0;
-                } else {
-                    vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR, "%s: %u, rc=%08x",
-                              "could not set the number of cpus of the domain to",
-                              CPUCount, (unsigned)rc);
-                }
-                machine->vtbl->nsisupports.Release((nsISupports *)machine);
+    rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
+    if (NS_SUCCEEDED(rc)) {
+        data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
+        if (machine) {
+            rc = machine->vtbl->SetCPUCount(machine, CPUCount);
+            if (NS_SUCCEEDED(rc)) {
+                machine->vtbl->SaveSettings(machine);
+                ret = 0;
             } else {
-                vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                          "no domain with matching id %d", dom->id);
+                vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR, "%s: %u, rc=%08x",
+                          "could not set the number of cpus of the domain to",
+                          CPUCount, (unsigned)rc);
             }
+            VBOX_RELEASE(machine);
         } else {
             vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
-                      "can't open session to the domain with id %d", dom->id);
+                      "no domain with matching id %d", dom->id);
         }
-        data->vboxSession->vtbl->Close(data->vboxSession);
+    } else {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,
+                  "can't open session to the domain with id %d", dom->id);
     }
+    data->vboxSession->vtbl->Close(data->vboxSession);
 
 #if VBOX_API_VERSION == 2002
 cleanup:
@@ -1736,22 +1929,19 @@ cleanup:
 }
 
 static int vboxDomainGetMaxVcpus(virDomainPtr dom) {
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
+    ISystemProperties *systemProperties = NULL;
     PRUint32 maxCPUCount = 0;
-    int ret = -1;
 
     /* Currently every domain supports the same number of max cpus
      * as that supported by vbox and thus take it directly from
      * the systemproperties.
      */
-    if(data->vboxObj) {
-        ISystemProperties *systemProperties = NULL;
 
-        data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-        if (systemProperties) {
-            systemProperties->vtbl->GetMaxGuestCPUCount(systemProperties, &maxCPUCount);
-            systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-        }
+    data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+    if (systemProperties) {
+        systemProperties->vtbl->GetMaxGuestCPUCount(systemProperties, &maxCPUCount);
+        VBOX_RELEASE(systemProperties);
     }
 
     if (maxCPUCount > 0)
@@ -1761,13 +1951,12 @@ static int vboxDomainGetMaxVcpus(virDomainPtr dom) {
 }
 
 static char *vboxDomainDumpXML(virDomainPtr dom, int flags) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, char *, NULL);
     virDomainDefPtr def  = NULL;
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
-    char *ret            = NULL;
     int gotAllABoutDef   = -1;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -1781,608 +1970,821 @@ static char *vboxDomainDumpXML(virDomainPtr dom, int flags) {
         goto cleanup;
     }
 
-    if(data->vboxObj) {
-
-        vboxIIDFromUUID(dom->uuid, iid);
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_SUCCEEDED(rc) && machine) {
-            PRBool accessible = PR_FALSE;
-
-            machine->vtbl->GetAccessible(machine, &accessible);
-            if (accessible) {
-                int i = 0;
-                struct utsname utsname;
-                PRBool PAEEnabled                   = PR_FALSE;
-                PRBool ACPIEnabled                  = PR_FALSE;
-                PRBool IOAPICEnabled                = PR_FALSE;
-                PRBool VRDPEnabled                  = PR_FALSE;
-                PRInt32 hddNum                      = 0;
-                PRUint32 CPUCount                   = 0;
-                PRUint32 memorySize                 = 0;
-                PRUint32 netAdpCnt                  = 0;
-                PRUint32 netAdpIncCnt               = 0;
-                PRUint32 maxMemorySize              = 4 * 1024;
-                PRUint32 USBFilterCount             = 0;
-                PRUint32 maxBootPosition            = 0;
-                PRUint32 serialPortCount            = 0;
-                PRUint32 serialPortIncCount         = 0;
-                PRUint32 parallelPortCount          = 0;
-                PRUint32 parallelPortIncCount       = 0;
-                IBIOSSettings *bios                 = NULL;
-                IDVDDrive *dvdDrive                 = NULL;
-                IHardDisk *hardDiskPM               = NULL;
-                IHardDisk *hardDiskPS               = NULL;
-                IHardDisk *hardDiskSS               = NULL;
-                PRUnichar *hddBusUtf16              = NULL;
-                IVRDPServer *VRDPServer             = NULL;
-                IFloppyDrive *floppyDrive           = NULL;
-                IAudioAdapter *audioAdapter         = NULL;
-                IUSBController *USBController       = NULL;
-                ISystemProperties *systemProperties = NULL;
-                char *hddBus = strdup("IDE");
+    vboxIIDFromUUID(dom->uuid, iid);
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_SUCCEEDED(rc) && machine) {
+        PRBool accessible = PR_FALSE;
 
+        machine->vtbl->GetAccessible(machine, &accessible);
+        if (accessible) {
+            int i = 0;
+            struct utsname utsname;
+            PRBool PAEEnabled                   = PR_FALSE;
+            PRBool ACPIEnabled                  = PR_FALSE;
+            PRBool IOAPICEnabled                = PR_FALSE;
+            PRBool VRDPEnabled                  = PR_FALSE;
+            PRUint32 CPUCount                   = 0;
+            PRUint32 memorySize                 = 0;
+            PRUint32 netAdpCnt                  = 0;
+            PRUint32 netAdpIncCnt               = 0;
+            PRUint32 maxMemorySize              = 4 * 1024;
+            PRUint32 USBFilterCount             = 0;
+            PRUint32 maxBootPosition            = 0;
+            PRUint32 serialPortCount            = 0;
+            PRUint32 serialPortIncCount         = 0;
+            PRUint32 parallelPortCount          = 0;
+            PRUint32 parallelPortIncCount       = 0;
+            IBIOSSettings *bios                 = NULL;
+#if VBOX_API_VERSION < 3001
+            PRInt32       hddNum                = 0;
+            IDVDDrive    *dvdDrive              = NULL;
+            IHardDisk    *hardDiskPM            = NULL;
+            IHardDisk    *hardDiskPS            = NULL;
+            IHardDisk    *hardDiskSS            = NULL;
+            const char   *hddBus                = "IDE";
+            PRUnichar    *hddBusUtf16           = NULL;
+            IFloppyDrive *floppyDrive           = NULL;
+#else  /* VBOX_API_VERSION >= 3001 */
+            PRUint32      mediumAttachSize      = 0;
+            IMediumAttachment **mediumAttachments = NULL;
+#endif /* VBOX_API_VERSION >= 3001 */
+            IVRDPServer *VRDPServer             = NULL;
+            IAudioAdapter *audioAdapter         = NULL;
+            IUSBController *USBController       = NULL;
+            ISystemProperties *systemProperties = NULL;
 
-                def->virtType = VIR_DOMAIN_VIRT_VBOX;
-                def->id = dom->id;
-                memcpy(def->uuid, dom->uuid, VIR_UUID_BUFLEN);
-                def->name = strdup(dom->name);
 
-                machine->vtbl->GetMemorySize(machine, &memorySize);
-                def->memory = memorySize * 1024;
+            def->virtType = VIR_DOMAIN_VIRT_VBOX;
+            def->id = dom->id;
+            memcpy(def->uuid, dom->uuid, VIR_UUID_BUFLEN);
+            def->name = strdup(dom->name);
 
-                data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-                if (systemProperties) {
-                    systemProperties->vtbl->GetMaxGuestRAM(systemProperties, &maxMemorySize);
-                    systemProperties->vtbl->GetMaxBootPosition(systemProperties, &maxBootPosition);
-                    systemProperties->vtbl->GetNetworkAdapterCount(systemProperties, &netAdpCnt);
-                    systemProperties->vtbl->GetSerialPortCount(systemProperties, &serialPortCount);
-                    systemProperties->vtbl->GetParallelPortCount(systemProperties, &parallelPortCount);
-                    systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-                    systemProperties = NULL;
-                }
-                /* Currently setting memory and maxMemory as same, cause
-                 * the notation here seems to be inconsistent while
-                 * reading and while dumping xml
-                 */
-                /* def->maxmem = maxMemorySize * 1024; */
-                def->maxmem = memorySize * 1024;
+            machine->vtbl->GetMemorySize(machine, &memorySize);
+            def->memory = memorySize * 1024;
 
-                machine->vtbl->GetCPUCount(machine, &CPUCount);
-                def->vcpus = CPUCount;
-
-                /* Skip cpumasklen, cpumask, onReboot, onPoweroff, onCrash */
-
-                def->os.type = strdup("hvm");
-
-                uname(&utsname);
-                def->os.arch = strdup(utsname.machine);
-
-                def->os.nBootDevs = 0;
-                for (i = 0; (i < VIR_DOMAIN_BOOT_LAST) && (i < maxBootPosition); i++) {
-                    PRUint32 device = DeviceType_Null;
-
-                    machine->vtbl->GetBootOrder(machine, i+1, &device);
-
-                    if (device == DeviceType_Floppy) {
-                        def->os.bootDevs[i] = VIR_DOMAIN_BOOT_FLOPPY;
-                        def->os.nBootDevs++;
-                    } else if (device == DeviceType_DVD) {
-                        def->os.bootDevs[i] = VIR_DOMAIN_BOOT_CDROM;
-                        def->os.nBootDevs++;
-                    } else if (device == DeviceType_HardDisk) {
-                        def->os.bootDevs[i] = VIR_DOMAIN_BOOT_DISK;
-                        def->os.nBootDevs++;
-                    } else if (device == DeviceType_Network) {
-                        def->os.bootDevs[i] = VIR_DOMAIN_BOOT_NET;
-                        def->os.nBootDevs++;
-                    } else if (device == DeviceType_USB) {
-                        /* Not supported by libvirt yet */
-                    } else if (device == DeviceType_SharedFolder) {
-                        /* Not supported by libvirt yet */
-                    }
-                }
+            data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+            if (systemProperties) {
+                systemProperties->vtbl->GetMaxGuestRAM(systemProperties, &maxMemorySize);
+                systemProperties->vtbl->GetMaxBootPosition(systemProperties, &maxBootPosition);
+                systemProperties->vtbl->GetNetworkAdapterCount(systemProperties, &netAdpCnt);
+                systemProperties->vtbl->GetSerialPortCount(systemProperties, &serialPortCount);
+                systemProperties->vtbl->GetParallelPortCount(systemProperties, &parallelPortCount);
+                VBOX_RELEASE(systemProperties);
+                systemProperties = NULL;
+            }
+            /* Currently setting memory and maxMemory as same, cause
+             * the notation here seems to be inconsistent while
+             * reading and while dumping xml
+             */
+            /* def->maxmem = maxMemorySize * 1024; */
+            def->maxmem = memorySize * 1024;
+
+            machine->vtbl->GetCPUCount(machine, &CPUCount);
+            def->vcpus = CPUCount;
+
+            /* Skip cpumasklen, cpumask, onReboot, onPoweroff, onCrash */
+
+            def->os.type = strdup("hvm");
+
+            uname(&utsname);
+            def->os.arch = strdup(utsname.machine);
+
+            def->os.nBootDevs = 0;
+            for (i = 0; (i < VIR_DOMAIN_BOOT_LAST) && (i < maxBootPosition); i++) {
+                PRUint32 device = DeviceType_Null;
 
-                def->features = 0;
-                machine->vtbl->GetPAEEnabled(machine, &PAEEnabled);
-                if (PAEEnabled) {
-                    def->features = def->features | (1 << VIR_DOMAIN_FEATURE_PAE);
+                machine->vtbl->GetBootOrder(machine, i+1, &device);
+
+                if (device == DeviceType_Floppy) {
+                    def->os.bootDevs[i] = VIR_DOMAIN_BOOT_FLOPPY;
+                    def->os.nBootDevs++;
+                } else if (device == DeviceType_DVD) {
+                    def->os.bootDevs[i] = VIR_DOMAIN_BOOT_CDROM;
+                    def->os.nBootDevs++;
+                } else if (device == DeviceType_HardDisk) {
+                    def->os.bootDevs[i] = VIR_DOMAIN_BOOT_DISK;
+                    def->os.nBootDevs++;
+                } else if (device == DeviceType_Network) {
+                    def->os.bootDevs[i] = VIR_DOMAIN_BOOT_NET;
+                    def->os.nBootDevs++;
+                } else if (device == DeviceType_USB) {
+                    /* Not supported by libvirt yet */
+                } else if (device == DeviceType_SharedFolder) {
+                    /* Not supported by libvirt yet */
                 }
+            }
 
-                machine->vtbl->GetBIOSSettings(machine, &bios);
-                if (bios) {
-                    bios->vtbl->GetACPIEnabled(bios, &ACPIEnabled);
-                    if (ACPIEnabled) {
-                        def->features = def->features | (1 << VIR_DOMAIN_FEATURE_ACPI);
-                    }
+            def->features = 0;
+#if VBOX_API_VERSION < 3001
+            machine->vtbl->GetPAEEnabled(machine, &PAEEnabled);
+#else  /* VBOX_API_VERSION >= 3001 */
+            machine->vtbl->GetCpuProperty(machine, CpuPropertyType_PAE, &PAEEnabled);
+#endif /* VBOX_API_VERSION >= 3001 */
+            if (PAEEnabled) {
+                def->features = def->features | (1 << VIR_DOMAIN_FEATURE_PAE);
+            }
 
-                    bios->vtbl->GetIOAPICEnabled(bios, &IOAPICEnabled);
-                    if (IOAPICEnabled) {
-                        def->features = def->features | (1 << VIR_DOMAIN_FEATURE_APIC);
-                    }
+            machine->vtbl->GetBIOSSettings(machine, &bios);
+            if (bios) {
+                bios->vtbl->GetACPIEnabled(bios, &ACPIEnabled);
+                if (ACPIEnabled) {
+                    def->features = def->features | (1 << VIR_DOMAIN_FEATURE_ACPI);
+                }
 
-                    bios->vtbl->nsisupports.Release((nsISupports *)bios);
+                bios->vtbl->GetIOAPICEnabled(bios, &IOAPICEnabled);
+                if (IOAPICEnabled) {
+                    def->features = def->features | (1 << VIR_DOMAIN_FEATURE_APIC);
                 }
 
-                /* Currently VirtualBox always uses locatime
-                 * so locatime is always true here */
-                def->localtime = 1;
-
-                /* dump video options vram/2d/3d/directx/etc. */
-                {
-                    /* Currently supports only one graphics card */
-                    def->nvideos = 1;
-                    if (VIR_ALLOC_N(def->videos, def->nvideos) >= 0) {
-                        if (VIR_ALLOC(def->videos[0]) >= 0) {
-                            /* the default is: vram is 8MB, One monitor, 3dAccel Off */
-                            PRUint32 VRAMSize          = 8 * 1024;
-                            PRUint32 monitorCount      = 1;
-                            PRBool accelerate3DEnabled = PR_FALSE;
-
-                            machine->vtbl->GetVRAMSize(machine, &VRAMSize);
-                            machine->vtbl->GetMonitorCount(machine, &monitorCount);
-                            machine->vtbl->GetAccelerate3DEnabled(machine, &accelerate3DEnabled);
-
-                            def->videos[0]->type            = VIR_DOMAIN_VIDEO_TYPE_VBOX;
-                            def->videos[0]->vram            = VRAMSize;
-                            def->videos[0]->heads           = monitorCount;
-                            if (VIR_ALLOC(def->videos[0]->accel) >= 0) {
-                                def->videos[0]->accel->support3d = accelerate3DEnabled;
-                                /* Not supported yet, but should be in 3.1 soon */
-                                def->videos[0]->accel->support2d = 0;
-                            } else
-                                virReportOOMError(dom->conn);
+                VBOX_RELEASE(bios);
+            }
+
+            /* Currently VirtualBox always uses locatime
+             * so locatime is always true here */
+            def->localtime = 1;
+
+            /* dump video options vram/2d/3d/directx/etc. */
+            {
+                /* Currently supports only one graphics card */
+                def->nvideos = 1;
+                if (VIR_ALLOC_N(def->videos, def->nvideos) >= 0) {
+                    if (VIR_ALLOC(def->videos[0]) >= 0) {
+                        /* the default is: vram is 8MB, One monitor, 3dAccel Off */
+                        PRUint32 VRAMSize          = 8 * 1024;
+                        PRUint32 monitorCount      = 1;
+                        PRBool accelerate3DEnabled = PR_FALSE;
+
+                        machine->vtbl->GetVRAMSize(machine, &VRAMSize);
+                        machine->vtbl->GetMonitorCount(machine, &monitorCount);
+                        machine->vtbl->GetAccelerate3DEnabled(machine, &accelerate3DEnabled);
+
+                        def->videos[0]->type            = VIR_DOMAIN_VIDEO_TYPE_VBOX;
+                        def->videos[0]->vram            = VRAMSize;
+                        def->videos[0]->heads           = monitorCount;
+                        if (VIR_ALLOC(def->videos[0]->accel) >= 0) {
+                            def->videos[0]->accel->support3d = accelerate3DEnabled;
+                            /* Not supported yet, but should be in 3.1 soon */
+                            def->videos[0]->accel->support2d = 0;
                         } else
                             virReportOOMError(dom->conn);
                     } else
                         virReportOOMError(dom->conn);
-                }
+                } else
+                    virReportOOMError(dom->conn);
+            }
 
-                /* dump display options vrdp/gui/sdl */
-                {
-                    int vrdpPresent           = 0;
-                    int sdlPresent            = 0;
-                    int guiPresent            = 0;
-                    int totalPresent          = 0;
-                    char *guiDisplay          = NULL;
-                    char *sdlDisplay          = NULL;
-                    PRUnichar *keyTypeUtf16   = NULL;
-                    PRUnichar *valueTypeUtf16 = NULL;
-                    char      *valueTypeUtf8  = NULL;
-
-                    def->ngraphics = 0;
-
-                    data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Type", &keyTypeUtf16);
-                    machine->vtbl->GetExtraData(machine, keyTypeUtf16, &valueTypeUtf16);
-                    data->pFuncs->pfnUtf16Free(keyTypeUtf16);
+            /* dump display options vrdp/gui/sdl */
+            {
+                int vrdpPresent           = 0;
+                int sdlPresent            = 0;
+                int guiPresent            = 0;
+                int totalPresent          = 0;
+                char *guiDisplay          = NULL;
+                char *sdlDisplay          = NULL;
+                PRUnichar *keyTypeUtf16   = NULL;
+                PRUnichar *valueTypeUtf16 = NULL;
+                char      *valueTypeUtf8  = NULL;
 
-                    if (valueTypeUtf16) {
-                        data->pFuncs->pfnUtf16ToUtf8(valueTypeUtf16, &valueTypeUtf8);
-                        data->pFuncs->pfnUtf16Free(valueTypeUtf16);
+                def->ngraphics = 0;
 
-                        if ( STREQ(valueTypeUtf8, "sdl") || STREQ(valueTypeUtf8, "gui") ) {
-                            PRUnichar *keyDislpayUtf16   = NULL;
-                            PRUnichar *valueDisplayUtf16 = NULL;
-                            char      *valueDisplayUtf8  = NULL;
+                VBOX_UTF8_TO_UTF16("FRONTEND/Type", &keyTypeUtf16);
+                machine->vtbl->GetExtraData(machine, keyTypeUtf16, &valueTypeUtf16);
+                VBOX_UTF16_FREE(keyTypeUtf16);
 
-                            data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Display", &keyDislpayUtf16);
-                            machine->vtbl->GetExtraData(machine, keyDislpayUtf16, &valueDisplayUtf16);
-                            data->pFuncs->pfnUtf16Free(keyDislpayUtf16);
+                if (valueTypeUtf16) {
+                    VBOX_UTF16_TO_UTF8(valueTypeUtf16, &valueTypeUtf8);
+                    VBOX_UTF16_FREE(valueTypeUtf16);
 
-                            if (valueDisplayUtf16) {
-                                data->pFuncs->pfnUtf16ToUtf8(valueDisplayUtf16, &valueDisplayUtf8);
-                                data->pFuncs->pfnUtf16Free(valueDisplayUtf16);
+                    if ( STREQ(valueTypeUtf8, "sdl") || STREQ(valueTypeUtf8, "gui") ) {
+                        PRUnichar *keyDislpayUtf16   = NULL;
+                        PRUnichar *valueDisplayUtf16 = NULL;
+                        char      *valueDisplayUtf8  = NULL;
 
-                                if (strlen(valueDisplayUtf8) <= 0) {
-                                    data->pFuncs->pfnUtf8Free(valueDisplayUtf8);
-                                    valueDisplayUtf8 = NULL;
-                                }
-                            }
+                        VBOX_UTF8_TO_UTF16("FRONTEND/Display", &keyDislpayUtf16);
+                        machine->vtbl->GetExtraData(machine, keyDislpayUtf16, &valueDisplayUtf16);
+                        VBOX_UTF16_FREE(keyDislpayUtf16);
 
-                            if (STREQ(valueTypeUtf8, "sdl")) {
-                                sdlPresent = 1;
-                                if (valueDisplayUtf8)
-                                    sdlDisplay = strdup(valueDisplayUtf8);
-                                if (sdlDisplay == NULL) {
-                                    virReportOOMError(dom->conn);
-                                    /* just don't go to cleanup yet as it is ok to have
-                                     * sdlDisplay as NULL and we check it below if it
-                                     * exist and then only use it there
-                                     */
-                                }
-                                totalPresent++;
-                            }
+                        if (valueDisplayUtf16) {
+                            VBOX_UTF16_TO_UTF8(valueDisplayUtf16, &valueDisplayUtf8);
+                            VBOX_UTF16_FREE(valueDisplayUtf16);
 
-                            if (STREQ(valueTypeUtf8, "gui")) {
-                                guiPresent = 1;
-                                if (valueDisplayUtf8)
-                                    guiDisplay = strdup(valueDisplayUtf8);
-                                if (guiDisplay == NULL) {
-                                    virReportOOMError(dom->conn);
-                                    /* just don't go to cleanup yet as it is ok to have
-                                     * guiDisplay as NULL and we check it below if it
-                                     * exist and then only use it there
-                                     */
-                                }
-                                totalPresent++;
+                            if (strlen(valueDisplayUtf8) <= 0) {
+                                VBOX_UTF8_FREE(valueDisplayUtf8);
+                                valueDisplayUtf8 = NULL;
                             }
-                            if (valueDisplayUtf8)
-                                data->pFuncs->pfnUtf8Free(valueDisplayUtf8);
                         }
 
-                        if (STREQ(valueTypeUtf8, "vrdp"))
-                            vrdpPresent = 1;
-
-                        data->pFuncs->pfnUtf8Free(valueTypeUtf8);
-                    }
-
-                    if ((totalPresent > 0) && (VIR_ALLOC_N(def->graphics, totalPresent) >= 0)) {
-                        if ((guiPresent) && (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0)) {
-                            def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP;
-                            if (guiDisplay)
-                                def->graphics[def->ngraphics]->data.desktop.display = guiDisplay;
-                            def->ngraphics++;
+                        if (STREQ(valueTypeUtf8, "sdl")) {
+                            sdlPresent = 1;
+                            if (valueDisplayUtf8)
+                                sdlDisplay = strdup(valueDisplayUtf8);
+                            if (sdlDisplay == NULL) {
+                                virReportOOMError(dom->conn);
+                                /* just don't go to cleanup yet as it is ok to have
+                                 * sdlDisplay as NULL and we check it below if it
+                                 * exist and then only use it there
+                                 */
+                            }
+                            totalPresent++;
                         }
 
-                        if ((sdlPresent) && (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0)) {
-                            def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
-                            if (sdlDisplay)
-                                def->graphics[def->ngraphics]->data.sdl.display = sdlDisplay;
-                            def->ngraphics++;
-                        }
-                    } else if ((vrdpPresent != 1) && (totalPresent == 0) && (VIR_ALLOC_N(def->graphics, 1) >= 0)) {
-                        if (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0) {
-                            def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP;
-                            def->graphics[def->ngraphics]->data.desktop.display = strdup(getenv("DISPLAY"));
-                            if (def->graphics[def->ngraphics]->data.desktop.display == NULL) {
+                        if (STREQ(valueTypeUtf8, "gui")) {
+                            guiPresent = 1;
+                            if (valueDisplayUtf8)
+                                guiDisplay = strdup(valueDisplayUtf8);
+                            if (guiDisplay == NULL) {
                                 virReportOOMError(dom->conn);
                                 /* just don't go to cleanup yet as it is ok to have
-                                 * display as NULL
+                                 * guiDisplay as NULL and we check it below if it
+                                 * exist and then only use it there
                                  */
                             }
                             totalPresent++;
-                            def->ngraphics++;
                         }
+                        if (valueDisplayUtf8)
+                            VBOX_UTF8_FREE(valueDisplayUtf8);
                     }
 
-                    machine->vtbl->GetVRDPServer(machine, &VRDPServer);
-                    if (VRDPServer) {
-                        VRDPServer->vtbl->GetEnabled(VRDPServer, &VRDPEnabled);
-                        if (VRDPEnabled) {
-
-                            totalPresent++;
+                    if (STREQ(valueTypeUtf8, "vrdp"))
+                        vrdpPresent = 1;
 
-                            if ((VIR_REALLOC_N(def->graphics, totalPresent) >= 0) &&
-                                (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0)) {
-                                PRUint32 VRDPport            = 0;
-                                PRUnichar *netAddressUtf16   = NULL;
-                                char      *netAddressUtf8    = NULL;
-                                PRBool allowMultiConnection  = PR_FALSE;
-                                PRBool reuseSingleConnection = PR_FALSE;
+                    VBOX_UTF8_FREE(valueTypeUtf8);
+                }
 
-                                def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_RDP;
+                if ((totalPresent > 0) && (VIR_ALLOC_N(def->graphics, totalPresent) >= 0)) {
+                    if ((guiPresent) && (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0)) {
+                        def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP;
+                        if (guiDisplay)
+                            def->graphics[def->ngraphics]->data.desktop.display = guiDisplay;
+                        def->ngraphics++;
+                    }
 
-                                VRDPServer->vtbl->GetPort(VRDPServer, &VRDPport);
-                                if (VRDPport) {
-                                    def->graphics[def->ngraphics]->data.rdp.port = VRDPport;
-                                } else {
-                                    def->graphics[def->ngraphics]->data.rdp.autoport = 1;
-                                }
+                    if ((sdlPresent) && (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0)) {
+                        def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
+                        if (sdlDisplay)
+                            def->graphics[def->ngraphics]->data.sdl.display = sdlDisplay;
+                        def->ngraphics++;
+                    }
+                } else if ((vrdpPresent != 1) && (totalPresent == 0) && (VIR_ALLOC_N(def->graphics, 1) >= 0)) {
+                    if (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0) {
+                        def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP;
+                        def->graphics[def->ngraphics]->data.desktop.display = strdup(getenv("DISPLAY"));
+                        if (def->graphics[def->ngraphics]->data.desktop.display == NULL) {
+                            virReportOOMError(dom->conn);
+                            /* just don't go to cleanup yet as it is ok to have
+                             * display as NULL
+                             */
+                        }
+                        totalPresent++;
+                        def->ngraphics++;
+                    }
+                }
 
-                                VRDPServer->vtbl->GetNetAddress(VRDPServer, &netAddressUtf16);
-                                if (netAddressUtf16) {
-                                    data->pFuncs->pfnUtf16ToUtf8(netAddressUtf16, &netAddressUtf8);
-                                    if (STRNEQ(netAddressUtf8, ""))
-                                            def->graphics[def->ngraphics]->data.rdp.listenAddr = strdup(netAddressUtf8);
-                                    data->pFuncs->pfnUtf16Free(netAddressUtf16);
-                                    data->pFuncs->pfnUtf8Free(netAddressUtf8);
-                                }
+                machine->vtbl->GetVRDPServer(machine, &VRDPServer);
+                if (VRDPServer) {
+                    VRDPServer->vtbl->GetEnabled(VRDPServer, &VRDPEnabled);
+                    if (VRDPEnabled) {
+
+                        totalPresent++;
+
+                        if ((VIR_REALLOC_N(def->graphics, totalPresent) >= 0) &&
+                            (VIR_ALLOC(def->graphics[def->ngraphics]) >= 0)) {
+                            PRUnichar *netAddressUtf16   = NULL;
+                            char      *netAddressUtf8    = NULL;
+                            PRBool allowMultiConnection  = PR_FALSE;
+                            PRBool reuseSingleConnection = PR_FALSE;
+#if VBOX_API_VERSION < 3001
+                            PRUint32 VRDPport = 0;
+                            VRDPServer->vtbl->GetPort(VRDPServer, &VRDPport);
+                            if (VRDPport) {
+                                def->graphics[def->ngraphics]->data.rdp.port = VRDPport;
+#else  /* VBOX_API_VERSION >= 3001 */
+                            PRUnichar *VRDPport = NULL;
+                            VRDPServer->vtbl->GetPorts(VRDPServer, &VRDPport);
+                            if (VRDPport) {
+                                /* even if vbox supports mutilpe ports, single port for now here */
+                                def->graphics[def->ngraphics]->data.rdp.port = PRUnicharToInt(VRDPport);
+                                VBOX_UTF16_FREE(VRDPport);
+#endif /* VBOX_API_VERSION >= 3001 */
+                            } else {
+                                def->graphics[def->ngraphics]->data.rdp.autoport = 1;
+                            }
 
-                                VRDPServer->vtbl->GetAllowMultiConnection(VRDPServer, &allowMultiConnection);
-                                if (allowMultiConnection) {
-                                    def->graphics[def->ngraphics]->data.rdp.multiUser = 1;
-                                }
+                            def->graphics[def->ngraphics]->type = VIR_DOMAIN_GRAPHICS_TYPE_RDP;
 
-                                VRDPServer->vtbl->GetReuseSingleConnection(VRDPServer, &reuseSingleConnection);
-                                if (reuseSingleConnection) {
-                                    def->graphics[def->ngraphics]->data.rdp.replaceUser = 1;
-                                }
+                            VRDPServer->vtbl->GetNetAddress(VRDPServer, &netAddressUtf16);
+                            if (netAddressUtf16) {
+                                VBOX_UTF16_TO_UTF8(netAddressUtf16, &netAddressUtf8);
+                                if (STRNEQ(netAddressUtf8, ""))
+                                        def->graphics[def->ngraphics]->data.rdp.listenAddr = strdup(netAddressUtf8);
+                                VBOX_UTF16_FREE(netAddressUtf16);
+                                VBOX_UTF8_FREE(netAddressUtf8);
+                            }
 
-                                def->ngraphics++;
-                            } else
-                                virReportOOMError(dom->conn);
-                        }
-                        VRDPServer->vtbl->nsisupports.Release((nsISupports *)VRDPServer);
-                    }
-                }
+                            VRDPServer->vtbl->GetAllowMultiConnection(VRDPServer, &allowMultiConnection);
+                            if (allowMultiConnection) {
+                                def->graphics[def->ngraphics]->data.rdp.multiUser = 1;
+                            }
 
-                /* dump IDE hdds if present */
-                data->pFuncs->pfnUtf8ToUtf16(hddBus, &hddBusUtf16);
-                VIR_FREE(hddBus);
+                            VRDPServer->vtbl->GetReuseSingleConnection(VRDPServer, &reuseSingleConnection);
+                            if (reuseSingleConnection) {
+                                def->graphics[def->ngraphics]->data.rdp.replaceUser = 1;
+                            }
 
-                def->ndisks = 0;
-                machine->vtbl->GetHardDisk(machine, hddBusUtf16, 0, 0,  &hardDiskPM);
-                if (hardDiskPM)
-                    def->ndisks++;
-
-                machine->vtbl->GetHardDisk(machine, hddBusUtf16, 0, 1,  &hardDiskPS);
-                if (hardDiskPS)
-                    def->ndisks++;
-
-                machine->vtbl->GetHardDisk(machine, hddBusUtf16, 1, 1,  &hardDiskSS);
-                if (hardDiskSS)
-                    def->ndisks++;
-
-                data->pFuncs->pfnUtf16Free(hddBusUtf16);
-
-                if ((def->ndisks > 0) && (VIR_ALLOC_N(def->disks, def->ndisks) >= 0)) {
-                    for (i = 0; i < def->ndisks; i++) {
-                        if (VIR_ALLOC(def->disks[i]) >= 0) {
-                            def->disks[i]->device = VIR_DOMAIN_DISK_DEVICE_DISK;
-                            def->disks[i]->bus = VIR_DOMAIN_DISK_BUS_IDE;
-                            def->disks[i]->type = VIR_DOMAIN_DISK_TYPE_FILE;
+                            def->ngraphics++;
                         } else
                             virReportOOMError(dom->conn);
                     }
+                    VBOX_RELEASE(VRDPServer);
                 }
+            }
+
+#if VBOX_API_VERSION < 3001
+            /* dump IDE hdds if present */
+            VBOX_UTF8_TO_UTF16(hddBus, &hddBusUtf16);
 
-                if (hardDiskPM) {
-                    PRUnichar *hddlocationUtf16 = NULL;
-                    char *hddlocation           = NULL;
-                    PRUint32 hddType            = HardDiskType_Normal;
+            def->ndisks = 0;
+            machine->vtbl->GetHardDisk(machine, hddBusUtf16, 0, 0,  &hardDiskPM);
+            if (hardDiskPM)
+                def->ndisks++;
 
-                    hardDiskPM->vtbl->imedium.GetLocation((IMedium *)hardDiskPM, &hddlocationUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(hddlocationUtf16, &hddlocation);
+            machine->vtbl->GetHardDisk(machine, hddBusUtf16, 0, 1,  &hardDiskPS);
+            if (hardDiskPS)
+                def->ndisks++;
 
-                    hardDiskPM->vtbl->GetType(hardDiskPM, &hddType);
+            machine->vtbl->GetHardDisk(machine, hddBusUtf16, 1, 1,  &hardDiskSS);
+            if (hardDiskSS)
+                def->ndisks++;
 
-                    if (hddType == HardDiskType_Immutable)
-                        def->disks[hddNum]->readonly = 1;
-                    def->disks[hddNum]->src = strdup(hddlocation);
-                    def->disks[hddNum]->dst = strdup("hda");
-                    hddNum++;
+            VBOX_UTF16_FREE(hddBusUtf16);
 
-                    data->pFuncs->pfnUtf8Free(hddlocation);
-                    data->pFuncs->pfnUtf16Free(hddlocationUtf16);
-                    hardDiskPM->vtbl->imedium.nsisupports.Release((nsISupports *)hardDiskPM);
+            if ((def->ndisks > 0) && (VIR_ALLOC_N(def->disks, def->ndisks) >= 0)) {
+                for (i = 0; i < def->ndisks; i++) {
+                    if (VIR_ALLOC(def->disks[i]) >= 0) {
+                        def->disks[i]->device = VIR_DOMAIN_DISK_DEVICE_DISK;
+                        def->disks[i]->bus = VIR_DOMAIN_DISK_BUS_IDE;
+                        def->disks[i]->type = VIR_DOMAIN_DISK_TYPE_FILE;
+                    } else
+                        virReportOOMError(dom->conn);
                 }
+            }
 
-                if (hardDiskPS) {
-                    PRUnichar *hddlocationUtf16 = NULL;
-                    char *hddlocation           = NULL;
-                    PRUint32 hddType            = HardDiskType_Normal;
+            if (hardDiskPM) {
+                PRUnichar *hddlocationUtf16 = NULL;
+                char *hddlocation           = NULL;
+                PRUint32 hddType            = HardDiskType_Normal;
 
-                    hardDiskPS->vtbl->imedium.GetLocation((IMedium *)hardDiskPS, &hddlocationUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(hddlocationUtf16, &hddlocation);
+                hardDiskPM->vtbl->imedium.GetLocation((IMedium *)hardDiskPM, &hddlocationUtf16);
+                VBOX_UTF16_TO_UTF8(hddlocationUtf16, &hddlocation);
 
-                    hardDiskPS->vtbl->GetType(hardDiskPS, &hddType);
+                hardDiskPM->vtbl->GetType(hardDiskPM, &hddType);
 
-                    if (hddType == HardDiskType_Immutable)
-                        def->disks[hddNum]->readonly = 1;
-                    def->disks[hddNum]->src = strdup(hddlocation);
-                    def->disks[hddNum]->dst = strdup("hdb");
-                    hddNum++;
+                if (hddType == HardDiskType_Immutable)
+                    def->disks[hddNum]->readonly = 1;
+                def->disks[hddNum]->src = strdup(hddlocation);
+                def->disks[hddNum]->dst = strdup("hda");
+                hddNum++;
 
-                    data->pFuncs->pfnUtf8Free(hddlocation);
-                    data->pFuncs->pfnUtf16Free(hddlocationUtf16);
-                    hardDiskPS->vtbl->imedium.nsisupports.Release((nsISupports *)hardDiskPS);
-                }
+                VBOX_UTF8_FREE(hddlocation);
+                VBOX_UTF16_FREE(hddlocationUtf16);
+                VBOX_MEDIUM_RELEASE(hardDiskPM);
+            }
 
-                if (hardDiskSS) {
-                    PRUnichar *hddlocationUtf16 = NULL;
-                    char *hddlocation           = NULL;
-                    PRUint32 hddType            = HardDiskType_Normal;
+            if (hardDiskPS) {
+                PRUnichar *hddlocationUtf16 = NULL;
+                char *hddlocation           = NULL;
+                PRUint32 hddType            = HardDiskType_Normal;
 
-                    hardDiskSS->vtbl->imedium.GetLocation((IMedium *)hardDiskSS, &hddlocationUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(hddlocationUtf16, &hddlocation);
+                hardDiskPS->vtbl->imedium.GetLocation((IMedium *)hardDiskPS, &hddlocationUtf16);
+                VBOX_UTF16_TO_UTF8(hddlocationUtf16, &hddlocation);
 
-                    hardDiskSS->vtbl->GetType(hardDiskSS, &hddType);
+                hardDiskPS->vtbl->GetType(hardDiskPS, &hddType);
 
-                    if (hddType == HardDiskType_Immutable)
-                        def->disks[hddNum]->readonly = 1;
-                    def->disks[hddNum]->src = strdup(hddlocation);
-                    def->disks[hddNum]->dst = strdup("hdd");
-                    hddNum++;
+                if (hddType == HardDiskType_Immutable)
+                    def->disks[hddNum]->readonly = 1;
+                def->disks[hddNum]->src = strdup(hddlocation);
+                def->disks[hddNum]->dst = strdup("hdb");
+                hddNum++;
 
-                    data->pFuncs->pfnUtf8Free(hddlocation);
-                    data->pFuncs->pfnUtf16Free(hddlocationUtf16);
-                    hardDiskSS->vtbl->imedium.nsisupports.Release((nsISupports *)hardDiskSS);
-                }
+                VBOX_UTF8_FREE(hddlocation);
+                VBOX_UTF16_FREE(hddlocationUtf16);
+                VBOX_MEDIUM_RELEASE(hardDiskPS);
+            }
 
-                /* dump network cards if present */
-                def->nnets = 0;
-                /* Get which network cards are enabled */
-                for (i = 0; i < netAdpCnt; i++) {
-                    INetworkAdapter *adapter = NULL;
+            if (hardDiskSS) {
+                PRUnichar *hddlocationUtf16 = NULL;
+                char *hddlocation           = NULL;
+                PRUint32 hddType            = HardDiskType_Normal;
 
-                    machine->vtbl->GetNetworkAdapter(machine, i, &adapter);
-                    if (adapter) {
-                        PRBool enabled = PR_FALSE;
+                hardDiskSS->vtbl->imedium.GetLocation((IMedium *)hardDiskSS, &hddlocationUtf16);
+                VBOX_UTF16_TO_UTF8(hddlocationUtf16, &hddlocation);
 
-                        adapter->vtbl->GetEnabled(adapter, &enabled);
-                        if (enabled) {
-                            def->nnets++;
-                        }
+                hardDiskSS->vtbl->GetType(hardDiskSS, &hddType);
 
-                        adapter->vtbl->nsisupports.Release((nsISupports *)adapter);
+                if (hddType == HardDiskType_Immutable)
+                    def->disks[hddNum]->readonly = 1;
+                def->disks[hddNum]->src = strdup(hddlocation);
+                def->disks[hddNum]->dst = strdup("hdd");
+                hddNum++;
+
+                VBOX_UTF8_FREE(hddlocation);
+                VBOX_UTF16_FREE(hddlocationUtf16);
+                VBOX_MEDIUM_RELEASE(hardDiskSS);
+            }
+#else  /* VBOX_API_VERSION >= 3001 */
+            /* dump IDE hdds if present */
+
+            bool error = false;
+            int diskCount = 0;
+            PRUint32   maxPortPerInst[StorageBus_Floppy + 1] = {};
+            PRUint32   maxSlotPerPort[StorageBus_Floppy + 1] = {};
+            def->ndisks = 0;
+            machine->vtbl->GetMediumAttachments(machine, &mediumAttachSize, &mediumAttachments);
+
+            /* get the number of attachments */
+            for (i = 0; i < mediumAttachSize; i++) {
+                IMediumAttachment *imediumattach = mediumAttachments[i];
+                if (imediumattach) {
+                    IMedium *medium = NULL;
+
+                    imediumattach->vtbl->GetMedium(imediumattach, &medium);
+                    if (medium) {
+                        def->ndisks++;
+                        VBOX_RELEASE(medium);
                     }
                 }
+            }
 
-                /* Allocate memory for the networkcards which are enabled */
-                if ((def->nnets > 0) && (VIR_ALLOC_N(def->nets, def->nnets) >= 0)) {
-                    for (i = 0; i < def->nnets; i++) {
-                        if (VIR_ALLOC(def->nets[i]) >= 0) {
-                        } else
-                            virReportOOMError(dom->conn);
+            /* Allocate mem, if fails return error */
+            if (VIR_ALLOC_N(def->disks, def->ndisks) >= 0) {
+                for (i = 0; i < def->ndisks; i++) {
+                    if (VIR_ALLOC(def->disks[i]) < 0) {
+                        virReportOOMError(dom->conn);
+                        error = true;
+                        break;
                     }
                 }
+            } else {
+                virReportOOMError(dom->conn);
+                error = true;
+            }
+
+            if (!error)
+                error = !vboxGetMaxPortSlotValues(data->vboxObj, maxPortPerInst, maxSlotPerPort);
+
+            /* get the attachment details here */
+            for (i = 0; i < mediumAttachSize && diskCount < def->ndisks && !error; i++) {
+                IMediumAttachment *imediumattach = mediumAttachments[i];
+                IStorageController *storageController = NULL;
+                PRUnichar *storageControllerName = NULL;
+                PRUint32   deviceType     = DeviceType_Null;
+                PRUint32   storageBus     = StorageBus_Null;
+                PRBool     readOnly       = PR_FALSE;
+                IMedium   *medium         = NULL;
+                PRUnichar *mediumLocUtf16 = NULL;
+                char      *mediumLocUtf8  = NULL;
+                PRUint32   deviceInst     = 0;
+                PRInt32    devicePort     = 0;
+                PRInt32    deviceSlot     = 0;
+
+                if (!imediumattach)
+                    continue;
+
+                imediumattach->vtbl->GetMedium(imediumattach, &medium);
+                if (!medium)
+                    continue;
+
+                imediumattach->vtbl->GetController(imediumattach, &storageControllerName);
+                if (!storageControllerName) {
+                    VBOX_RELEASE(medium);
+                    continue;
+                }
+
+                machine->vtbl->GetStorageControllerByName(machine,
+                                                          storageControllerName,
+                                                          &storageController);
+                VBOX_UTF16_FREE(storageControllerName);
+                if (!storageController) {
+                    VBOX_RELEASE(medium);
+                    continue;
+                }
+
+                medium->vtbl->GetLocation(medium, &mediumLocUtf16);
+                VBOX_UTF16_TO_UTF8(mediumLocUtf16, &mediumLocUtf8);
+                VBOX_UTF16_FREE(mediumLocUtf16);
+                def->disks[diskCount]->src = strdup(mediumLocUtf8);
+                VBOX_UTF8_FREE(mediumLocUtf8);
+
+                if (!(def->disks[diskCount]->src)) {
+                    VBOX_RELEASE(medium);
+                    VBOX_RELEASE(storageController);
+                    virReportOOMError(dom->conn);
+                    error = true;
+                    break;
+                }
 
-                /* Now get the details about the network cards here */
-                for (i = 0;(netAdpIncCnt < def->nnets) && (i < netAdpCnt); i++) {
-                    INetworkAdapter *adapter = NULL;
+                storageController->vtbl->GetBus(storageController, &storageBus);
+                if (storageBus == StorageBus_IDE) {
+                    def->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_IDE;
+                } else if (storageBus == StorageBus_SATA) {
+                    def->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_SATA;
+                } else if (storageBus == StorageBus_SCSI) {
+                    def->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_SCSI;
+                } else if (storageBus == StorageBus_Floppy) {
+                    def->disks[diskCount]->bus = VIR_DOMAIN_DISK_BUS_FDC;
+                }
 
-                    machine->vtbl->GetNetworkAdapter(machine, i, &adapter);
-                    if (adapter) {
-                        PRBool enabled = PR_FALSE;
+                imediumattach->vtbl->GetType(imediumattach, &deviceType);
+                if (deviceType == DeviceType_HardDisk)
+                    def->disks[diskCount]->device = VIR_DOMAIN_DISK_DEVICE_DISK;
+                else if (deviceType == DeviceType_Floppy)
+                    def->disks[diskCount]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
+                else if (deviceType == DeviceType_DVD)
+                    def->disks[diskCount]->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
+
+                imediumattach->vtbl->GetPort(imediumattach, &devicePort);
+                imediumattach->vtbl->GetDevice(imediumattach, &deviceSlot);
+                def->disks[diskCount]->dst = vboxGenerateMediumName(dom->conn,
+                                                                    storageBus,
+                                                                    deviceInst,
+                                                                    devicePort,
+                                                                    deviceSlot,
+                                                                    maxPortPerInst,
+                                                                    maxSlotPerPort);
+
+                medium->vtbl->GetReadOnly(medium, &readOnly);
+                if (readOnly == PR_TRUE)
+                    def->disks[diskCount]->readonly = 1;
+
+                def->disks[diskCount]->type = VIR_DOMAIN_DISK_TYPE_FILE;
+
+                VBOX_RELEASE(medium);
+                VBOX_RELEASE(storageController);
+                diskCount++;
+            }
 
-                        adapter->vtbl->GetEnabled(adapter, &enabled);
-                        if (enabled) {
-                            PRUint32 attachmentType    = NetworkAttachmentType_Null;
-                            PRUint32 adapterType       = NetworkAdapterType_Null;
-                            PRUnichar *MACAddressUtf16 = NULL;
-                            char *MACAddress           = NULL;
-                            char macaddr[VIR_MAC_STRING_BUFLEN] = {0};
+            /* free the memory */
+            for (i = 0; i < mediumAttachSize; i++)
+                VBOX_RELEASE(mediumAttachments[i]);
 
-                            adapter->vtbl->GetAttachmentType(adapter, &attachmentType);
-                            if (attachmentType == NetworkAttachmentType_NAT) {
+            /* cleanup on error */
+            if (error) {
+                for (i = 0; i < def->ndisks; i++) {
+                    VIR_FREE(def->disks[i]);
+                }
+                VIR_FREE(def->disks);
+                def->ndisks = 0;
+            }
 
-                                def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_USER;
+#endif /* VBOX_API_VERSION >= 3001 */
 
-                            } else if (attachmentType == NetworkAttachmentType_Bridged) {
-                                PRUnichar *hostIntUtf16 = NULL;
-                                char *hostInt           = NULL;
+            /* dump network cards if present */
+            def->nnets = 0;
+            /* Get which network cards are enabled */
+            for (i = 0; i < netAdpCnt; i++) {
+                INetworkAdapter *adapter = NULL;
 
-                                def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
+                machine->vtbl->GetNetworkAdapter(machine, i, &adapter);
+                if (adapter) {
+                    PRBool enabled = PR_FALSE;
 
-                                adapter->vtbl->GetHostInterface(adapter, &hostIntUtf16);
+                    adapter->vtbl->GetEnabled(adapter, &enabled);
+                    if (enabled) {
+                        def->nnets++;
+                    }
 
-                                data->pFuncs->pfnUtf16ToUtf8(hostIntUtf16, &hostInt);
-                                def->nets[netAdpIncCnt]->data.bridge.brname = strdup(hostInt);
+                    VBOX_RELEASE(adapter);
+                }
+            }
 
-                                data->pFuncs->pfnUtf8Free(hostInt);
-                                data->pFuncs->pfnUtf16Free(hostIntUtf16);
+            /* Allocate memory for the networkcards which are enabled */
+            if ((def->nnets > 0) && (VIR_ALLOC_N(def->nets, def->nnets) >= 0)) {
+                for (i = 0; i < def->nnets; i++) {
+                    if (VIR_ALLOC(def->nets[i]) >= 0) {
+                    } else
+                        virReportOOMError(dom->conn);
+                }
+            }
 
-                            } else if (attachmentType == NetworkAttachmentType_Internal) {
-                                PRUnichar *intNetUtf16 = NULL;
-                                char *intNet           = NULL;
+            /* Now get the details about the network cards here */
+            for (i = 0;(netAdpIncCnt < def->nnets) && (i < netAdpCnt); i++) {
+                INetworkAdapter *adapter = NULL;
+
+                machine->vtbl->GetNetworkAdapter(machine, i, &adapter);
+                if (adapter) {
+                    PRBool enabled = PR_FALSE;
 
-                                def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_INTERNAL;
+                    adapter->vtbl->GetEnabled(adapter, &enabled);
+                    if (enabled) {
+                        PRUint32 attachmentType    = NetworkAttachmentType_Null;
+                        PRUint32 adapterType       = NetworkAdapterType_Null;
+                        PRUnichar *MACAddressUtf16 = NULL;
+                        char *MACAddress           = NULL;
+                        char macaddr[VIR_MAC_STRING_BUFLEN] = {0};
 
-                                adapter->vtbl->GetInternalNetwork(adapter, &intNetUtf16);
+                        adapter->vtbl->GetAttachmentType(adapter, &attachmentType);
+                        if (attachmentType == NetworkAttachmentType_NAT) {
 
-                                data->pFuncs->pfnUtf16ToUtf8(intNetUtf16, &intNet);
-                                def->nets[netAdpIncCnt]->data.internal.name = strdup(intNet);
+                            def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_USER;
 
-                                data->pFuncs->pfnUtf8Free(intNet);
-                                data->pFuncs->pfnUtf16Free(intNetUtf16);
+                        } else if (attachmentType == NetworkAttachmentType_Bridged) {
+                            PRUnichar *hostIntUtf16 = NULL;
+                            char *hostInt           = NULL;
 
-                            } else if (attachmentType == NetworkAttachmentType_HostOnly) {
-                                PRUnichar *hostIntUtf16 = NULL;
-                                char *hostInt           = NULL;
+                            def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
 
-                                def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_NETWORK;
+                            adapter->vtbl->GetHostInterface(adapter, &hostIntUtf16);
 
-                                adapter->vtbl->GetHostInterface(adapter, &hostIntUtf16);
+                            VBOX_UTF16_TO_UTF8(hostIntUtf16, &hostInt);
+                            def->nets[netAdpIncCnt]->data.bridge.brname = strdup(hostInt);
 
-                                data->pFuncs->pfnUtf16ToUtf8(hostIntUtf16, &hostInt);
-                                def->nets[netAdpIncCnt]->data.network.name = strdup(hostInt);
+                            VBOX_UTF8_FREE(hostInt);
+                            VBOX_UTF16_FREE(hostIntUtf16);
 
-                                data->pFuncs->pfnUtf8Free(hostInt);
-                                data->pFuncs->pfnUtf16Free(hostIntUtf16);
+                        } else if (attachmentType == NetworkAttachmentType_Internal) {
+                            PRUnichar *intNetUtf16 = NULL;
+                            char *intNet           = NULL;
 
-                            } else {
-                                /* default to user type i.e. NAT in VirtualBox if this
-                                 * dump is ever used to create a machine.
-                                 */
-                                def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_USER;
-                            }
+                            def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_INTERNAL;
 
-                            adapter->vtbl->GetAdapterType(adapter, &adapterType);
-                            if (adapterType == NetworkAdapterType_Am79C970A) {
-                                def->nets[netAdpIncCnt]->model = strdup("Am79C970A");
-                            } else if (adapterType == NetworkAdapterType_Am79C973) {
-                                def->nets[netAdpIncCnt]->model = strdup("Am79C973");
-                            } else if (adapterType == NetworkAdapterType_I82540EM) {
-                                def->nets[netAdpIncCnt]->model = strdup("82540EM");
-                            } else if (adapterType == NetworkAdapterType_I82545EM) {
-                                def->nets[netAdpIncCnt]->model = strdup("82545EM");
-                            } else if (adapterType == NetworkAdapterType_I82543GC) {
-                                def->nets[netAdpIncCnt]->model = strdup("82543GC");
-                            }
+                            adapter->vtbl->GetInternalNetwork(adapter, &intNetUtf16);
 
-                            adapter->vtbl->GetMACAddress(adapter, &MACAddressUtf16);
-                            data->pFuncs->pfnUtf16ToUtf8(MACAddressUtf16, &MACAddress);
-                            snprintf(macaddr, VIR_MAC_STRING_BUFLEN,
-                                     "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
-                                     MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3],
-                                     MACAddress[4], MACAddress[5], MACAddress[6], MACAddress[7],
-                                     MACAddress[8], MACAddress[9], MACAddress[10], MACAddress[11]);
+                            VBOX_UTF16_TO_UTF8(intNetUtf16, &intNet);
+                            def->nets[netAdpIncCnt]->data.internal.name = strdup(intNet);
 
-                            /* XXX some real error handling here some day ... */
-                            if (virParseMacAddr(macaddr, def->nets[netAdpIncCnt]->mac) < 0)
-                            {}
+                            VBOX_UTF8_FREE(intNet);
+                            VBOX_UTF16_FREE(intNetUtf16);
 
-                            netAdpIncCnt++;
+                        } else if (attachmentType == NetworkAttachmentType_HostOnly) {
+                            PRUnichar *hostIntUtf16 = NULL;
+                            char *hostInt           = NULL;
 
-                            data->pFuncs->pfnUtf16Free(MACAddressUtf16);
-                            data->pFuncs->pfnUtf8Free(MACAddress);
+                            def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_NETWORK;
+
+                            adapter->vtbl->GetHostInterface(adapter, &hostIntUtf16);
+
+                            VBOX_UTF16_TO_UTF8(hostIntUtf16, &hostInt);
+                            def->nets[netAdpIncCnt]->data.network.name = strdup(hostInt);
+
+                            VBOX_UTF8_FREE(hostInt);
+                            VBOX_UTF16_FREE(hostIntUtf16);
+
+                        } else {
+                            /* default to user type i.e. NAT in VirtualBox if this
+                             * dump is ever used to create a machine.
+                             */
+                            def->nets[netAdpIncCnt]->type = VIR_DOMAIN_NET_TYPE_USER;
+                        }
+
+                        adapter->vtbl->GetAdapterType(adapter, &adapterType);
+                        if (adapterType == NetworkAdapterType_Am79C970A) {
+                            def->nets[netAdpIncCnt]->model = strdup("Am79C970A");
+                        } else if (adapterType == NetworkAdapterType_Am79C973) {
+                            def->nets[netAdpIncCnt]->model = strdup("Am79C973");
+                        } else if (adapterType == NetworkAdapterType_I82540EM) {
+                            def->nets[netAdpIncCnt]->model = strdup("82540EM");
+                        } else if (adapterType == NetworkAdapterType_I82545EM) {
+                            def->nets[netAdpIncCnt]->model = strdup("82545EM");
+                        } else if (adapterType == NetworkAdapterType_I82543GC) {
+                            def->nets[netAdpIncCnt]->model = strdup("82543GC");
+#if VBOX_API_VERSION >= 3001
+                        } else if (adapterType == NetworkAdapterType_Virtio) {
+                            def->nets[netAdpIncCnt]->model = strdup("virtio");
+#endif /* VBOX_API_VERSION >= 3001 */
                         }
 
-                        adapter->vtbl->nsisupports.Release((nsISupports *)adapter);
+                        adapter->vtbl->GetMACAddress(adapter, &MACAddressUtf16);
+                        VBOX_UTF16_TO_UTF8(MACAddressUtf16, &MACAddress);
+                        snprintf(macaddr, VIR_MAC_STRING_BUFLEN,
+                                 "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
+                                 MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3],
+                                 MACAddress[4], MACAddress[5], MACAddress[6], MACAddress[7],
+                                 MACAddress[8], MACAddress[9], MACAddress[10], MACAddress[11]);
+
+                        /* XXX some real error handling here some day ... */
+                        if (virParseMacAddr(macaddr, def->nets[netAdpIncCnt]->mac) < 0)
+                        {}
+
+                        netAdpIncCnt++;
+
+                        VBOX_UTF16_FREE(MACAddressUtf16);
+                        VBOX_UTF8_FREE(MACAddress);
                     }
+
+                    VBOX_RELEASE(adapter);
                 }
+            }
 
-                /* dump sound card if active */
+            /* dump sound card if active */
 
-                /* Set def->nsounds to one as VirtualBox currently supports
-                 * only one sound card
-                 */
+            /* Set def->nsounds to one as VirtualBox currently supports
+             * only one sound card
+             */
 
-                machine->vtbl->GetAudioAdapter(machine, &audioAdapter);
-                if (audioAdapter) {
-                    PRBool enabled = PR_FALSE;
+            machine->vtbl->GetAudioAdapter(machine, &audioAdapter);
+            if (audioAdapter) {
+                PRBool enabled = PR_FALSE;
+
+                audioAdapter->vtbl->GetEnabled(audioAdapter, &enabled);
+                if (enabled) {
+                    PRUint32 audioController = AudioControllerType_AC97;
+
+                    def->nsounds = 1;
+                    if (VIR_ALLOC_N(def->sounds, def->nsounds) >= 0) {
+                        if (VIR_ALLOC(def->sounds[0]) >= 0) {
+                            audioAdapter->vtbl->GetAudioController(audioAdapter, &audioController);
+                            if (audioController == AudioControllerType_SB16) {
+                                def->sounds[0]->model = VIR_DOMAIN_SOUND_MODEL_SB16;
+                            } else if (audioController == AudioControllerType_AC97) {
+                                def->sounds[0]->model = VIR_DOMAIN_SOUND_MODEL_AC97;
+                            }
+                        } else {
+                            VIR_FREE(def->sounds);
+                            def->nsounds = 0;
+                            virReportOOMError(dom->conn);
+                        }
+                    } else {
+                        def->nsounds = 0;
+                        virReportOOMError(dom->conn);
+                    }
+                }
+                VBOX_RELEASE(audioAdapter);
+            }
 
-                    audioAdapter->vtbl->GetEnabled(audioAdapter, &enabled);
-                    if (enabled) {
-                        PRUint32 audioController = AudioControllerType_AC97;
-
-                        def->nsounds = 1;
-                        if (VIR_ALLOC_N(def->sounds, def->nsounds) >= 0) {
-                            if (VIR_ALLOC(def->sounds[0]) >= 0) {
-                                audioAdapter->vtbl->GetAudioController(audioAdapter, &audioController);
-                                if (audioController == AudioControllerType_SB16) {
-                                    def->sounds[0]->model = VIR_DOMAIN_SOUND_MODEL_SB16;
-                                } else if (audioController == AudioControllerType_AC97) {
-                                    def->sounds[0]->model = VIR_DOMAIN_SOUND_MODEL_AC97;
-                                }
+#if VBOX_API_VERSION < 3001
+            /* dump CDROM/DVD if the drive is attached and has DVD/CD in it */
+            machine->vtbl->GetDVDDrive(machine, &dvdDrive);
+            if (dvdDrive) {
+                PRUint32 state = DriveState_Null;
+
+                dvdDrive->vtbl->GetState(dvdDrive, &state);
+                if (state == DriveState_ImageMounted) {
+                    IDVDImage *dvdImage = NULL;
+
+                    dvdDrive->vtbl->GetImage(dvdDrive, &dvdImage);
+                    if (dvdImage) {
+                        PRUnichar *locationUtf16 = NULL;
+                        char *location           = NULL;
+
+                        dvdImage->vtbl->imedium.GetLocation((IMedium *)dvdImage, &locationUtf16);
+                        VBOX_UTF16_TO_UTF8(locationUtf16, &location);
+
+                        def->ndisks++;
+                        if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
+                            if (VIR_ALLOC(def->disks[def->ndisks - 1]) >= 0) {
+                                def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
+                                def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_IDE;
+                                def->disks[def->ndisks - 1]->type = VIR_DOMAIN_DISK_TYPE_FILE;
+                                def->disks[def->ndisks - 1]->readonly = 1;
+                                def->disks[def->ndisks - 1]->src = strdup(location);
+                                def->disks[def->ndisks - 1]->dst = strdup("hdc");
                             } else {
-                                VIR_FREE(def->sounds);
-                                def->nsounds = 0;
+                                def->ndisks--;
                                 virReportOOMError(dom->conn);
                             }
                         } else {
-                            def->nsounds = 0;
+                            def->ndisks--;
                             virReportOOMError(dom->conn);
                         }
+
+                        VBOX_UTF8_FREE(location);
+                        VBOX_UTF16_FREE(locationUtf16);
+                        VBOX_MEDIUM_RELEASE(dvdImage);
                     }
-                    audioAdapter->vtbl->nsisupports.Release((nsISupports *)audioAdapter);
                 }
+                VBOX_RELEASE(dvdDrive);
+            }
+
+            /* dump Floppy if the drive is attached and has floppy in it */
+            machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
+            if (floppyDrive) {
+                PRBool enabled = PR_FALSE;
 
-                /* dump CDROM/DVD if the drive is attached and has DVD/CD in it */
-                machine->vtbl->GetDVDDrive(machine, &dvdDrive);
-                if (dvdDrive) {
+                floppyDrive->vtbl->GetEnabled(floppyDrive, &enabled);
+                if (enabled) {
                     PRUint32 state = DriveState_Null;
 
-                    dvdDrive->vtbl->GetState(dvdDrive, &state);
+                    floppyDrive->vtbl->GetState(floppyDrive, &state);
                     if (state == DriveState_ImageMounted) {
-                        IDVDImage *dvdImage = NULL;
+                        IFloppyImage *floppyImage = NULL;
 
-                        dvdDrive->vtbl->GetImage(dvdDrive, &dvdImage);
-                        if (dvdImage) {
+                        floppyDrive->vtbl->GetImage(floppyDrive, &floppyImage);
+                        if (floppyImage) {
                             PRUnichar *locationUtf16 = NULL;
                             char *location           = NULL;
 
-                            dvdImage->vtbl->imedium.GetLocation((IMedium *)dvdImage, &locationUtf16);
-                            data->pFuncs->pfnUtf16ToUtf8(locationUtf16, &location);
+                            floppyImage->vtbl->imedium.GetLocation((IMedium *)floppyImage, &locationUtf16);
+                            VBOX_UTF16_TO_UTF8(locationUtf16, &location);
 
                             def->ndisks++;
                             if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
                                 if (VIR_ALLOC(def->disks[def->ndisks - 1]) >= 0) {
-                                    def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
-                                    def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_IDE;
+                                    def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
+                                    def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_FDC;
                                     def->disks[def->ndisks - 1]->type = VIR_DOMAIN_DISK_TYPE_FILE;
-                                    def->disks[def->ndisks - 1]->readonly = 1;
+                                    def->disks[def->ndisks - 1]->readonly = 0;
                                     def->disks[def->ndisks - 1]->src = strdup(location);
-                                    def->disks[def->ndisks - 1]->dst = strdup("hdc");
+                                    def->disks[def->ndisks - 1]->dst = strdup("fda");
                                 } else {
                                     def->ndisks--;
                                     virReportOOMError(dom->conn);
@@ -2392,305 +2794,257 @@ static char *vboxDomainDumpXML(virDomainPtr dom, int flags) {
                                 virReportOOMError(dom->conn);
                             }
 
-                            data->pFuncs->pfnUtf8Free(location);
-                            data->pFuncs->pfnUtf16Free(locationUtf16);
-                            dvdImage->vtbl->imedium.nsisupports.Release((nsISupports *)dvdImage);
+                            VBOX_UTF8_FREE(location);
+                            VBOX_UTF16_FREE(locationUtf16);
+                            VBOX_MEDIUM_RELEASE(floppyImage);
                         }
                     }
-                    dvdDrive->vtbl->nsisupports.Release((nsISupports *)dvdDrive);
                 }
 
-                /* dump Floppy if the drive is attached and has floppy in it */
-                machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
-                if (floppyDrive) {
+                VBOX_RELEASE(floppyDrive);
+            }
+#else  /* VBOX_API_VERSION >= 3001 */
+#endif /* VBOX_API_VERSION >= 3001 */
+
+            /* dump serial port if active */
+            def->nserials = 0;
+            /* Get which serial ports are enabled/active */
+            for (i = 0; i < serialPortCount; i++) {
+                ISerialPort *serialPort = NULL;
+
+                machine->vtbl->GetSerialPort(machine, i, &serialPort);
+                if (serialPort) {
                     PRBool enabled = PR_FALSE;
 
-                    floppyDrive->vtbl->GetEnabled(floppyDrive, &enabled);
+                    serialPort->vtbl->GetEnabled(serialPort, &enabled);
                     if (enabled) {
-                        PRUint32 state = DriveState_Null;
-
-                        floppyDrive->vtbl->GetState(floppyDrive, &state);
-                        if (state == DriveState_ImageMounted) {
-                            IFloppyImage *floppyImage = NULL;
-
-                            floppyDrive->vtbl->GetImage(floppyDrive, &floppyImage);
-                            if (floppyImage) {
-                                PRUnichar *locationUtf16 = NULL;
-                                char *location           = NULL;
-
-                                floppyImage->vtbl->imedium.GetLocation((IMedium *)floppyImage, &locationUtf16);
-                                data->pFuncs->pfnUtf16ToUtf8(locationUtf16, &location);
-
-                                def->ndisks++;
-                                if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
-                                    if (VIR_ALLOC(def->disks[def->ndisks - 1]) >= 0) {
-                                        def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
-                                        def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_FDC;
-                                        def->disks[def->ndisks - 1]->type = VIR_DOMAIN_DISK_TYPE_FILE;
-                                        def->disks[def->ndisks - 1]->readonly = 0;
-                                        def->disks[def->ndisks - 1]->src = strdup(location);
-                                        def->disks[def->ndisks - 1]->dst = strdup("fda");
-                                    } else {
-                                        def->ndisks--;
-                                        virReportOOMError(dom->conn);
-                                    }
-                                } else {
-                                    def->ndisks--;
-                                    virReportOOMError(dom->conn);
-                                }
-
-                                data->pFuncs->pfnUtf8Free(location);
-                                data->pFuncs->pfnUtf16Free(locationUtf16);
-                                floppyImage->vtbl->imedium.nsisupports.Release((nsISupports *)floppyImage);
-                            }
-                        }
+                        def->nserials++;
                     }
 
-                    floppyDrive->vtbl->nsisupports.Release((nsISupports *)floppyDrive);
+                    VBOX_RELEASE(serialPort);
                 }
+            }
 
-                /* dump serial port if active */
-                def->nserials = 0;
-                /* Get which serial ports are enabled/active */
-                for (i = 0; i < serialPortCount; i++) {
-                    ISerialPort *serialPort = NULL;
-
-                    machine->vtbl->GetSerialPort(machine, i, &serialPort);
-                    if (serialPort) {
-                        PRBool enabled = PR_FALSE;
-
-                        serialPort->vtbl->GetEnabled(serialPort, &enabled);
-                        if (enabled) {
-                            def->nserials++;
-                        }
-
-                        serialPort->vtbl->nsisupports.Release((nsISupports *)serialPort);
-                    }
+            /* Allocate memory for the serial ports which are enabled */
+            if ((def->nserials > 0) && (VIR_ALLOC_N(def->serials, def->nserials) >= 0)) {
+                for (i = 0; i < def->nserials; i++) {
+                    if (VIR_ALLOC(def->serials[i]) >= 0) {
+                    } else
+                        virReportOOMError(dom->conn);
                 }
+            }
 
-                /* Allocate memory for the serial ports which are enabled */
-                if ((def->nserials > 0) && (VIR_ALLOC_N(def->serials, def->nserials) >= 0)) {
-                    for (i = 0; i < def->nserials; i++) {
-                        if (VIR_ALLOC(def->serials[i]) >= 0) {
-                        } else
-                            virReportOOMError(dom->conn);
-                    }
-                }
+            /* Now get the details about the serial ports here */
+            for (i = 0;(serialPortIncCount < def->nserials) && (i < serialPortCount); i++) {
+                ISerialPort *serialPort = NULL;
 
-                /* Now get the details about the serial ports here */
-                for (i = 0;(serialPortIncCount < def->nserials) && (i < serialPortCount); i++) {
-                    ISerialPort *serialPort = NULL;
-
-                    machine->vtbl->GetSerialPort(machine, i, &serialPort);
-                    if (serialPort) {
-                        PRBool enabled = PR_FALSE;
-
-                        serialPort->vtbl->GetEnabled(serialPort, &enabled);
-                        if (enabled) {
-                            PRUint32 hostMode    = PortMode_Disconnected;
-                            PRUint32 IOBase      = 0;
-                            PRUint32 IRQ         = 0;
-                            PRUnichar *pathUtf16 = NULL;
-                            char *path           = NULL;
-
-                            serialPort->vtbl->GetHostMode(serialPort, &hostMode);
-                            if (hostMode == PortMode_HostPipe) {
-                                def->serials[serialPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_PIPE;
-                            } else if (hostMode == PortMode_HostDevice) {
-                                def->serials[serialPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_DEV;
-                            } else {
-                                def->serials[serialPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_NULL;
-                            }
+                machine->vtbl->GetSerialPort(machine, i, &serialPort);
+                if (serialPort) {
+                    PRBool enabled = PR_FALSE;
 
-                            serialPort->vtbl->GetIRQ(serialPort, &IRQ);
-                            serialPort->vtbl->GetIOBase(serialPort, &IOBase);
-                            if ((IRQ == 4) && (IOBase == 1016)) {
-                                def->serials[serialPortIncCount]->target.port = 0;
-                            } else if ((IRQ == 3) && (IOBase == 760)) {
-                                def->serials[serialPortIncCount]->target.port = 1;
-                            }
+                    serialPort->vtbl->GetEnabled(serialPort, &enabled);
+                    if (enabled) {
+                        PRUint32 hostMode    = PortMode_Disconnected;
+                        PRUint32 IOBase      = 0;
+                        PRUint32 IRQ         = 0;
+                        PRUnichar *pathUtf16 = NULL;
+                        char *path           = NULL;
+
+                        serialPort->vtbl->GetHostMode(serialPort, &hostMode);
+                        if (hostMode == PortMode_HostPipe) {
+                            def->serials[serialPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_PIPE;
+                        } else if (hostMode == PortMode_HostDevice) {
+                            def->serials[serialPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_DEV;
+                        } else {
+                            def->serials[serialPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_NULL;
+                        }
 
-                            serialPort->vtbl->GetPath(serialPort, &pathUtf16);
+                        serialPort->vtbl->GetIRQ(serialPort, &IRQ);
+                        serialPort->vtbl->GetIOBase(serialPort, &IOBase);
+                        if ((IRQ == 4) && (IOBase == 1016)) {
+                            def->serials[serialPortIncCount]->target.port = 0;
+                        } else if ((IRQ == 3) && (IOBase == 760)) {
+                            def->serials[serialPortIncCount]->target.port = 1;
+                        }
 
-                            data->pFuncs->pfnUtf16ToUtf8(pathUtf16, &path);
-                            def->serials[serialPortIncCount]->data.file.path = strdup(path);
+                        serialPort->vtbl->GetPath(serialPort, &pathUtf16);
 
-                            serialPortIncCount++;
+                        VBOX_UTF16_TO_UTF8(pathUtf16, &path);
+                        def->serials[serialPortIncCount]->data.file.path = strdup(path);
 
-                            data->pFuncs->pfnUtf16Free(pathUtf16);
-                            data->pFuncs->pfnUtf8Free(path);
-                        }
+                        serialPortIncCount++;
 
-                        serialPort->vtbl->nsisupports.Release((nsISupports *)serialPort);
+                        VBOX_UTF16_FREE(pathUtf16);
+                        VBOX_UTF8_FREE(path);
                     }
-                }
 
-                /* dump parallel ports if active */
-                def->nparallels = 0;
-                /* Get which parallel ports are enabled/active */
-                for (i = 0; i < parallelPortCount; i++) {
-                    IParallelPort *parallelPort = NULL;
+                    VBOX_RELEASE(serialPort);
+                }
+            }
 
-                    machine->vtbl->GetParallelPort(machine, i, &parallelPort);
-                    if (parallelPort) {
-                        PRBool enabled = PR_FALSE;
+            /* dump parallel ports if active */
+            def->nparallels = 0;
+            /* Get which parallel ports are enabled/active */
+            for (i = 0; i < parallelPortCount; i++) {
+                IParallelPort *parallelPort = NULL;
 
-                        parallelPort->vtbl->GetEnabled(parallelPort, &enabled);
-                        if (enabled) {
-                            def->nparallels++;
-                        }
+                machine->vtbl->GetParallelPort(machine, i, &parallelPort);
+                if (parallelPort) {
+                    PRBool enabled = PR_FALSE;
 
-                        parallelPort->vtbl->nsisupports.Release((nsISupports *)parallelPort);
+                    parallelPort->vtbl->GetEnabled(parallelPort, &enabled);
+                    if (enabled) {
+                        def->nparallels++;
                     }
+
+                    VBOX_RELEASE(parallelPort);
                 }
+            }
 
-                /* Allocate memory for the parallel ports which are enabled */
-                if ((def->nparallels > 0) && (VIR_ALLOC_N(def->parallels, def->nparallels) >= 0)) {
-                    for (i = 0; i < def->nparallels; i++) {
-                        if (VIR_ALLOC(def->parallels[i]) >= 0) {
-                        } else
-                            virReportOOMError(dom->conn);
-                    }
+            /* Allocate memory for the parallel ports which are enabled */
+            if ((def->nparallels > 0) && (VIR_ALLOC_N(def->parallels, def->nparallels) >= 0)) {
+                for (i = 0; i < def->nparallels; i++) {
+                    if (VIR_ALLOC(def->parallels[i]) >= 0) {
+                    } else
+                        virReportOOMError(dom->conn);
                 }
+            }
 
-                /* Now get the details about the parallel ports here */
-                for (i = 0;(parallelPortIncCount < def->nparallels) && (i < parallelPortCount); i++) {
-                    IParallelPort *parallelPort = NULL;
-
-                    machine->vtbl->GetParallelPort(machine, i, &parallelPort);
-                    if (parallelPort) {
-                        PRBool enabled = PR_FALSE;
-
-                        parallelPort->vtbl->GetEnabled(parallelPort, &enabled);
-                        if (enabled) {
-                            PRUint32 IOBase      = 0;
-                            PRUint32 IRQ         = 0;
-                            PRUnichar *pathUtf16 = NULL;
-                            char *path           = NULL;
-
-                            parallelPort->vtbl->GetIRQ(parallelPort, &IRQ);
-                            parallelPort->vtbl->GetIOBase(parallelPort, &IOBase);
-                            if ((IRQ == 7) && (IOBase == 888)) {
-                                def->parallels[parallelPortIncCount]->target.port = 0;
-                            } else if ((IRQ == 5) && (IOBase == 632)) {
-                                def->parallels[parallelPortIncCount]->target.port = 1;
-                            }
+            /* Now get the details about the parallel ports here */
+            for (i = 0;(parallelPortIncCount < def->nparallels) && (i < parallelPortCount); i++) {
+                IParallelPort *parallelPort = NULL;
 
-                            def->parallels[parallelPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_FILE;
+                machine->vtbl->GetParallelPort(machine, i, &parallelPort);
+                if (parallelPort) {
+                    PRBool enabled = PR_FALSE;
 
-                            parallelPort->vtbl->GetPath(parallelPort, &pathUtf16);
+                    parallelPort->vtbl->GetEnabled(parallelPort, &enabled);
+                    if (enabled) {
+                        PRUint32 IOBase      = 0;
+                        PRUint32 IRQ         = 0;
+                        PRUnichar *pathUtf16 = NULL;
+                        char *path           = NULL;
+
+                        parallelPort->vtbl->GetIRQ(parallelPort, &IRQ);
+                        parallelPort->vtbl->GetIOBase(parallelPort, &IOBase);
+                        if ((IRQ == 7) && (IOBase == 888)) {
+                            def->parallels[parallelPortIncCount]->target.port = 0;
+                        } else if ((IRQ == 5) && (IOBase == 632)) {
+                            def->parallels[parallelPortIncCount]->target.port = 1;
+                        }
 
-                            data->pFuncs->pfnUtf16ToUtf8(pathUtf16, &path);
-                            def->parallels[parallelPortIncCount]->data.file.path = strdup(path);
+                        def->parallels[parallelPortIncCount]->type = VIR_DOMAIN_CHR_TYPE_FILE;
 
-                            parallelPortIncCount++;
+                        parallelPort->vtbl->GetPath(parallelPort, &pathUtf16);
 
-                            data->pFuncs->pfnUtf16Free(pathUtf16);
-                            data->pFuncs->pfnUtf8Free(path);
-                        }
+                        VBOX_UTF16_TO_UTF8(pathUtf16, &path);
+                        def->parallels[parallelPortIncCount]->data.file.path = strdup(path);
+
+                        parallelPortIncCount++;
 
-                        parallelPort->vtbl->nsisupports.Release((nsISupports *)parallelPort);
+                        VBOX_UTF16_FREE(pathUtf16);
+                        VBOX_UTF8_FREE(path);
                     }
+
+                    VBOX_RELEASE(parallelPort);
                 }
+            }
 
-                /* dump USB devices/filters if active */
-                def->nhostdevs = 0;
-                machine->vtbl->GetUSBController(machine, &USBController);
-                if (USBController) {
-                    PRBool enabled = PR_FALSE;
+            /* dump USB devices/filters if active */
+            def->nhostdevs = 0;
+            machine->vtbl->GetUSBController(machine, &USBController);
+            if (USBController) {
+                PRBool enabled = PR_FALSE;
 
-                    USBController->vtbl->GetEnabled(USBController, &enabled);
-                    if (enabled) {
-                        PRUint32 deviceFiltersNum        = 0;
-                        IUSBDeviceFilter **deviceFilters = NULL;
+                USBController->vtbl->GetEnabled(USBController, &enabled);
+                if (enabled) {
+                    PRUint32 deviceFiltersNum        = 0;
+                    IUSBDeviceFilter **deviceFilters = NULL;
 
-                        USBController->vtbl->GetDeviceFilters(USBController,
-                                                              &deviceFiltersNum,
-                                                              &deviceFilters);
+                    USBController->vtbl->GetDeviceFilters(USBController,
+                                                          &deviceFiltersNum,
+                                                          &deviceFilters);
 
-                        if (deviceFiltersNum > 0) {
+                    if (deviceFiltersNum > 0) {
 
-                            /* check if the filters are active and then only
-                             * alloc mem and set def->nhostdevs
-                             */
+                        /* check if the filters are active and then only
+                         * alloc mem and set def->nhostdevs
+                         */
 
-                            for(i = 0; i < deviceFiltersNum; i++) {
-                                PRBool active = PR_FALSE;
+                        for(i = 0; i < deviceFiltersNum; i++) {
+                            PRBool active = PR_FALSE;
 
-                                deviceFilters[i]->vtbl->GetActive(deviceFilters[i], &active);
-                                if (active) {
-                                    def->nhostdevs++;
-                                }
+                            deviceFilters[i]->vtbl->GetActive(deviceFilters[i], &active);
+                            if (active) {
+                                def->nhostdevs++;
                             }
+                        }
 
-                            if (def->nhostdevs > 0) {
-                                /* Alloc mem needed for the filters now */
-                                if (VIR_ALLOC_N(def->hostdevs, def->nhostdevs) >= 0) {
+                        if (def->nhostdevs > 0) {
+                            /* Alloc mem needed for the filters now */
+                            if (VIR_ALLOC_N(def->hostdevs, def->nhostdevs) >= 0) {
 
-                                    for(i = 0; (USBFilterCount < def->nhostdevs) || (i < deviceFiltersNum); i++) {
-                                        PRBool active = PR_FALSE;
+                                for(i = 0; (USBFilterCount < def->nhostdevs) || (i < deviceFiltersNum); i++) {
+                                    PRBool active = PR_FALSE;
 
-                                        deviceFilters[i]->vtbl->GetActive(deviceFilters[i], &active);
-                                        if (active) {
-                                            if (VIR_ALLOC(def->hostdevs[USBFilterCount]) >= 0) {
-                                                PRUnichar *vendorIdUtf16  = NULL;
-                                                char *vendorIdUtf8        = NULL;
-                                                unsigned vendorId         = 0;
-                                                PRUnichar *productIdUtf16 = NULL;
-                                                char *productIdUtf8       = NULL;
-                                                unsigned productId        = 0;
-                                                char *endptr              = NULL;
+                                    deviceFilters[i]->vtbl->GetActive(deviceFilters[i], &active);
+                                    if (active) {
+                                        if (VIR_ALLOC(def->hostdevs[USBFilterCount]) >= 0) {
+                                            PRUnichar *vendorIdUtf16  = NULL;
+                                            char *vendorIdUtf8        = NULL;
+                                            unsigned vendorId         = 0;
+                                            PRUnichar *productIdUtf16 = NULL;
+                                            char *productIdUtf8       = NULL;
+                                            unsigned productId        = 0;
+                                            char *endptr              = NULL;
 
-                                                def->hostdevs[USBFilterCount]->mode =
-                                                    VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
-                                                def->hostdevs[USBFilterCount]->source.subsys.type =
-                                                    VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB;
+                                            def->hostdevs[USBFilterCount]->mode =
+                                                VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
+                                            def->hostdevs[USBFilterCount]->source.subsys.type =
+                                                VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB;
 
-                                                deviceFilters[i]->vtbl->GetVendorId(deviceFilters[i], &vendorIdUtf16);
-                                                deviceFilters[i]->vtbl->GetProductId(deviceFilters[i], &productIdUtf16);
+                                            deviceFilters[i]->vtbl->GetVendorId(deviceFilters[i], &vendorIdUtf16);
+                                            deviceFilters[i]->vtbl->GetProductId(deviceFilters[i], &productIdUtf16);
 
-                                                data->pFuncs->pfnUtf16ToUtf8(vendorIdUtf16, &vendorIdUtf8);
-                                                data->pFuncs->pfnUtf16ToUtf8(productIdUtf16, &productIdUtf8);
+                                            VBOX_UTF16_TO_UTF8(vendorIdUtf16, &vendorIdUtf8);
+                                            VBOX_UTF16_TO_UTF8(productIdUtf16, &productIdUtf8);
 
-                                                vendorId  = strtol(vendorIdUtf8, &endptr, 16);
-                                                productId = strtol(productIdUtf8, &endptr, 16);
+                                            vendorId  = strtol(vendorIdUtf8, &endptr, 16);
+                                            productId = strtol(productIdUtf8, &endptr, 16);
 
-                                                def->hostdevs[USBFilterCount]->source.subsys.u.usb.vendor  = vendorId;
-                                                def->hostdevs[USBFilterCount]->source.subsys.u.usb.product = productId;
+                                            def->hostdevs[USBFilterCount]->source.subsys.u.usb.vendor  = vendorId;
+                                            def->hostdevs[USBFilterCount]->source.subsys.u.usb.product = productId;
 
-                                                data->pFuncs->pfnUtf16Free(vendorIdUtf16);
-                                                data->pFuncs->pfnUtf8Free(vendorIdUtf8);
+                                            VBOX_UTF16_FREE(vendorIdUtf16);
+                                            VBOX_UTF8_FREE(vendorIdUtf8);
 
-                                                data->pFuncs->pfnUtf16Free(productIdUtf16);
-                                                data->pFuncs->pfnUtf8Free(productIdUtf8);
+                                            VBOX_UTF16_FREE(productIdUtf16);
+                                            VBOX_UTF8_FREE(productIdUtf8);
 
-                                                USBFilterCount++;
-                                            } else
-                                                virReportOOMError(dom->conn);
-                                        }
+                                            USBFilterCount++;
+                                        } else
+                                            virReportOOMError(dom->conn);
                                     }
-                                } else
-                                    virReportOOMError(dom->conn);
-                            }
-                        }
-
-                        /* Cleanup */
-                        for(i = 0; i < deviceFiltersNum; i++) {
-                            if (deviceFilters[i])
-                                deviceFilters[i]->vtbl->nsisupports.Release((nsISupports *)deviceFilters[i]);
+                                }
+                            } else
+                                virReportOOMError(dom->conn);
                         }
                     }
-                    USBController->vtbl->nsisupports.Release((nsISupports *)USBController);
-                }
 
-                /* all done so set gotAllABoutDef and pass def to virDomainDefFormat
-                 * to generate XML for it
-                 */
-                gotAllABoutDef = 0;
+                    /* Cleanup */
+                    for(i = 0; i < deviceFiltersNum; i++)
+                        VBOX_RELEASE(deviceFilters[i]);
+                }
+                VBOX_RELEASE(USBController);
             }
-            machine->vtbl->nsisupports.Release((nsISupports *)machine);
-            machine = NULL;
+
+            /* all done so set gotAllABoutDef and pass def to virDomainDefFormat
+             * to generate XML for it
+             */
+            gotAllABoutDef = 0;
         }
+        VBOX_RELEASE(machine);
+        machine = NULL;
     }
 
     if (gotAllABoutDef == 0)
@@ -2703,128 +3057,107 @@ cleanup:
 }
 
 static int vboxListDefinedDomains(virConnectPtr conn, char ** const names, int maxnames) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, int, -1);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
     char *machineName    = NULL;
     PRUnichar *machineNameUtf16 = NULL;
     PRUint32 state;
-    int ret = -1;
+    nsresult rc;
     int i, j;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of Defined Domains",(unsigned)rc);
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of Defined Domains",(unsigned)rc);
+        goto cleanup;
+    }
 
-        if (machineCnt == 0) {
-            ret = 0;
-            goto cleanup;
-        }
+    if (machineCnt == 0) {
+        ret = 0;
+        goto cleanup;
+    }
 
-        for (i = 0,j = 0; (i < machineCnt) && (j < maxnames); i++) {
-            IMachine *machine = machines[i];
-
-            if (machine) {
-                PRBool isAccessible = PR_FALSE;
-                machine->vtbl->GetAccessible(machine, &isAccessible);
-                if (isAccessible) {
-                    machine->vtbl->GetState(machine, &state);
-                    if ((state != MachineState_Starting)   &&
-                        (state != MachineState_Running)    &&
-                        (state != MachineState_Stuck)      &&
-                        (state != MachineState_Stopping)   &&
-                        (state != MachineState_Saving)     &&
-                        (state != MachineState_Restoring)  &&
-                        (state != MachineState_Discarding) &&
-                        (state != MachineState_Paused) ) {
-                        machine->vtbl->GetName(machine, &machineNameUtf16);
-                        data->pFuncs->pfnUtf16ToUtf8(machineNameUtf16, &machineName);
-                        if (!(names[j++] = strdup(machineName))) {
-                            virReportOOMError(conn);
-                            for ( ; j >= 0 ; j--)
-                                VIR_FREE(names[j]);
-                            ret = -1;
-                            goto cleanup;
-                        }
-                        ret++;
+    for (i = 0,j = 0; (i < machineCnt) && (j < maxnames); i++) {
+        IMachine *machine = machines[i];
+
+        if (machine) {
+            PRBool isAccessible = PR_FALSE;
+            machine->vtbl->GetAccessible(machine, &isAccessible);
+            if (isAccessible) {
+                machine->vtbl->GetState(machine, &state);
+                if (   (state < MachineState_FirstOnline)
+                    || (state > MachineState_LastOnline) ) {
+                    machine->vtbl->GetName(machine, &machineNameUtf16);
+                    VBOX_UTF16_TO_UTF8(machineNameUtf16, &machineName);
+                    if (!(names[j++] = strdup(machineName))) {
+                        virReportOOMError(conn);
+                        for ( ; j >= 0 ; j--)
+                            VIR_FREE(names[j]);
+                        ret = -1;
+                        goto cleanup;
                     }
+                    ret++;
                 }
             }
         }
-        ret++;
     }
+    ret++;
 
 cleanup:
-    data->pFuncs->pfnUtf8Free(machineName);
-    data->pFuncs->pfnUtf16Free(machineNameUtf16);
+    VBOX_UTF8_FREE(machineName);
+    VBOX_UTF16_FREE(machineNameUtf16);
     for (i = 0; i < machineCnt; ++i)
-        if (machines[i])
-            machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+        VBOX_RELEASE(machines[i]);
     return ret;
 }
 
 static int vboxNumOfDefinedDomains(virConnectPtr conn) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, int, -1);
     IMachine **machines  = NULL;
     PRUint32 machineCnt  = 0;
     PRUint32 state       = MachineState_Null;
-    int ret = -1;
+    nsresult rc;
     int i;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get number of Defined Domains",(unsigned)rc);
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get number of Defined Domains",(unsigned)rc);
+        goto cleanup;
+    }
 
-        if (machineCnt == 0) {
-            ret = 0;
-            goto cleanup;
-        }
+    if (machineCnt == 0) {
+        ret = 0;
+        goto cleanup;
+    }
 
-        /* Do the cleanup as required by GetMachines() */
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-
-            if (machine) {
-                PRBool isAccessible = PR_FALSE;
-                machine->vtbl->GetAccessible(machine, &isAccessible);
-                if (isAccessible) {
-                    machine->vtbl->GetState(machine, &state);
-                    if ((state != MachineState_Starting)   &&
-                        (state != MachineState_Running)    &&
-                        (state != MachineState_Stuck)      &&
-                        (state != MachineState_Stopping)   &&
-                        (state != MachineState_Saving)     &&
-                        (state != MachineState_Restoring)  &&
-                        (state != MachineState_Discarding) &&
-                        (state != MachineState_Paused) ) {
-                        ret++;
-                    }
+    /* Do the cleanup as required by GetMachines() */
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+
+        if (machine) {
+            PRBool isAccessible = PR_FALSE;
+            machine->vtbl->GetAccessible(machine, &isAccessible);
+            if (isAccessible) {
+                machine->vtbl->GetState(machine, &state);
+                if (   (state < MachineState_FirstOnline)
+                    || (state > MachineState_LastOnline) ) {
+                    ret++;
                 }
             }
         }
-        ret++;
     }
+    ret++;
 
 cleanup:
     for (i = 0; i < machineCnt; ++i)
-        if (machines[i])
-            machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+        VBOX_RELEASE(machines[i]);
     return ret;
 }
 
 static int vboxDomainCreate(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data   = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine **machines    = NULL;
     IProgress *progress    = NULL;
     PRUint32 machineCnt    = 0;
@@ -2832,8 +3165,8 @@ static int vboxDomainCreate(virDomainPtr dom) {
     PRUnichar *sessionType = NULL;
     char displayutf8[32]   = {0};
     unsigned char iidl[VIR_UUID_BUFLEN] = {0};
-    int i, ret = -1;
-
+    nsresult rc;
+    int i = 0;
 
     if (!dom->name) {
         vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s",
@@ -2841,221 +3174,215 @@ static int vboxDomainCreate(virDomainPtr dom) {
         goto cleanup;
     }
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "Could not get list of machines",(unsigned)rc);
-            goto cleanup;
-        }
-
-        for (i = 0; i < machineCnt; ++i) {
-            IMachine *machine = machines[i];
-            PRBool isAccessible = PR_FALSE;
-
-            if (!machine)
-                continue;
-
-            machine->vtbl->GetAccessible(machine, &isAccessible);
-            if (isAccessible) {
-                vboxIID *iid = NULL;
+    rc = data->vboxObj->vtbl->GetMachines(data->vboxObj, &machineCnt, &machines);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "Could not get list of machines",(unsigned)rc);
+        goto cleanup;
+    }
 
-                machine->vtbl->GetId(machine, &iid);
-                if (!iid)
-                    continue;
-                vboxIIDToUUID(iidl, iid);
+    for (i = 0; i < machineCnt; ++i) {
+        IMachine *machine = machines[i];
+        PRBool isAccessible = PR_FALSE;
 
-                if (memcmp(dom->uuid, iidl, VIR_UUID_BUFLEN) == 0) {
-                    PRUint32 state = MachineState_Null;
-                    machine->vtbl->GetState(machine, &state);
-
-                    if ( (state == MachineState_PoweredOff) ||
-                         (state == MachineState_Saved) ||
-                         (state == MachineState_Aborted) ) {
-                        int vrdpPresent              = 0;
-                        int sdlPresent               = 0;
-                        int guiPresent               = 0;
-                        char *guiDisplay             = NULL;
-                        char *sdlDisplay             = NULL;
-                        PRUnichar *keyTypeUtf16      = NULL;
-                        PRUnichar *valueTypeUtf16    = NULL;
-                        char      *valueTypeUtf8     = NULL;
-                        PRUnichar *keyDislpayUtf16   = NULL;
-                        PRUnichar *valueDisplayUtf16 = NULL;
-                        char      *valueDisplayUtf8  = NULL;
+        if (!machine)
+            continue;
 
-                        data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Type", &keyTypeUtf16);
-                        machine->vtbl->GetExtraData(machine, keyTypeUtf16, &valueTypeUtf16);
-                        data->pFuncs->pfnUtf16Free(keyTypeUtf16);
+        machine->vtbl->GetAccessible(machine, &isAccessible);
+        if (isAccessible) {
+            vboxIID *iid = NULL;
 
-                        if (valueTypeUtf16) {
-                            data->pFuncs->pfnUtf16ToUtf8(valueTypeUtf16, &valueTypeUtf8);
-                            data->pFuncs->pfnUtf16Free(valueTypeUtf16);
+            machine->vtbl->GetId(machine, &iid);
+            if (!iid)
+                continue;
+            vboxIIDToUUID(iidl, iid);
+
+            if (memcmp(dom->uuid, iidl, VIR_UUID_BUFLEN) == 0) {
+                PRUint32 state = MachineState_Null;
+                machine->vtbl->GetState(machine, &state);
+
+                if ( (state == MachineState_PoweredOff) ||
+                     (state == MachineState_Saved) ||
+                     (state == MachineState_Aborted) ) {
+                    int vrdpPresent              = 0;
+                    int sdlPresent               = 0;
+                    int guiPresent               = 0;
+                    char *guiDisplay             = NULL;
+                    char *sdlDisplay             = NULL;
+                    PRUnichar *keyTypeUtf16      = NULL;
+                    PRUnichar *valueTypeUtf16    = NULL;
+                    char      *valueTypeUtf8     = NULL;
+                    PRUnichar *keyDislpayUtf16   = NULL;
+                    PRUnichar *valueDisplayUtf16 = NULL;
+                    char      *valueDisplayUtf8  = NULL;
+
+                    VBOX_UTF8_TO_UTF16("FRONTEND/Type", &keyTypeUtf16);
+                    machine->vtbl->GetExtraData(machine, keyTypeUtf16, &valueTypeUtf16);
+                    VBOX_UTF16_FREE(keyTypeUtf16);
 
-                            if ( STREQ(valueTypeUtf8, "sdl") || STREQ(valueTypeUtf8, "gui") ) {
+                    if (valueTypeUtf16) {
+                        VBOX_UTF16_TO_UTF8(valueTypeUtf16, &valueTypeUtf8);
+                        VBOX_UTF16_FREE(valueTypeUtf16);
 
-                                data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Display", &keyDislpayUtf16);
-                                machine->vtbl->GetExtraData(machine, keyDislpayUtf16, &valueDisplayUtf16);
-                                data->pFuncs->pfnUtf16Free(keyDislpayUtf16);
+                        if ( STREQ(valueTypeUtf8, "sdl") || STREQ(valueTypeUtf8, "gui") ) {
 
-                                if (valueDisplayUtf16) {
-                                    data->pFuncs->pfnUtf16ToUtf8(valueDisplayUtf16, &valueDisplayUtf8);
-                                    data->pFuncs->pfnUtf16Free(valueDisplayUtf16);
+                            VBOX_UTF8_TO_UTF16("FRONTEND/Display", &keyDislpayUtf16);
+                            machine->vtbl->GetExtraData(machine, keyDislpayUtf16, &valueDisplayUtf16);
+                            VBOX_UTF16_FREE(keyDislpayUtf16);
 
-                                    if (strlen(valueDisplayUtf8) <= 0) {
-                                        data->pFuncs->pfnUtf8Free(valueDisplayUtf8);
-                                        valueDisplayUtf8 = NULL;
-                                    }
-                                }
+                            if (valueDisplayUtf16) {
+                                VBOX_UTF16_TO_UTF8(valueDisplayUtf16, &valueDisplayUtf8);
+                                VBOX_UTF16_FREE(valueDisplayUtf16);
 
-                                if (STREQ(valueTypeUtf8, "sdl")) {
-                                    sdlPresent = 1;
-                                    if (valueDisplayUtf8) {
-                                        sdlDisplay = strdup(valueDisplayUtf8);
-                                        if (sdlDisplay == NULL) {
-                                            vboxError(dom->conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
-                                            /* just don't go to cleanup yet as it is ok to have
-                                             * sdlDisplay as NULL and we check it below if it
-                                             * exist and then only use it there
-                                             */
-                                        }
-                                    }
+                                if (strlen(valueDisplayUtf8) <= 0) {
+                                    VBOX_UTF8_FREE(valueDisplayUtf8);
+                                    valueDisplayUtf8 = NULL;
                                 }
+                            }
 
-                                if (STREQ(valueTypeUtf8, "gui")) {
-                                    guiPresent = 1;
-                                    if (valueDisplayUtf8) {
-                                        guiDisplay = strdup(valueDisplayUtf8);
-                                        if (guiDisplay == NULL) {
-                                            vboxError(dom->conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
-                                            /* just don't go to cleanup yet as it is ok to have
-                                             * guiDisplay as NULL and we check it below if it
-                                             * exist and then only use it there
-                                             */
-                                        }
+                            if (STREQ(valueTypeUtf8, "sdl")) {
+                                sdlPresent = 1;
+                                if (valueDisplayUtf8) {
+                                    sdlDisplay = strdup(valueDisplayUtf8);
+                                    if (sdlDisplay == NULL) {
+                                        vboxError(dom->conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
+                                        /* just don't go to cleanup yet as it is ok to have
+                                         * sdlDisplay as NULL and we check it below if it
+                                         * exist and then only use it there
+                                         */
                                     }
                                 }
                             }
 
-                            if (STREQ(valueTypeUtf8, "vrdp")) {
-                                vrdpPresent = 1;
-                            }
-
-                            if (!vrdpPresent && !sdlPresent && !guiPresent) {
-                                /* if nothing is selected it means either the machine xml
-                                 * file is really old or some values are missing so fallback
-                                 */
+                            if (STREQ(valueTypeUtf8, "gui")) {
                                 guiPresent = 1;
+                                if (valueDisplayUtf8) {
+                                    guiDisplay = strdup(valueDisplayUtf8);
+                                    if (guiDisplay == NULL) {
+                                        vboxError(dom->conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
+                                        /* just don't go to cleanup yet as it is ok to have
+                                         * guiDisplay as NULL and we check it below if it
+                                         * exist and then only use it there
+                                         */
+                                    }
+                                }
                             }
+                        }
 
-                            data->pFuncs->pfnUtf8Free(valueTypeUtf8);
+                        if (STREQ(valueTypeUtf8, "vrdp")) {
+                            vrdpPresent = 1;
+                        }
 
-                        } else {
+                        if (!vrdpPresent && !sdlPresent && !guiPresent) {
+                            /* if nothing is selected it means either the machine xml
+                             * file is really old or some values are missing so fallback
+                             */
                             guiPresent = 1;
                         }
-                        if (valueDisplayUtf8)
-                            data->pFuncs->pfnUtf8Free(valueDisplayUtf8);
 
-                        if (guiPresent) {
-                            if (guiDisplay) {
-                                sprintf(displayutf8, "DISPLAY=%.24s", guiDisplay);
-                                data->pFuncs->pfnUtf8ToUtf16(displayutf8, &env);
-                                VIR_FREE(guiDisplay);
-                            }
+                        VBOX_UTF8_FREE(valueTypeUtf8);
 
-                            data->pFuncs->pfnUtf8ToUtf16("gui", &sessionType);
+                    } else {
+                        guiPresent = 1;
+                    }
+                    if (valueDisplayUtf8)
+                        VBOX_UTF8_FREE(valueDisplayUtf8);
+
+                    if (guiPresent) {
+                        if (guiDisplay) {
+                            sprintf(displayutf8, "DISPLAY=%.24s", guiDisplay);
+                            VBOX_UTF8_TO_UTF16(displayutf8, &env);
+                            VIR_FREE(guiDisplay);
                         }
 
-                        if (sdlPresent) {
-                            if (sdlDisplay) {
-                                sprintf(displayutf8, "DISPLAY=%.24s", sdlDisplay);
-                                data->pFuncs->pfnUtf8ToUtf16(displayutf8, &env);
-                                VIR_FREE(sdlDisplay);
-                            }
+                        VBOX_UTF8_TO_UTF16("gui", &sessionType);
+                    }
 
-                            data->pFuncs->pfnUtf8ToUtf16("sdl", &sessionType);
+                    if (sdlPresent) {
+                        if (sdlDisplay) {
+                            sprintf(displayutf8, "DISPLAY=%.24s", sdlDisplay);
+                            VBOX_UTF8_TO_UTF16(displayutf8, &env);
+                            VIR_FREE(sdlDisplay);
                         }
 
-                        if (vrdpPresent) {
-                            data->pFuncs->pfnUtf8ToUtf16("vrdp", &sessionType);
-                        }
+                        VBOX_UTF8_TO_UTF16("sdl", &sessionType);
+                    }
 
-                        rc = data->vboxObj->vtbl->OpenRemoteSession(data->vboxObj,
-                                                                    data->vboxSession,
-                                                                    iid,
-                                                                    sessionType,
-                                                                    env,
-                                                                    &progress );
-                        if (NS_FAILED(rc)) {
-                            vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                                      "%s", "openremotesession failed, domain can't be started");
-                            ret = -1;
-                        } else {
-                            PRBool completed = 0;
+                    if (vrdpPresent) {
+                        VBOX_UTF8_TO_UTF16("vrdp", &sessionType);
+                    }
+
+                    rc = data->vboxObj->vtbl->OpenRemoteSession(data->vboxObj,
+                                                                data->vboxSession,
+                                                                iid,
+                                                                sessionType,
+                                                                env,
+                                                                &progress );
+                    if (NS_FAILED(rc)) {
+                        vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                                  "%s", "openremotesession failed, domain can't be started");
+                        ret = -1;
+                    } else {
+                        PRBool completed = 0;
 #if VBOX_API_VERSION == 2002
-                            nsresult resultCode;
+                        nsresult resultCode;
 #else
-                            PRInt32  resultCode;
+                        PRInt32  resultCode;
 #endif
-                            progress->vtbl->WaitForCompletion(progress, -1);
-                            rc = progress->vtbl->GetCompleted(progress, &completed);
-                            if (NS_FAILED(rc)) {
-                                /* error */
-                                ret = -1;
-                            }
-                            progress->vtbl->GetResultCode(progress, &resultCode);
-                            if (NS_FAILED(resultCode)) {
-                                /* error */
-                                ret = -1;
-                            } else {
-                                /* all ok set the domid */
-                                dom->id = i + 1;
-                                ret = 0;
-                            }
+                        progress->vtbl->WaitForCompletion(progress, -1);
+                        rc = progress->vtbl->GetCompleted(progress, &completed);
+                        if (NS_FAILED(rc)) {
+                            /* error */
+                            ret = -1;
+                        }
+                        progress->vtbl->GetResultCode(progress, &resultCode);
+                        if (NS_FAILED(resultCode)) {
+                            /* error */
+                            ret = -1;
+                        } else {
+                            /* all ok set the domid */
+                            dom->id = i + 1;
+                            ret = 0;
                         }
+                    }
 
-                        if (progress)
-                            progress->vtbl->nsisupports.Release((nsISupports *)progress);
+                    VBOX_RELEASE(progress);
 
-                        data->vboxSession->vtbl->Close(data->vboxSession);
+                    data->vboxSession->vtbl->Close(data->vboxSession);
 
-                    } else {
-                        vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
-                                  "%s", "machine is not in poweroff|saved|"
-                                        "aborted state, so couldn't start it");
-                        ret = -1;
-                    }
+                } else {
+                    vboxError(dom->conn, VIR_ERR_OPERATION_FAILED,
+                              "%s", "machine is not in poweroff|saved|"
+                                    "aborted state, so couldn't start it");
+                    ret = -1;
                 }
-                vboxIIDUnalloc(iid);
-                if (ret != -1)
-                    break;
             }
-        }
-
-        /* Do the cleanup and take care you dont leak any memory */
-        for (i = 0; i < machineCnt; ++i) {
-            if (machines[i])
-                machines[i]->vtbl->nsisupports.Release((nsISupports *)machines[i]);
+            vboxIIDUnalloc(iid);
+            if (ret != -1)
+                break;
         }
     }
 
-    data->pFuncs->pfnUtf16Free(env);
-    data->pFuncs->pfnUtf16Free(sessionType);
+    /* Do the cleanup and take care you dont leak any memory */
+    for (i = 0; i < machineCnt; ++i)
+        VBOX_RELEASE(machines[i]);
+
+    VBOX_UTF16_FREE(env);
+    VBOX_UTF16_FREE(sessionType);
 
 cleanup:
     return ret;
 }
 
 static virDomainPtr vboxDomainDefineXML(virConnectPtr conn, const char *xml) {
-    nsresult rc;
-    vboxGlobalData *data        = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, virDomainPtr, NULL);
     IMachine       *machine     = NULL;
     IBIOSSettings  *bios        = NULL;
-    virDomainPtr    dom         = NULL;
     vboxIID        *iid         = NULL;
     vboxIID        *mchiid      = NULL;
     virDomainDefPtr def         = NULL;
     PRUnichar *machineNameUtf16 = NULL;
+    nsresult rc;
 
     if (!(def = virDomainDefParseString(conn, data->caps, xml,
                                         VIR_DOMAIN_XML_INACTIVE))) {
@@ -3069,446 +3396,656 @@ static virDomainPtr vboxDomainDefineXML(virConnectPtr conn, const char *xml) {
     }
 #endif
 
-    if (data->vboxObj) {
-        data->pFuncs->pfnUtf8ToUtf16(def->name, &machineNameUtf16);
-        vboxIIDFromUUID(def->uuid, iid);
-        rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
-                                                machineNameUtf16,
-                                                NULL,
-                                                NULL,
-                                                iid,
-                                                &machine);
-        data->pFuncs->pfnUtf16Free(machineNameUtf16);
+    VBOX_UTF8_TO_UTF16(def->name, &machineNameUtf16);
+    vboxIIDFromUUID(def->uuid, iid);
+    rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
+                                            machineNameUtf16,
+                                            NULL,
+                                            NULL,
+                                            iid,
+                                            &machine);
+    VBOX_UTF16_FREE(machineNameUtf16);
+
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "could not define a domain",(unsigned)rc);
+        goto cleanup;
+    }
 
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "could not define a domain",(unsigned)rc);
-            goto cleanup;
-        }
+    rc = machine->vtbl->SetMemorySize(machine, def->memory / 1024);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%lu Kb, rc=%08x",
+                  "could not set the memory size of the domain to",
+                  def->memory, (unsigned)rc);
+    }
 
-        rc = machine->vtbl->SetMemorySize(machine, def->memory / 1024);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%lu Kb, rc=%08x",
-                      "could not set the memory size of the domain to",
-                      def->memory, (unsigned)rc);
-        }
+    rc = machine->vtbl->SetCPUCount(machine, def->vcpus);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%lu, rc=%08x",
+                  "could not set the number of virtual CPUs to",
+                  def->vcpus, (unsigned)rc);
+    }
 
-        rc = machine->vtbl->SetCPUCount(machine, def->vcpus);
-        if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%lu, rc=%08x",
-                      "could not set the number of virtual CPUs to",
-                      def->vcpus, (unsigned)rc);
-        }
+#if VBOX_API_VERSION < 3001
+    rc = machine->vtbl->SetPAEEnabled(machine, (def->features) &
+                                      (1 << VIR_DOMAIN_FEATURE_PAE));
+#else  /* VBOX_API_VERSION >= 3001 */
+    rc = machine->vtbl->SetCpuProperty(machine, CpuPropertyType_PAE,
+                                       (def->features) &
+                                       (1 << VIR_DOMAIN_FEATURE_PAE));
+#endif /* VBOX_API_VERSION >= 3001 */
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                  "could not change PAE status to",
+                  ((def->features) & (1 << VIR_DOMAIN_FEATURE_PAE))
+                  ? "Enabled" : "Disabled", (unsigned)rc);
+    }
 
-        rc = machine->vtbl->SetPAEEnabled(machine, (def->features) &
-                                          (1 << VIR_DOMAIN_FEATURE_PAE));
+    machine->vtbl->GetBIOSSettings(machine, &bios);
+    if (bios) {
+        rc = bios->vtbl->SetACPIEnabled(bios, (def->features) &
+                                        (1 << VIR_DOMAIN_FEATURE_ACPI));
         if (NS_FAILED(rc)) {
             vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                      "could not change PAE status to",
-                      ((def->features) & (1 << VIR_DOMAIN_FEATURE_PAE))
+                      "could not change ACPI status to",
+                      ((def->features) & (1 << VIR_DOMAIN_FEATURE_ACPI))
                       ? "Enabled" : "Disabled", (unsigned)rc);
         }
-
-        machine->vtbl->GetBIOSSettings(machine, &bios);
-        if (bios) {
-            rc = bios->vtbl->SetACPIEnabled(bios, (def->features) &
-                                            (1 << VIR_DOMAIN_FEATURE_ACPI));
-            if (NS_FAILED(rc)) {
-                vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                          "could not change ACPI status to",
-                          ((def->features) & (1 << VIR_DOMAIN_FEATURE_ACPI))
-                          ? "Enabled" : "Disabled", (unsigned)rc);
-            }
-            rc = bios->vtbl->SetIOAPICEnabled(bios, (def->features) &
-                                              (1 << VIR_DOMAIN_FEATURE_APIC));
-            if (NS_FAILED(rc)) {
-                vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                          "could not change APIC status to",
-                          ((def->features) & (1 << VIR_DOMAIN_FEATURE_APIC))
-                          ? "Enabled" : "Disabled", (unsigned)rc);
-            }
-            bios->vtbl->nsisupports.Release((nsISupports *)bios);
-        }
-
-        /* Register the machine before attaching other devices to it */
-        rc = data->vboxObj->vtbl->RegisterMachine(data->vboxObj, machine);
+        rc = bios->vtbl->SetIOAPICEnabled(bios, (def->features) &
+                                          (1 << VIR_DOMAIN_FEATURE_APIC));
         if (NS_FAILED(rc)) {
-            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                      "could not define a domain",(unsigned)rc);
-            goto cleanup;
+            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                      "could not change APIC status to",
+                      ((def->features) & (1 << VIR_DOMAIN_FEATURE_APIC))
+                      ? "Enabled" : "Disabled", (unsigned)rc);
         }
+        VBOX_RELEASE(bios);
+    }
 
-        /* Get the uuid of the machine, currently it is immutable
-         * object so open a session to it and get it back, so that
-         * you can make changes to the machine setting
-         */
-        machine->vtbl->GetId(machine, &mchiid);
-        data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, mchiid);
-        data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-
-        {   /* Started:Block to set the boot device order */
-            ISystemProperties *systemProperties = NULL;
-            PRUint32 maxBootPosition            = 0;
-            int i = 0;
+    /* Register the machine before attaching other devices to it */
+    rc = data->vboxObj->vtbl->RegisterMachine(data->vboxObj, machine);
+    if (NS_FAILED(rc)) {
+        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "could not define a domain",(unsigned)rc);
+        goto cleanup;
+    }
 
-            DEBUG("def->os.type             %s", def->os.type);
-            DEBUG("def->os.arch             %s", def->os.arch);
-            DEBUG("def->os.machine          %s", def->os.machine);
-            DEBUG("def->os.nBootDevs        %d", def->os.nBootDevs);
-            DEBUG("def->os.bootDevs[0]      %d", def->os.bootDevs[0]);
-            DEBUG("def->os.bootDevs[1]      %d", def->os.bootDevs[1]);
-            DEBUG("def->os.bootDevs[2]      %d", def->os.bootDevs[2]);
-            DEBUG("def->os.bootDevs[3]      %d", def->os.bootDevs[3]);
-            DEBUG("def->os.init             %s", def->os.init);
-            DEBUG("def->os.kernel           %s", def->os.kernel);
-            DEBUG("def->os.initrd           %s", def->os.initrd);
-            DEBUG("def->os.cmdline          %s", def->os.cmdline);
-            DEBUG("def->os.root             %s", def->os.root);
-            DEBUG("def->os.loader           %s", def->os.loader);
-            DEBUG("def->os.bootloader       %s", def->os.bootloader);
-            DEBUG("def->os.bootloaderArgs   %s", def->os.bootloaderArgs);
+    /* Get the uuid of the machine, currently it is immutable
+     * object so open a session to it and get it back, so that
+     * you can make changes to the machine setting
+     */
+    machine->vtbl->GetId(machine, &mchiid);
+    data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, mchiid);
+    data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
 
-            data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-            if (systemProperties) {
-                systemProperties->vtbl->GetMaxBootPosition(systemProperties, &maxBootPosition);
-                systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-                systemProperties = NULL;
-            }
+    {   /* Started:Block to set the boot device order */
+        ISystemProperties *systemProperties = NULL;
+        PRUint32 maxBootPosition            = 0;
+        int i = 0;
+
+        DEBUG("def->os.type             %s", def->os.type);
+        DEBUG("def->os.arch             %s", def->os.arch);
+        DEBUG("def->os.machine          %s", def->os.machine);
+        DEBUG("def->os.nBootDevs        %d", def->os.nBootDevs);
+        DEBUG("def->os.bootDevs[0]      %d", def->os.bootDevs[0]);
+        DEBUG("def->os.bootDevs[1]      %d", def->os.bootDevs[1]);
+        DEBUG("def->os.bootDevs[2]      %d", def->os.bootDevs[2]);
+        DEBUG("def->os.bootDevs[3]      %d", def->os.bootDevs[3]);
+        DEBUG("def->os.init             %s", def->os.init);
+        DEBUG("def->os.kernel           %s", def->os.kernel);
+        DEBUG("def->os.initrd           %s", def->os.initrd);
+        DEBUG("def->os.cmdline          %s", def->os.cmdline);
+        DEBUG("def->os.root             %s", def->os.root);
+        DEBUG("def->os.loader           %s", def->os.loader);
+        DEBUG("def->os.bootloader       %s", def->os.bootloader);
+        DEBUG("def->os.bootloaderArgs   %s", def->os.bootloaderArgs);
 
-            /* Clear the defaults first */
-            for (i = 0; i < maxBootPosition; i++) {
-                machine->vtbl->SetBootOrder(machine, i+1, DeviceType_Null);
-            }
+        data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+        if (systemProperties) {
+            systemProperties->vtbl->GetMaxBootPosition(systemProperties, &maxBootPosition);
+            VBOX_RELEASE(systemProperties);
+            systemProperties = NULL;
+        }
 
-            for (i = 0; (i < def->os.nBootDevs) && (i < maxBootPosition); i++) {
-                PRUint32 device = DeviceType_Null;
+        /* Clear the defaults first */
+        for (i = 0; i < maxBootPosition; i++) {
+            machine->vtbl->SetBootOrder(machine, i+1, DeviceType_Null);
+        }
 
-                if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_FLOPPY) {
-                    device = DeviceType_Floppy;
-                } else if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_CDROM) {
-                    device = DeviceType_DVD;
-                } else if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_DISK) {
-                    device = DeviceType_HardDisk;
-                } else if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_NET) {
-                    device = DeviceType_Network;
-                }
-                machine->vtbl->SetBootOrder(machine, i+1, device);
+        for (i = 0; (i < def->os.nBootDevs) && (i < maxBootPosition); i++) {
+            PRUint32 device = DeviceType_Null;
+
+            if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_FLOPPY) {
+                device = DeviceType_Floppy;
+            } else if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_CDROM) {
+                device = DeviceType_DVD;
+            } else if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_DISK) {
+                device = DeviceType_HardDisk;
+            } else if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_NET) {
+                device = DeviceType_Network;
             }
-        }   /* Finished:Block to set the boot device order */
-
-        {   /* Started:Block to attach the CDROM/DVD Drive and HardDisks to the VM */
-
-            if (def->ndisks > 0) {
-                int i;
-
-                for (i = 0; i < def->ndisks; i++) {
-                    DEBUG("disk(%d) type:       %d", i, def->disks[i]->type);
-                    DEBUG("disk(%d) device:     %d", i, def->disks[i]->device);
-                    DEBUG("disk(%d) bus:        %d", i, def->disks[i]->bus);
-                    DEBUG("disk(%d) src:        %s", i, def->disks[i]->src);
-                    DEBUG("disk(%d) dst:        %s", i, def->disks[i]->dst);
-                    DEBUG("disk(%d) driverName: %s", i, def->disks[i]->driverName);
-                    DEBUG("disk(%d) driverType: %s", i, def->disks[i]->driverType);
-                    DEBUG("disk(%d) cachemode:  %d", i, def->disks[i]->cachemode);
-                    DEBUG("disk(%d) readonly:   %s", i, def->disks[i]->readonly ? "True" : "False");
-                    DEBUG("disk(%d) shared:     %s", i, def->disks[i]->shared ? "True" : "False");
-
-                    if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
-                        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                            IDVDDrive *dvdDrive = NULL;
-                            /* Currently CDROM/DVD Drive is always IDE
-                             * Secondary Master so neglecting the following
-                             * parameters:
-                             *      def->disks[i]->bus
-                             *      def->disks[i]->dst
-                             */
+            machine->vtbl->SetBootOrder(machine, i+1, device);
+        }
+    }   /* Finished:Block to set the boot device order */
+
+#if VBOX_API_VERSION < 3001
+    {   /* Started:Block to attach the CDROM/DVD Drive and HardDisks to the VM */
+
+        if (def->ndisks > 0) {
+            int i;
+
+            for (i = 0; i < def->ndisks; i++) {
+                DEBUG("disk(%d) type:       %d", i, def->disks[i]->type);
+                DEBUG("disk(%d) device:     %d", i, def->disks[i]->device);
+                DEBUG("disk(%d) bus:        %d", i, def->disks[i]->bus);
+                DEBUG("disk(%d) src:        %s", i, def->disks[i]->src);
+                DEBUG("disk(%d) dst:        %s", i, def->disks[i]->dst);
+                DEBUG("disk(%d) driverName: %s", i, def->disks[i]->driverName);
+                DEBUG("disk(%d) driverType: %s", i, def->disks[i]->driverType);
+                DEBUG("disk(%d) cachemode:  %d", i, def->disks[i]->cachemode);
+                DEBUG("disk(%d) readonly:   %s", i, def->disks[i]->readonly ? "True" : "False");
+                DEBUG("disk(%d) shared:     %s", i, def->disks[i]->shared ? "True" : "False");
+
+                if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
+                    if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                        IDVDDrive *dvdDrive = NULL;
+                        /* Currently CDROM/DVD Drive is always IDE
+                         * Secondary Master so neglecting the following
+                         * parameters:
+                         *      def->disks[i]->bus
+                         *      def->disks[i]->dst
+                         */
 
-                            machine->vtbl->GetDVDDrive(machine, &dvdDrive);
-                            if (dvdDrive) {
-                                IDVDImage *dvdImage          = NULL;
-                                PRUnichar *dvdfileUtf16      = NULL;
-                                vboxIID *dvduuid             = NULL;
+                        machine->vtbl->GetDVDDrive(machine, &dvdDrive);
+                        if (dvdDrive) {
+                            IDVDImage *dvdImage          = NULL;
+                            PRUnichar *dvdfileUtf16      = NULL;
+                            vboxIID *dvduuid             = NULL;
 #if VBOX_API_VERSION == 2002
-                                nsID dvdemptyuuid;
+                            nsID dvdemptyuuid;
 
-                                memset(&dvdemptyuuid, 0, sizeof(dvdemptyuuid));
+                            memset(&dvdemptyuuid, 0, sizeof(dvdemptyuuid));
 #else
-                                PRUnichar *dvdemptyuuidUtf16 = NULL;
+                            PRUnichar *dvdemptyuuidUtf16 = NULL;
 #endif
 
-                                data->pFuncs->pfnUtf8ToUtf16(def->disks[i]->src, &dvdfileUtf16);
+                            VBOX_UTF8_TO_UTF16(def->disks[i]->src, &dvdfileUtf16);
 
-                                data->vboxObj->vtbl->FindDVDImage(data->vboxObj, dvdfileUtf16, &dvdImage);
-                                if (!dvdImage) {
+                            data->vboxObj->vtbl->FindDVDImage(data->vboxObj, dvdfileUtf16, &dvdImage);
+                            if (!dvdImage) {
 #if VBOX_API_VERSION == 2002
-                                    data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, &dvdemptyuuid, &dvdImage);
+                                data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, &dvdemptyuuid, &dvdImage);
 #else
-                                    data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, dvdemptyuuidUtf16, &dvdImage);
+                                data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, dvdemptyuuidUtf16, &dvdImage);
 #endif
-                                }
-                                if (dvdImage) {
-                                    rc = dvdImage->vtbl->imedium.GetId((IMedium *)dvdImage, &dvduuid);
+                            }
+                            if (dvdImage) {
+                                rc = dvdImage->vtbl->imedium.GetId((IMedium *)dvdImage, &dvduuid);
+                                if (NS_FAILED(rc)) {
+                                    vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                                              "can't get the uuid of the file to be attached to cdrom",
+                                              def->disks[i]->src, (unsigned)rc);
+                                } else {
+                                    rc = dvdDrive->vtbl->MountImage(dvdDrive, dvduuid);
                                     if (NS_FAILED(rc)) {
                                         vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                  "can't get the uuid of the file to be attached to cdrom",
+                                                  "could not attach the file to cdrom",
                                                   def->disks[i]->src, (unsigned)rc);
                                     } else {
-                                        rc = dvdDrive->vtbl->MountImage(dvdDrive, dvduuid);
-                                        if (NS_FAILED(rc)) {
-                                            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                      "could not attach the file to cdrom",
-                                                      def->disks[i]->src, (unsigned)rc);
-                                        } else {
-                                            DEBUGIID("CD/DVDImage UUID:", dvduuid);
-                                        }
+                                        DEBUGIID("CD/DVDImage UUID:", dvduuid);
                                     }
-
-                                    dvdImage->vtbl->imedium.nsisupports.Release((nsISupports *)dvdImage);
                                 }
-                                vboxIIDUnalloc(dvduuid);
-                                data->pFuncs->pfnUtf16Free(dvdfileUtf16);
-                                dvdDrive->vtbl->nsisupports.Release((nsISupports *)dvdDrive);
+
+                                VBOX_MEDIUM_RELEASE(dvdImage);
                             }
-                        } else if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                            vboxIIDUnalloc(dvduuid);
+                            VBOX_UTF16_FREE(dvdfileUtf16);
+                            VBOX_RELEASE(dvdDrive);
                         }
-                    } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
-                        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                            IHardDisk *hardDisk     = NULL;
-                            PRUnichar *hddfileUtf16 = NULL;
-                            vboxIID   *hdduuid      = NULL;
-                            PRUnichar *hddEmpty     = NULL;
-                            /* Current Limitation: Harddisk can't be connected to
-                             * Secondary Master as Secondary Master is always used
-                             * for CD/DVD Drive, so don't connect the harddisk if it
-                             * is requested to be connected to Secondary master
-                             */
+                    } else if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                    }
+                } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
+                    if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                        IHardDisk *hardDisk     = NULL;
+                        PRUnichar *hddfileUtf16 = NULL;
+                        vboxIID   *hdduuid      = NULL;
+                        PRUnichar *hddEmpty     = NULL;
+                        /* Current Limitation: Harddisk can't be connected to
+                         * Secondary Master as Secondary Master is always used
+                         * for CD/DVD Drive, so don't connect the harddisk if it
+                         * is requested to be connected to Secondary master
+                         */
 
-                            data->pFuncs->pfnUtf8ToUtf16(def->disks[i]->src, &hddfileUtf16);
-                            data->pFuncs->pfnUtf8ToUtf16("", &hddEmpty);
+                        VBOX_UTF8_TO_UTF16(def->disks[i]->src, &hddfileUtf16);
+                        VBOX_UTF8_TO_UTF16("", &hddEmpty);
 
-                            data->vboxObj->vtbl->FindHardDisk(data->vboxObj, hddfileUtf16, &hardDisk);
+                        data->vboxObj->vtbl->FindHardDisk(data->vboxObj, hddfileUtf16, &hardDisk);
 
-                            if (!hardDisk) {
+                        if (!hardDisk) {
 #if VBOX_API_VERSION == 2002
-                                data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
-                                                                  hddfileUtf16,
-                                                                  AccessMode_ReadWrite,
-                                                                  &hardDisk);
+                            data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
+                                                              hddfileUtf16,
+                                                              AccessMode_ReadWrite,
+                                                              &hardDisk);
 #else
-                                data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
-                                                                  hddfileUtf16,
-                                                                  AccessMode_ReadWrite,
-                                                                  0,
-                                                                  hddEmpty,
-                                                                  0,
-                                                                  hddEmpty,
-                                                                  &hardDisk);
+                            data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
+                                                              hddfileUtf16,
+                                                              AccessMode_ReadWrite,
+                                                              0,
+                                                              hddEmpty,
+                                                              0,
+                                                              hddEmpty,
+                                                              &hardDisk);
 #endif
-                            }
+                        }
 
-                            if (hardDisk) {
-                                rc = hardDisk->vtbl->imedium.GetId((IMedium *)hardDisk, &hdduuid);
-                                if (NS_FAILED(rc)) {
-                                    vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                              "can't get the uuid of the file to be attached as harddisk",
-                                              def->disks[i]->src, (unsigned)rc);
-                                } else {
-                                    if (def->disks[i]->readonly) {
-                                        hardDisk->vtbl->SetType(hardDisk, HardDiskType_Immutable);
-                                        DEBUG0("setting harddisk to readonly");
-                                    } else if (!def->disks[i]->readonly) {
-                                        hardDisk->vtbl->SetType(hardDisk, HardDiskType_Normal);
-                                        DEBUG0("setting harddisk type to normal");
-                                    }
-                                    if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_IDE) {
-                                        if (STREQ(def->disks[i]->dst, "hdc")) {
-                                            DEBUG0("Not connecting harddisk to hdc as hdc"
-                                                   " is taken by CD/DVD Drive");
-                                        } else {
-                                            PRInt32 channel          = 0;
-                                            PRInt32 device           = 0;
-                                            PRUnichar *hddcnameUtf16 = NULL;
-
-                                            char *hddcname = strdup("IDE");
-                                            data->pFuncs->pfnUtf8ToUtf16(hddcname, &hddcnameUtf16);
-                                            VIR_FREE(hddcname);
-
-                                            if (STREQ(def->disks[i]->dst, "hda")) {
-                                                channel = 0;
-                                                device  = 0;
-                                            } else if (STREQ(def->disks[i]->dst, "hdb")) {
-                                                channel = 0;
-                                                device  = 1;
-                                            } else if (STREQ(def->disks[i]->dst, "hdd")) {
-                                                channel = 1;
-                                                device  = 1;
-                                            }
+                        if (hardDisk) {
+                            rc = hardDisk->vtbl->imedium.GetId((IMedium *)hardDisk, &hdduuid);
+                            if (NS_FAILED(rc)) {
+                                vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                                          "can't get the uuid of the file to be attached as harddisk",
+                                          def->disks[i]->src, (unsigned)rc);
+                            } else {
+                                if (def->disks[i]->readonly) {
+                                    hardDisk->vtbl->SetType(hardDisk, HardDiskType_Immutable);
+                                    DEBUG0("setting harddisk to readonly");
+                                } else if (!def->disks[i]->readonly) {
+                                    hardDisk->vtbl->SetType(hardDisk, HardDiskType_Normal);
+                                    DEBUG0("setting harddisk type to normal");
+                                }
+                                if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_IDE) {
+                                    if (STREQ(def->disks[i]->dst, "hdc")) {
+                                        DEBUG0("Not connecting harddisk to hdc as hdc"
+                                               " is taken by CD/DVD Drive");
+                                    } else {
+                                        PRInt32 channel          = 0;
+                                        PRInt32 device           = 0;
+                                        PRUnichar *hddcnameUtf16 = NULL;
+
+                                        char *hddcname = strdup("IDE");
+                                        VBOX_UTF8_TO_UTF16(hddcname, &hddcnameUtf16);
+                                        VIR_FREE(hddcname);
+
+                                        if (STREQ(def->disks[i]->dst, "hda")) {
+                                            channel = 0;
+                                            device  = 0;
+                                        } else if (STREQ(def->disks[i]->dst, "hdb")) {
+                                            channel = 0;
+                                            device  = 1;
+                                        } else if (STREQ(def->disks[i]->dst, "hdd")) {
+                                            channel = 1;
+                                            device  = 1;
+                                        }
 
-                                            rc = machine->vtbl->AttachHardDisk(machine,
-                                                                               hdduuid,
-                                                                               hddcnameUtf16,
-                                                                               channel,
-                                                                               device);
-                                            data->pFuncs->pfnUtf16Free(hddcnameUtf16);
+                                        rc = machine->vtbl->AttachHardDisk(machine,
+                                                                           hdduuid,
+                                                                           hddcnameUtf16,
+                                                                           channel,
+                                                                           device);
+                                        VBOX_UTF16_FREE(hddcnameUtf16);
 
-                                            if (NS_FAILED(rc)) {
-                                                vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                          "could not attach the file as harddisk",
-                                                          def->disks[i]->src, (unsigned)rc);
-                                            } else {
-                                                DEBUGIID("Attached HDD with UUID", hdduuid);
-                                            }
+                                        if (NS_FAILED(rc)) {
+                                            vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                                                      "could not attach the file as harddisk",
+                                                      def->disks[i]->src, (unsigned)rc);
+                                        } else {
+                                            DEBUGIID("Attached HDD with UUID", hdduuid);
                                         }
                                     }
                                 }
-                                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
                             }
-                            vboxIIDUnalloc(hdduuid);
-                            data->pFuncs->pfnUtf16Free(hddEmpty);
-                            data->pFuncs->pfnUtf16Free(hddfileUtf16);
-                        } else if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                            VBOX_MEDIUM_RELEASE(hardDisk);
                         }
-                    } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
-                        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                            IFloppyDrive *floppyDrive;
-                            machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
-                            if (floppyDrive) {
-                                rc = floppyDrive->vtbl->SetEnabled(floppyDrive, 1);
-                                if (NS_SUCCEEDED(rc)) {
-                                    IFloppyImage *floppyImage   = NULL;
-                                    PRUnichar *fdfileUtf16      = NULL;
-                                    vboxIID *fduuid             = NULL;
+                        vboxIIDUnalloc(hdduuid);
+                        VBOX_UTF16_FREE(hddEmpty);
+                        VBOX_UTF16_FREE(hddfileUtf16);
+                    } else if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                    }
+                } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
+                    if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                        IFloppyDrive *floppyDrive;
+                        machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
+                        if (floppyDrive) {
+                            rc = floppyDrive->vtbl->SetEnabled(floppyDrive, 1);
+                            if (NS_SUCCEEDED(rc)) {
+                                IFloppyImage *floppyImage   = NULL;
+                                PRUnichar *fdfileUtf16      = NULL;
+                                vboxIID *fduuid             = NULL;
 #if VBOX_API_VERSION == 2002
-                                    nsID fdemptyuuid;
+                                nsID fdemptyuuid;
 
-                                    memset(&fdemptyuuid, 0, sizeof(fdemptyuuid));
+                                memset(&fdemptyuuid, 0, sizeof(fdemptyuuid));
 #else
-                                    PRUnichar *fdemptyuuidUtf16 = NULL;
+                                PRUnichar *fdemptyuuidUtf16 = NULL;
 #endif
 
-                                    data->pFuncs->pfnUtf8ToUtf16(def->disks[i]->src, &fdfileUtf16);
-                                    rc = data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
-                                                                              fdfileUtf16,
-                                                                              &floppyImage);
+                                VBOX_UTF8_TO_UTF16(def->disks[i]->src, &fdfileUtf16);
+                                rc = data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
+                                                                          fdfileUtf16,
+                                                                          &floppyImage);
 
-                                    if (!floppyImage) {
-                                        data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
-                                                                             fdfileUtf16,
+                                if (!floppyImage) {
+                                    data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
+                                                                         fdfileUtf16,
 #if VBOX_API_VERSION == 2002
-                                                                             &fdemptyuuid,
+                                                                         &fdemptyuuid,
 #else
-                                                                             fdemptyuuidUtf16,
+                                                                         fdemptyuuidUtf16,
 #endif
-                                                                             &floppyImage);
-                                    }
+                                                                         &floppyImage);
+                                }
 
-                                    if (floppyImage) {
-                                        rc = floppyImage->vtbl->imedium.GetId((IMedium *)floppyImage, &fduuid);
+                                if (floppyImage) {
+                                    rc = floppyImage->vtbl->imedium.GetId((IMedium *)floppyImage, &fduuid);
+                                    if (NS_FAILED(rc)) {
+                                        vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                                                  "can't get the uuid of the file to be attached to floppy drive",
+                                                  def->disks[i]->src, (unsigned)rc);
+                                    } else {
+                                        rc = floppyDrive->vtbl->MountImage(floppyDrive, fduuid);
                                         if (NS_FAILED(rc)) {
                                             vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                      "can't get the uuid of the file to be attached to floppy drive",
+                                                      "could not attach the file to floppy drive",
                                                       def->disks[i]->src, (unsigned)rc);
                                         } else {
-                                            rc = floppyDrive->vtbl->MountImage(floppyDrive, fduuid);
-                                            if (NS_FAILED(rc)) {
-                                                vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                          "could not attach the file to floppy drive",
-                                                          def->disks[i]->src, (unsigned)rc);
-                                            } else {
-                                                DEBUGIID("floppyImage UUID", fduuid);
-                                            }
+                                            DEBUGIID("floppyImage UUID", fduuid);
                                         }
-                                        floppyImage->vtbl->imedium.nsisupports.Release((nsISupports *)floppyImage);
                                     }
-                                    vboxIIDUnalloc(fduuid);
-                                    data->pFuncs->pfnUtf16Free(fdfileUtf16);
+                                    VBOX_MEDIUM_RELEASE(floppyImage);
                                 }
-                                floppyDrive->vtbl->nsisupports.Release((nsISupports *)floppyDrive);
+                                vboxIIDUnalloc(fduuid);
+                                VBOX_UTF16_FREE(fdfileUtf16);
                             }
-                        } else if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                            VBOX_RELEASE(floppyDrive);
                         }
+                    } else if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
                     }
                 }
             }
+        }
 
-        }   /* Finished:Block to attach the CDROM/DVD Drive and HardDisks to the VM */
+    }   /* Finished:Block to attach the CDROM/DVD Drive and HardDisks to the VM */
 
-        {   /* Started:Block to attach the Sound Controller to the VM */
-            /* Check if def->nsounds is one as VirtualBox currently supports
-             * only one sound card
-             */
-            if (def->nsounds == 1) {
-                IAudioAdapter *audioAdapter = NULL;
+#else  /* VBOX_API_VERSION >= 3001 */
+    {
+        PRUint32 maxPortPerInst[StorageBus_Floppy + 1] = {};
+        PRUint32 maxSlotPerPort[StorageBus_Floppy + 1] = {};
+        PRUnichar *storageCtlName = NULL;
+        bool error = false;
+        int i = 0;
 
-                machine->vtbl->GetAudioAdapter(machine, &audioAdapter);
-                if (audioAdapter) {
-                    rc = audioAdapter->vtbl->SetEnabled(audioAdapter, 1);
-                    if (NS_SUCCEEDED(rc)) {
-                        if (def->sounds[0]->model == VIR_DOMAIN_SOUND_MODEL_SB16) {
-                            audioAdapter->vtbl->SetAudioController(audioAdapter, AudioControllerType_SB16);
-                        } else if (def->sounds[0]->model == VIR_DOMAIN_SOUND_MODEL_AC97) {
-                            audioAdapter->vtbl->SetAudioController(audioAdapter, AudioControllerType_AC97);
-                        }
+        /* get the max port/slots/etc for the given storage bus */
+        error = !vboxGetMaxPortSlotValues(data->vboxObj, maxPortPerInst, maxSlotPerPort);
+
+        /* add a storage controller for the mediums to be attached */
+        /* this needs to change when multiple controller are supported for ver > 3.1 */
+        {
+            IStorageController *storageCtl = NULL;
+            PRUnichar *sName = NULL;
+
+            VBOX_UTF8_TO_UTF16("IDE Controller", &sName);
+            machine->vtbl->AddStorageController(machine,
+                                                sName,
+                                                StorageBus_IDE,
+                                                &storageCtl);
+            VBOX_UTF16_FREE(sName);
+            VBOX_RELEASE(storageCtl);
+
+            VBOX_UTF8_TO_UTF16("SATA Controller", &sName);
+            machine->vtbl->AddStorageController(machine,
+                                                sName,
+                                                StorageBus_SATA,
+                                                &storageCtl);
+            VBOX_UTF16_FREE(sName);
+            VBOX_RELEASE(storageCtl);
+
+            VBOX_UTF8_TO_UTF16("SCSI Controller", &sName);
+            machine->vtbl->AddStorageController(machine,
+                                                sName,
+                                                StorageBus_SCSI,
+                                                &storageCtl);
+            VBOX_UTF16_FREE(sName);
+            VBOX_RELEASE(storageCtl);
+
+            VBOX_UTF8_TO_UTF16("Floppy Controller", &sName);
+            machine->vtbl->AddStorageController(machine,
+                                                sName,
+                                                StorageBus_Floppy,
+                                                &storageCtl);
+            VBOX_UTF16_FREE(sName);
+            VBOX_RELEASE(storageCtl);
+        }
+
+        /* Started:Block to attach the CDROM/DVD Drive and HardDisks to the VM */
+        for (i = 0; i < def->ndisks && !error; i++) {
+            DEBUG("disk(%d) type:       %d", i, def->disks[i]->type);
+            DEBUG("disk(%d) device:     %d", i, def->disks[i]->device);
+            DEBUG("disk(%d) bus:        %d", i, def->disks[i]->bus);
+            DEBUG("disk(%d) src:        %s", i, def->disks[i]->src);
+            DEBUG("disk(%d) dst:        %s", i, def->disks[i]->dst);
+            DEBUG("disk(%d) driverName: %s", i, def->disks[i]->driverName);
+            DEBUG("disk(%d) driverType: %s", i, def->disks[i]->driverType);
+            DEBUG("disk(%d) cachemode:  %d", i, def->disks[i]->cachemode);
+            DEBUG("disk(%d) readonly:   %s", i, def->disks[i]->readonly ? "True" : "False");
+            DEBUG("disk(%d) shared:     %s", i, def->disks[i]->shared ? "True" : "False");
+
+            if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                IMedium   *medium          = NULL;
+                PRUnichar *mediumUUID      = NULL;
+                PRUnichar *mediumFileUtf16 = NULL;
+                PRUint32   storageBus      = StorageBus_Null;
+                PRUint32   deviceType      = DeviceType_Null;
+                PRInt32    deviceInst      = 0;
+                PRInt32    devicePort      = 0;
+                PRInt32    deviceSlot      = 0;
+
+                VBOX_UTF8_TO_UTF16(def->disks[i]->src, &mediumFileUtf16);
+
+                if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
+                    deviceType = DeviceType_HardDisk;
+                    data->vboxObj->vtbl->FindHardDisk(data->vboxObj, mediumFileUtf16, &medium);
+                } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
+                    deviceType = DeviceType_DVD;
+                    data->vboxObj->vtbl->FindDVDImage(data->vboxObj, mediumFileUtf16, &medium);
+                } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
+                    deviceType = DeviceType_Floppy;
+                    data->vboxObj->vtbl->FindFloppyImage(data->vboxObj, mediumFileUtf16, &medium);
+                } else {
+                    VBOX_UTF16_FREE(mediumFileUtf16);
+                    continue;
+                }
+
+                if (!medium) {
+                    PRUnichar *mediumEmpty = NULL;
+
+                    VBOX_UTF8_TO_UTF16("", &mediumEmpty);
+
+                    if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
+                        data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
+                                                          mediumFileUtf16,
+                                                          AccessMode_ReadWrite,
+                                                          false,
+                                                          mediumEmpty,
+                                                          false,
+                                                          mediumEmpty,
+                                                          &medium);
+                    } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
+                        data->vboxObj->vtbl->OpenDVDImage(data->vboxObj,
+                                                          mediumFileUtf16,
+                                                          mediumEmpty,
+                                                          &medium);
+                    } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
+                        data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
+                                                             mediumFileUtf16,
+                                                             mediumEmpty,
+                                                             &medium);
                     }
-                    audioAdapter->vtbl->nsisupports.Release((nsISupports *)audioAdapter);
+
+                    VBOX_UTF16_FREE(mediumEmpty);
                 }
-            }
-        }   /* Finished:Block to attach the Sound Controller to the VM */
 
-        {   /* Started:Block to attach the Network Card to the VM */
-            ISystemProperties *systemProperties = NULL;
-            PRUint32 networkAdapterCount        = 0;
-            int i = 0;
+                if (!medium) {
+                    vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                              "Failed to attach the following disk/dvd/floppy to the machine",
+                              def->disks[i]->src, (unsigned)rc);
+                    VBOX_UTF16_FREE(mediumFileUtf16);
+                    continue;
+                }
 
-            data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-            if (systemProperties) {
-                systemProperties->vtbl->GetNetworkAdapterCount(systemProperties, &networkAdapterCount);
-                systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-                systemProperties = NULL;
+                rc = medium->vtbl->GetId(medium, &mediumUUID);
+                if (NS_FAILED(rc)) {
+                    vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                              "can't get the uuid of the file to be attached as harddisk/dvd/floppy",
+                              def->disks[i]->src, (unsigned)rc);
+                    VBOX_RELEASE(medium);
+                    VBOX_UTF16_FREE(mediumFileUtf16);
+                    continue;
+                }
+
+                if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
+                    if (def->disks[i]->readonly) {
+                        medium->vtbl->SetType(medium, MediumType_Immutable);
+                        DEBUG0("setting harddisk to immutable");
+                    } else if (!def->disks[i]->readonly) {
+                        medium->vtbl->SetType(medium, MediumType_Normal);
+                        DEBUG0("setting harddisk type to normal");
+                    }
+                }
+
+                if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_IDE) {
+                    VBOX_UTF8_TO_UTF16("IDE Controller", &storageCtlName);
+                    storageBus = StorageBus_IDE;
+                } else if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_SATA) {
+                    VBOX_UTF8_TO_UTF16("SATA Controller", &storageCtlName);
+                    storageBus = StorageBus_SATA;
+                } else if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
+                    VBOX_UTF8_TO_UTF16("SCSI Controller", &storageCtlName);
+                    storageBus = StorageBus_SCSI;
+                } else if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_FDC) {
+                    VBOX_UTF8_TO_UTF16("Floppy Controller", &storageCtlName);
+                    storageBus = StorageBus_Floppy;
+                }
+
+                /* get the device details i.e instance, port and slot */
+                if (!vboxGetDeviceDetails(def->disks[i]->dst,
+                                          maxPortPerInst,
+                                          maxSlotPerPort,
+                                          storageBus,
+                                          &deviceInst,
+                                          &devicePort,
+                                          &deviceSlot)) {
+                    vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                              "can't get the port/slot number of harddisk/dvd/floppy to be attached",
+                              def->disks[i]->src, (unsigned)rc);
+                    VBOX_RELEASE(medium);
+                    VBOX_UTF16_FREE(mediumUUID);
+                    VBOX_UTF16_FREE(mediumFileUtf16);
+                    continue;
+                }
+
+                /* attach the harddisk/dvd/Floppy to the storage controller */
+                rc = machine->vtbl->AttachDevice(machine,
+                                                 storageCtlName,
+                                                 devicePort,
+                                                 deviceSlot,
+                                                 deviceType,
+                                                 mediumUUID);
+
+                if (NS_FAILED(rc)) {
+                    vboxError(conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                              "could not attach the file as harddisk/dvd/floppy",
+                              def->disks[i]->src, (unsigned)rc);
+                } else {
+                    DEBUGIID("Attached HDD/DVD/Floppy with UUID", mediumUUID);
+                }
+
+                VBOX_RELEASE(medium);
+                VBOX_UTF16_FREE(mediumUUID);
+                VBOX_UTF16_FREE(mediumFileUtf16);
+                VBOX_UTF16_FREE(storageCtlName);
             }
+        }
+    }
+    /* Finished:Block to attach the CDROM/DVD Drive and HardDisks to the VM */
+#endif /* VBOX_API_VERSION >= 3001 */
 
-            DEBUG("Number of Network Cards to be connected: %d", def->nnets);
-            DEBUG("Number of Network Cards available: %d", networkAdapterCount);
+    {   /* Started:Block to attach the Sound Controller to the VM */
+        /* Check if def->nsounds is one as VirtualBox currently supports
+         * only one sound card
+         */
+        if (def->nsounds == 1) {
+            IAudioAdapter *audioAdapter = NULL;
 
-            for (i = 0; (i < def->nnets) && (i < networkAdapterCount); i++) {
-                INetworkAdapter *adapter = NULL;
-                PRUint32 adapterType     = NetworkAdapterType_Null;
-                char macaddr[VIR_MAC_STRING_BUFLEN] = {0};
-                char macaddrvbox[VIR_MAC_STRING_BUFLEN - 5] = {0};
-
-                virFormatMacAddr(def->nets[i]->mac, macaddr);
-                snprintf(macaddrvbox, VIR_MAC_STRING_BUFLEN - 5,
-                         "%02X%02X%02X%02X%02X%02X",
-                         def->nets[i]->mac[0],
-                         def->nets[i]->mac[1],
-                         def->nets[i]->mac[2],
-                         def->nets[i]->mac[3],
-                         def->nets[i]->mac[4],
-                         def->nets[i]->mac[5]);
-                macaddrvbox[VIR_MAC_STRING_BUFLEN - 6] = '\0';
-
-                DEBUG("NIC(%d): Type:   %d", i, def->nets[i]->type);
-                DEBUG("NIC(%d): Model:  %s", i, def->nets[i]->model);
-                DEBUG("NIC(%d): Mac:    %s", i, macaddr);
-                DEBUG("NIC(%d): ifname: %s", i, def->nets[i]->ifname);
-                if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
-                    DEBUG("NIC(%d): name:    %s", i, def->nets[i]->data.network.name);
-                } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_INTERNAL) {
-                    DEBUG("NIC(%d): name:   %s", i, def->nets[i]->data.internal.name);
-                } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_USER) {
-                    DEBUG("NIC(%d): NAT.", i);
-                } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
-                    DEBUG("NIC(%d): brname: %s", i, def->nets[i]->data.bridge.brname);
-                    DEBUG("NIC(%d): script: %s", i, def->nets[i]->data.bridge.script);
-                    DEBUG("NIC(%d): ipaddr: %s", i, def->nets[i]->data.bridge.ipaddr);
+            machine->vtbl->GetAudioAdapter(machine, &audioAdapter);
+            if (audioAdapter) {
+                rc = audioAdapter->vtbl->SetEnabled(audioAdapter, 1);
+                if (NS_SUCCEEDED(rc)) {
+                    if (def->sounds[0]->model == VIR_DOMAIN_SOUND_MODEL_SB16) {
+                        audioAdapter->vtbl->SetAudioController(audioAdapter, AudioControllerType_SB16);
+                    } else if (def->sounds[0]->model == VIR_DOMAIN_SOUND_MODEL_AC97) {
+                        audioAdapter->vtbl->SetAudioController(audioAdapter, AudioControllerType_AC97);
+                    }
                 }
+                VBOX_RELEASE(audioAdapter);
+            }
+        }
+    }   /* Finished:Block to attach the Sound Controller to the VM */
 
-                machine->vtbl->GetNetworkAdapter(machine, i, &adapter);
-                if (adapter) {
-                    PRUnichar *MACAddress = NULL;
+    {   /* Started:Block to attach the Network Card to the VM */
+        ISystemProperties *systemProperties = NULL;
+        PRUint32 networkAdapterCount        = 0;
+        int i = 0;
+
+        data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+        if (systemProperties) {
+            systemProperties->vtbl->GetNetworkAdapterCount(systemProperties, &networkAdapterCount);
+            VBOX_RELEASE(systemProperties);
+            systemProperties = NULL;
+        }
+
+        DEBUG("Number of Network Cards to be connected: %d", def->nnets);
+        DEBUG("Number of Network Cards available: %d", networkAdapterCount);
+
+        for (i = 0; (i < def->nnets) && (i < networkAdapterCount); i++) {
+            INetworkAdapter *adapter = NULL;
+            PRUint32 adapterType     = NetworkAdapterType_Null;
+            char macaddr[VIR_MAC_STRING_BUFLEN] = {0};
+            char macaddrvbox[VIR_MAC_STRING_BUFLEN - 5] = {0};
+
+            virFormatMacAddr(def->nets[i]->mac, macaddr);
+            snprintf(macaddrvbox, VIR_MAC_STRING_BUFLEN - 5,
+                     "%02X%02X%02X%02X%02X%02X",
+                     def->nets[i]->mac[0],
+                     def->nets[i]->mac[1],
+                     def->nets[i]->mac[2],
+                     def->nets[i]->mac[3],
+                     def->nets[i]->mac[4],
+                     def->nets[i]->mac[5]);
+            macaddrvbox[VIR_MAC_STRING_BUFLEN - 6] = '\0';
+
+            DEBUG("NIC(%d): Type:   %d", i, def->nets[i]->type);
+            DEBUG("NIC(%d): Model:  %s", i, def->nets[i]->model);
+            DEBUG("NIC(%d): Mac:    %s", i, macaddr);
+            DEBUG("NIC(%d): ifname: %s", i, def->nets[i]->ifname);
+            if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
+                DEBUG("NIC(%d): name:    %s", i, def->nets[i]->data.network.name);
+            } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_INTERNAL) {
+                DEBUG("NIC(%d): name:   %s", i, def->nets[i]->data.internal.name);
+            } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_USER) {
+                DEBUG("NIC(%d): NAT.", i);
+            } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
+                DEBUG("NIC(%d): brname: %s", i, def->nets[i]->data.bridge.brname);
+                DEBUG("NIC(%d): script: %s", i, def->nets[i]->data.bridge.script);
+                DEBUG("NIC(%d): ipaddr: %s", i, def->nets[i]->data.bridge.ipaddr);
+            }
+
+            machine->vtbl->GetNetworkAdapter(machine, i, &adapter);
+            if (adapter) {
+                PRUnichar *MACAddress = NULL;
 
-                    adapter->vtbl->SetEnabled(adapter, 1);
+                adapter->vtbl->SetEnabled(adapter, 1);
 
+                if (def->nets[i]->model) {
                     if (STRCASEEQ(def->nets[i]->model , "Am79C970A")) {
                         adapterType = NetworkAdapterType_Am79C970A;
                     } else if (STRCASEEQ(def->nets[i]->model , "Am79C973")) {
@@ -3519,495 +4056,504 @@ static virDomainPtr vboxDomainDefineXML(virConnectPtr conn, const char *xml) {
                         adapterType = NetworkAdapterType_I82545EM;
                     } else if (STRCASEEQ(def->nets[i]->model , "82543GC")) {
                         adapterType = NetworkAdapterType_I82543GC;
+#if VBOX_API_VERSION >= 3001
+                    } else if (STRCASEEQ(def->nets[i]->model , "virtio")) {
+                        adapterType = NetworkAdapterType_Virtio;
+#endif /* VBOX_API_VERSION >= 3001 */
                     }
+                } else {
+                    adapterType = NetworkAdapterType_Am79C973;
+                }
 
-                    adapter->vtbl->SetAdapterType(adapter, adapterType);
-
-                    if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
-                        PRUnichar *hostInterface = NULL;
-                        /* Bridged Network */
+                adapter->vtbl->SetAdapterType(adapter, adapterType);
 
-                        adapter->vtbl->AttachToBridgedInterface(adapter);
+                if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
+                    PRUnichar *hostInterface = NULL;
+                    /* Bridged Network */
 
-                        if (def->nets[i]->data.bridge.brname) {
-                            data->pFuncs->pfnUtf8ToUtf16(def->nets[i]->data.bridge.brname, &hostInterface);
-                            adapter->vtbl->SetHostInterface(adapter, hostInterface);
-                            data->pFuncs->pfnUtf16Free(hostInterface);
-                        }
-                    } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_INTERNAL) {
-                        PRUnichar *internalNetwork = NULL;
-                        /* Internal Network */
+                    adapter->vtbl->AttachToBridgedInterface(adapter);
 
-                        adapter->vtbl->AttachToInternalNetwork(adapter);
+                    if (def->nets[i]->data.bridge.brname) {
+                        VBOX_UTF8_TO_UTF16(def->nets[i]->data.bridge.brname, &hostInterface);
+                        adapter->vtbl->SetHostInterface(adapter, hostInterface);
+                        VBOX_UTF16_FREE(hostInterface);
+                    }
+                } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_INTERNAL) {
+                    PRUnichar *internalNetwork = NULL;
+                    /* Internal Network */
 
-                        if (def->nets[i]->data.internal.name) {
-                            data->pFuncs->pfnUtf8ToUtf16(def->nets[i]->data.internal.name, &internalNetwork);
-                            adapter->vtbl->SetInternalNetwork(adapter, internalNetwork);
-                            data->pFuncs->pfnUtf16Free(internalNetwork);
-                        }
-                    } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
-                        PRUnichar *hostInterface = NULL;
-                        /* Host Only Networking (currently only vboxnet0 available
-                         * on *nix and mac, on windows you can create and configure
-                         * as many as you want)
-                         */
-                        adapter->vtbl->AttachToHostOnlyInterface(adapter);
+                    adapter->vtbl->AttachToInternalNetwork(adapter);
 
-                        if (def->nets[i]->data.network.name) {
-                            data->pFuncs->pfnUtf8ToUtf16(def->nets[i]->data.network.name, &hostInterface);
-                            adapter->vtbl->SetHostInterface(adapter, hostInterface);
-                            data->pFuncs->pfnUtf16Free(hostInterface);
-                        }
-                    } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_USER) {
-                        /* NAT */
-                        adapter->vtbl->AttachToNAT(adapter);
-                    } else {
-                        /* else always default to NAT if we don't understand
-                         * what option is been passed to us
-                         */
-                        adapter->vtbl->AttachToNAT(adapter);
+                    if (def->nets[i]->data.internal.name) {
+                        VBOX_UTF8_TO_UTF16(def->nets[i]->data.internal.name, &internalNetwork);
+                        adapter->vtbl->SetInternalNetwork(adapter, internalNetwork);
+                        VBOX_UTF16_FREE(internalNetwork);
                     }
+                } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
+                    PRUnichar *hostInterface = NULL;
+                    /* Host Only Networking (currently only vboxnet0 available
+                     * on *nix and mac, on windows you can create and configure
+                     * as many as you want)
+                     */
+                    adapter->vtbl->AttachToHostOnlyInterface(adapter);
 
-                    data->pFuncs->pfnUtf8ToUtf16(macaddrvbox, &MACAddress);
-                    if (def->nets[i]->mac) {
-                        adapter->vtbl->SetMACAddress(adapter, MACAddress);
+                    if (def->nets[i]->data.network.name) {
+                        VBOX_UTF8_TO_UTF16(def->nets[i]->data.network.name, &hostInterface);
+                        adapter->vtbl->SetHostInterface(adapter, hostInterface);
+                        VBOX_UTF16_FREE(hostInterface);
                     }
-                    data->pFuncs->pfnUtf16Free(MACAddress);
+                } else if (def->nets[i]->type == VIR_DOMAIN_NET_TYPE_USER) {
+                    /* NAT */
+                    adapter->vtbl->AttachToNAT(adapter);
+                } else {
+                    /* else always default to NAT if we don't understand
+                     * what option is been passed to us
+                     */
+                    adapter->vtbl->AttachToNAT(adapter);
                 }
+
+                VBOX_UTF8_TO_UTF16(macaddrvbox, &MACAddress);
+                if (def->nets[i]->mac) {
+                    adapter->vtbl->SetMACAddress(adapter, MACAddress);
+                }
+                VBOX_UTF16_FREE(MACAddress);
             }
-        }   /* Finished:Block to attach the Network Card to the VM */
+        }
+    }   /* Finished:Block to attach the Network Card to the VM */
 
-        {   /* Started:Block to attach the Serial Port to the VM */
-            ISystemProperties *systemProperties = NULL;
-            PRUint32 serialPortCount            = 0;
-            int i = 0;
+    {   /* Started:Block to attach the Serial Port to the VM */
+        ISystemProperties *systemProperties = NULL;
+        PRUint32 serialPortCount            = 0;
+        int i = 0;
 
-            data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-            if (systemProperties) {
-                systemProperties->vtbl->GetSerialPortCount(systemProperties, &serialPortCount);
-                systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-                systemProperties = NULL;
-            }
+        data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+        if (systemProperties) {
+            systemProperties->vtbl->GetSerialPortCount(systemProperties, &serialPortCount);
+            VBOX_RELEASE(systemProperties);
+            systemProperties = NULL;
+        }
 
-            DEBUG("Number of Serial Ports to be connected: %d", def->nserials);
-            DEBUG("Number of Serial Ports available: %d", serialPortCount);
-            for (i = 0; (i < def->nserials) && (i < serialPortCount); i++) {
-                ISerialPort *serialPort = NULL;
+        DEBUG("Number of Serial Ports to be connected: %d", def->nserials);
+        DEBUG("Number of Serial Ports available: %d", serialPortCount);
+        for (i = 0; (i < def->nserials) && (i < serialPortCount); i++) {
+            ISerialPort *serialPort = NULL;
 
-                DEBUG("SerialPort(%d): Type: %d", i, def->serials[i]->type);
-                DEBUG("SerialPort(%d): target.port: %d", i, def->serials[i]->target.port);
+            DEBUG("SerialPort(%d): Type: %d", i, def->serials[i]->type);
+            DEBUG("SerialPort(%d): target.port: %d", i, def->serials[i]->target.port);
 
-                machine->vtbl->GetSerialPort(machine, i, &serialPort);
-                if (serialPort) {
-                    PRUnichar *pathUtf16 = NULL;
+            machine->vtbl->GetSerialPort(machine, i, &serialPort);
+            if (serialPort) {
+                PRUnichar *pathUtf16 = NULL;
 
-                    serialPort->vtbl->SetEnabled(serialPort, 1);
-                    data->pFuncs->pfnUtf8ToUtf16(def->serials[i]->data.file.path, &pathUtf16);
+                serialPort->vtbl->SetEnabled(serialPort, 1);
+                VBOX_UTF8_TO_UTF16(def->serials[i]->data.file.path, &pathUtf16);
 
-                    /* For now hard code the serial ports to COM1 and COM2,
-                     * COM1 (Base Addr: 0x3F8 (decimal: 1016), IRQ: 4)
-                     * COM2 (Base Addr: 0x2F8 (decimal:  760), IRQ: 3)
-                     * TODO: make this more flexible
-                     */
-                    /* TODO: to improve the libvirt XMl handling so
-                     * that def->serials[i]->target.port shows real port
-                     * and not always start at 0
-                     */
-                    if (def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_DEV) {
-                        serialPort->vtbl->SetPath(serialPort, pathUtf16);
-                        if (def->serials[i]->target.port == 0) {
-                            serialPort->vtbl->SetIRQ(serialPort, 4);
-                            serialPort->vtbl->SetIOBase(serialPort, 1016);
-                            DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
-                                  i, 4, 1016, def->serials[i]->data.file.path);
-                        } else if (def->serials[i]->target.port == 1) {
-                            serialPort->vtbl->SetIRQ(serialPort, 3);
-                            serialPort->vtbl->SetIOBase(serialPort, 760);
-                            DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
-                                  i, 3, 760, def->serials[i]->data.file.path);
-                        }
-                        serialPort->vtbl->SetHostMode(serialPort, PortMode_HostDevice);
-                    } else if (def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_PIPE) {
-                        serialPort->vtbl->SetPath(serialPort, pathUtf16);
-                        if (def->serials[i]->target.port == 0) {
-                            serialPort->vtbl->SetIRQ(serialPort, 4);
-                            serialPort->vtbl->SetIOBase(serialPort, 1016);
-                            DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
-                                  i, 4, 1016, def->serials[i]->data.file.path);
-                        } else if (def->serials[i]->target.port == 1) {
-                            serialPort->vtbl->SetIRQ(serialPort, 3);
-                            serialPort->vtbl->SetIOBase(serialPort, 760);
-                            DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
-                                  i, 3, 760, def->serials[i]->data.file.path);
-                        }
-                        if (!virFileExists(def->serials[i]->data.file.path)) {
-                            serialPort->vtbl->SetServer(serialPort, 1);
-                        }
-                        serialPort->vtbl->SetHostMode(serialPort, PortMode_HostPipe);
-                    } else if (def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_NULL) {
-                        serialPort->vtbl->SetHostMode(serialPort, PortMode_Disconnected);
+                /* For now hard code the serial ports to COM1 and COM2,
+                 * COM1 (Base Addr: 0x3F8 (decimal: 1016), IRQ: 4)
+                 * COM2 (Base Addr: 0x2F8 (decimal:  760), IRQ: 3)
+                 * TODO: make this more flexible
+                 */
+                /* TODO: to improve the libvirt XMl handling so
+                 * that def->serials[i]->target.port shows real port
+                 * and not always start at 0
+                 */
+                if (def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_DEV) {
+                    serialPort->vtbl->SetPath(serialPort, pathUtf16);
+                    if (def->serials[i]->target.port == 0) {
+                        serialPort->vtbl->SetIRQ(serialPort, 4);
+                        serialPort->vtbl->SetIOBase(serialPort, 1016);
+                        DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
+                              i, 4, 1016, def->serials[i]->data.file.path);
+                    } else if (def->serials[i]->target.port == 1) {
+                        serialPort->vtbl->SetIRQ(serialPort, 3);
+                        serialPort->vtbl->SetIOBase(serialPort, 760);
+                        DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
+                              i, 3, 760, def->serials[i]->data.file.path);
                     }
-
-                    serialPort->vtbl->nsisupports.Release((nsISupports *)serialPort);
-                    if (pathUtf16) {
-                        data->pFuncs->pfnUtf16Free(pathUtf16);
-                        pathUtf16 = NULL;
+                    serialPort->vtbl->SetHostMode(serialPort, PortMode_HostDevice);
+                } else if (def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_PIPE) {
+                    serialPort->vtbl->SetPath(serialPort, pathUtf16);
+                    if (def->serials[i]->target.port == 0) {
+                        serialPort->vtbl->SetIRQ(serialPort, 4);
+                        serialPort->vtbl->SetIOBase(serialPort, 1016);
+                        DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
+                              i, 4, 1016, def->serials[i]->data.file.path);
+                    } else if (def->serials[i]->target.port == 1) {
+                        serialPort->vtbl->SetIRQ(serialPort, 3);
+                        serialPort->vtbl->SetIOBase(serialPort, 760);
+                        DEBUG(" serialPort-%d irq: %d, iobase 0x%x, path: %s",
+                              i, 3, 760, def->serials[i]->data.file.path);
                     }
+                    if (!virFileExists(def->serials[i]->data.file.path)) {
+                        serialPort->vtbl->SetServer(serialPort, 1);
+                    }
+                    serialPort->vtbl->SetHostMode(serialPort, PortMode_HostPipe);
+                } else if (def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_NULL) {
+                    serialPort->vtbl->SetHostMode(serialPort, PortMode_Disconnected);
                 }
-            }
-        }   /* Finished:Block to attach the Serial Port to the VM */
 
-        {   /* Started:Block to attach the Parallel Port to the VM */
-            ISystemProperties *systemProperties = NULL;
-            PRUint32 parallelPortCount          = 0;
-            int i = 0;
-
-            data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
-            if (systemProperties) {
-                systemProperties->vtbl->GetParallelPortCount(systemProperties, &parallelPortCount);
-                systemProperties->vtbl->nsisupports.Release((nsISupports *)systemProperties);
-                systemProperties = NULL;
+                VBOX_RELEASE(serialPort);
+                if (pathUtf16) {
+                    VBOX_UTF16_FREE(pathUtf16);
+                    pathUtf16 = NULL;
+                }
             }
+        }
+    }   /* Finished:Block to attach the Serial Port to the VM */
 
-            DEBUG("Number of Parallel Ports to be connected: %d", def->nparallels);
-            DEBUG("Number of Parallel Ports available: %d", parallelPortCount);
-            for (i = 0; (i < def->nparallels) && (i < parallelPortCount); i++) {
-                IParallelPort *parallelPort = NULL;
+    {   /* Started:Block to attach the Parallel Port to the VM */
+        ISystemProperties *systemProperties = NULL;
+        PRUint32 parallelPortCount          = 0;
+        int i = 0;
 
-                DEBUG("ParallelPort(%d): Type: %d", i, def->parallels[i]->type);
-                DEBUG("ParallelPort(%d): target.port: %d", i, def->parallels[i]->target.port);
+        data->vboxObj->vtbl->GetSystemProperties(data->vboxObj, &systemProperties);
+        if (systemProperties) {
+            systemProperties->vtbl->GetParallelPortCount(systemProperties, &parallelPortCount);
+            VBOX_RELEASE(systemProperties);
+            systemProperties = NULL;
+        }
 
-                machine->vtbl->GetParallelPort(machine, i, &parallelPort);
-                if (parallelPort) {
-                    PRUnichar *pathUtf16 = NULL;
+        DEBUG("Number of Parallel Ports to be connected: %d", def->nparallels);
+        DEBUG("Number of Parallel Ports available: %d", parallelPortCount);
+        for (i = 0; (i < def->nparallels) && (i < parallelPortCount); i++) {
+            IParallelPort *parallelPort = NULL;
 
-                    data->pFuncs->pfnUtf8ToUtf16(def->parallels[i]->data.file.path, &pathUtf16);
+            DEBUG("ParallelPort(%d): Type: %d", i, def->parallels[i]->type);
+            DEBUG("ParallelPort(%d): target.port: %d", i, def->parallels[i]->target.port);
 
-                    /* For now hard code the parallel ports to
-                     * LPT1 (Base Addr: 0x378 (decimal: 888), IRQ: 7)
-                     * LPT2 (Base Addr: 0x278 (decimal: 632), IRQ: 5)
-                     * TODO: make this more flexible
-                     */
-                    if ((def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_DEV)  ||
-                        (def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_PTY)  ||
-                        (def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_FILE) ||
-                        (def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_PIPE)) {
-                        parallelPort->vtbl->SetPath(parallelPort, pathUtf16);
-                        if (i == 0) {
-                            parallelPort->vtbl->SetIRQ(parallelPort, 7);
-                            parallelPort->vtbl->SetIOBase(parallelPort, 888);
-                            DEBUG(" parallePort-%d irq: %d, iobase 0x%x, path: %s",
-                                  i, 7, 888, def->parallels[i]->data.file.path);
-                        } else if (i == 1) {
-                            parallelPort->vtbl->SetIRQ(parallelPort, 5);
-                            parallelPort->vtbl->SetIOBase(parallelPort, 632);
-                            DEBUG(" parallePort-%d irq: %d, iobase 0x%x, path: %s",
-                                  i, 5, 632, def->parallels[i]->data.file.path);
-                        }
-                    }
+            machine->vtbl->GetParallelPort(machine, i, &parallelPort);
+            if (parallelPort) {
+                PRUnichar *pathUtf16 = NULL;
 
-                    /* like serial port, parallel port can't be enabled unless
-                     * correct IRQ and IOBase values are specified.
-                     */
-                    parallelPort->vtbl->SetEnabled(parallelPort, 1);
+                VBOX_UTF8_TO_UTF16(def->parallels[i]->data.file.path, &pathUtf16);
 
-                    parallelPort->vtbl->nsisupports.Release((nsISupports *)parallelPort);
-                    if (pathUtf16) {
-                        data->pFuncs->pfnUtf16Free(pathUtf16);
-                        pathUtf16 = NULL;
+                /* For now hard code the parallel ports to
+                 * LPT1 (Base Addr: 0x378 (decimal: 888), IRQ: 7)
+                 * LPT2 (Base Addr: 0x278 (decimal: 632), IRQ: 5)
+                 * TODO: make this more flexible
+                 */
+                if ((def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_DEV)  ||
+                    (def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_PTY)  ||
+                    (def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_FILE) ||
+                    (def->parallels[i]->type == VIR_DOMAIN_CHR_TYPE_PIPE)) {
+                    parallelPort->vtbl->SetPath(parallelPort, pathUtf16);
+                    if (i == 0) {
+                        parallelPort->vtbl->SetIRQ(parallelPort, 7);
+                        parallelPort->vtbl->SetIOBase(parallelPort, 888);
+                        DEBUG(" parallePort-%d irq: %d, iobase 0x%x, path: %s",
+                              i, 7, 888, def->parallels[i]->data.file.path);
+                    } else if (i == 1) {
+                        parallelPort->vtbl->SetIRQ(parallelPort, 5);
+                        parallelPort->vtbl->SetIOBase(parallelPort, 632);
+                        DEBUG(" parallePort-%d irq: %d, iobase 0x%x, path: %s",
+                              i, 5, 632, def->parallels[i]->data.file.path);
                     }
                 }
-            }
-        }   /* Finished:Block to attach the Parallel Port to the VM */
-
-        {   /* Started:Block to specify video card settings */
-            if ((def->nvideos == 1) && (def->videos[0]->type == VIR_DOMAIN_VIDEO_TYPE_VBOX)) {
-                machine->vtbl->SetVRAMSize(machine, def->videos[0]->vram);
-                machine->vtbl->SetMonitorCount(machine, def->videos[0]->heads);
-                if (def->videos[0]->accel)
-                    machine->vtbl->SetAccelerate3DEnabled(machine, def->videos[0]->accel->support3d);
-                else
-                    machine->vtbl->SetAccelerate3DEnabled(machine, 0);
-            }
-        }   /* Finished:Block to specify video card settings */
-
-        {   /* Started:Block to attach the Remote Display to VM */
-            int vrdpPresent  = 0;
-            int sdlPresent   = 0;
-            int guiPresent   = 0;
-            char *guiDisplay = NULL;
-            char *sdlDisplay = NULL;
-            int i = 0;
-
-            for (i = 0; i < def->ngraphics; i++) {
-                IVRDPServer *VRDPServer = NULL;
 
-                if ((def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_RDP) && (vrdpPresent == 0)) {
-
-                    vrdpPresent = 1;
-                    machine->vtbl->GetVRDPServer(machine, &VRDPServer);
-                    if (VRDPServer) {
-                        VRDPServer->vtbl->SetEnabled(VRDPServer, PR_TRUE);
-                        DEBUG0("VRDP Support turned ON on port: 3389");
-
-                        if (def->graphics[i]->data.rdp.port) {
-                            VRDPServer->vtbl->SetPort(VRDPServer, def->graphics[i]->data.rdp.port);
-                            DEBUG("VRDP Port changed to: %d", def->graphics[i]->data.rdp.port);
-                        } else if (def->graphics[i]->data.rdp.autoport) {
-                            /* Setting the port to 0 will reset its value to
-                             * the default one which is 3389 currently
-                             */
-                            VRDPServer->vtbl->SetPort(VRDPServer, 0);
-                            DEBUG0("VRDP Port changed to default, which is 3389 currently");
-                        }
-
-                        if (def->graphics[i]->data.rdp.replaceUser) {
-                            VRDPServer->vtbl->SetReuseSingleConnection(VRDPServer, PR_TRUE);
-                            DEBUG0("VRDP set to reuse single connection");
-                        }
+                /* like serial port, parallel port can't be enabled unless
+                 * correct IRQ and IOBase values are specified.
+                 */
+                parallelPort->vtbl->SetEnabled(parallelPort, 1);
 
-                        if (def->graphics[i]->data.rdp.multiUser) {
-                            VRDPServer->vtbl->SetAllowMultiConnection(VRDPServer, PR_TRUE);
-                            DEBUG0("VRDP set to allow multiple connection");
-                        }
+                VBOX_RELEASE(parallelPort);
+                if (pathUtf16) {
+                    VBOX_UTF16_FREE(pathUtf16);
+                    pathUtf16 = NULL;
+                }
+            }
+        }
+    }   /* Finished:Block to attach the Parallel Port to the VM */
+
+    {   /* Started:Block to specify video card settings */
+        if ((def->nvideos == 1) && (def->videos[0]->type == VIR_DOMAIN_VIDEO_TYPE_VBOX)) {
+            machine->vtbl->SetVRAMSize(machine, def->videos[0]->vram);
+            machine->vtbl->SetMonitorCount(machine, def->videos[0]->heads);
+            if (def->videos[0]->accel)
+                machine->vtbl->SetAccelerate3DEnabled(machine, def->videos[0]->accel->support3d);
+            else
+                machine->vtbl->SetAccelerate3DEnabled(machine, 0);
+        }
+    }   /* Finished:Block to specify video card settings */
+
+    {   /* Started:Block to attach the Remote Display to VM */
+        int vrdpPresent  = 0;
+        int sdlPresent   = 0;
+        int guiPresent   = 0;
+        char *guiDisplay = NULL;
+        char *sdlDisplay = NULL;
+        int i = 0;
+
+        for (i = 0; i < def->ngraphics; i++) {
+            IVRDPServer *VRDPServer = NULL;
+
+            if ((def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_RDP) && (vrdpPresent == 0)) {
+
+                vrdpPresent = 1;
+                machine->vtbl->GetVRDPServer(machine, &VRDPServer);
+                if (VRDPServer) {
+                    VRDPServer->vtbl->SetEnabled(VRDPServer, PR_TRUE);
+                    DEBUG0("VRDP Support turned ON.");
+
+#if VBOX_API_VERSION < 3001
+                    if (def->graphics[i]->data.rdp.port) {
+                        VRDPServer->vtbl->SetPort(VRDPServer, def->graphics[i]->data.rdp.port);
+                        DEBUG("VRDP Port changed to: %d", def->graphics[i]->data.rdp.port);
+                    } else if (def->graphics[i]->data.rdp.autoport) {
+                        /* Setting the port to 0 will reset its value to
+                         * the default one which is 3389 currently
+                         */
+                        VRDPServer->vtbl->SetPort(VRDPServer, 0);
+                        DEBUG0("VRDP Port changed to default, which is 3389 currently");
+                    }
+#else  /* VBOX_API_VERSION >= 3001 */
+                    PRUnichar *portUtf16 = NULL;
+                    portUtf16 = PRUnicharFromInt(def->graphics[i]->data.rdp.port);
+                    VRDPServer->vtbl->SetPorts(VRDPServer, portUtf16);
+                    VBOX_UTF16_FREE(portUtf16);
+#endif /* VBOX_API_VERSION >= 3001 */
+
+                    if (def->graphics[i]->data.rdp.replaceUser) {
+                        VRDPServer->vtbl->SetReuseSingleConnection(VRDPServer, PR_TRUE);
+                        DEBUG0("VRDP set to reuse single connection");
+                    }
 
-                        if (def->graphics[i]->data.rdp.listenAddr) {
-                            PRUnichar *netAddressUtf16 = NULL;
+                    if (def->graphics[i]->data.rdp.multiUser) {
+                        VRDPServer->vtbl->SetAllowMultiConnection(VRDPServer, PR_TRUE);
+                        DEBUG0("VRDP set to allow multiple connection");
+                    }
 
-                            data->pFuncs->pfnUtf8ToUtf16(def->graphics[i]->data.rdp.listenAddr, &netAddressUtf16);
-                            VRDPServer->vtbl->SetNetAddress(VRDPServer, netAddressUtf16);
-                            DEBUG("VRDP listen address is set to: %s", def->graphics[i]->data.rdp.listenAddr);
+                    if (def->graphics[i]->data.rdp.listenAddr) {
+                        PRUnichar *netAddressUtf16 = NULL;
 
-                            data->pFuncs->pfnUtf16Free(netAddressUtf16);
-                        }
+                        VBOX_UTF8_TO_UTF16(def->graphics[i]->data.rdp.listenAddr, &netAddressUtf16);
+                        VRDPServer->vtbl->SetNetAddress(VRDPServer, netAddressUtf16);
+                        DEBUG("VRDP listen address is set to: %s", def->graphics[i]->data.rdp.listenAddr);
 
-                        VRDPServer->vtbl->nsisupports.Release((nsISupports *)VRDPServer);
+                        VBOX_UTF16_FREE(netAddressUtf16);
                     }
+
+                    VBOX_RELEASE(VRDPServer);
                 }
+            }
 
-                if ((def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP) && (guiPresent == 0)) {
-                    guiPresent = 1;
-                    if (def->graphics[i]->data.desktop.display) {
-                        guiDisplay = strdup(def->graphics[i]->data.desktop.display);
-                        if (guiDisplay == NULL) {
-                            virReportOOMError(conn);
-                            /* just don't go to cleanup yet as it is ok to have
-                             * guiDisplay as NULL and we check it below if it
-                             * exist and then only use it there
-                             */
-                        }
+            if ((def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP) && (guiPresent == 0)) {
+                guiPresent = 1;
+                if (def->graphics[i]->data.desktop.display) {
+                    guiDisplay = strdup(def->graphics[i]->data.desktop.display);
+                    if (guiDisplay == NULL) {
+                        virReportOOMError(conn);
+                        /* just don't go to cleanup yet as it is ok to have
+                         * guiDisplay as NULL and we check it below if it
+                         * exist and then only use it there
+                         */
                     }
                 }
+            }
 
-                if ((def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) && (sdlPresent == 0)) {
-                    sdlPresent = 1;
-                    if (def->graphics[i]->data.sdl.display) {
-                        sdlDisplay = strdup(def->graphics[i]->data.sdl.display);
-                        if (sdlDisplay == NULL) {
-                            virReportOOMError(conn);
-                            /* just don't go to cleanup yet as it is ok to have
-                             * sdlDisplay as NULL and we check it below if it
-                             * exist and then only use it there
-                             */
-                        }
+            if ((def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) && (sdlPresent == 0)) {
+                sdlPresent = 1;
+                if (def->graphics[i]->data.sdl.display) {
+                    sdlDisplay = strdup(def->graphics[i]->data.sdl.display);
+                    if (sdlDisplay == NULL) {
+                        virReportOOMError(conn);
+                        /* just don't go to cleanup yet as it is ok to have
+                         * sdlDisplay as NULL and we check it below if it
+                         * exist and then only use it there
+                         */
                     }
                 }
             }
+        }
 
-            if ((vrdpPresent == 1) && (guiPresent == 0) && (sdlPresent == 0)) {
-                /* store extradata key that frontend is set to vrdp */
-                PRUnichar *keyTypeUtf16   = NULL;
-                PRUnichar *valueTypeUtf16 = NULL;
+        if ((vrdpPresent == 1) && (guiPresent == 0) && (sdlPresent == 0)) {
+            /* store extradata key that frontend is set to vrdp */
+            PRUnichar *keyTypeUtf16   = NULL;
+            PRUnichar *valueTypeUtf16 = NULL;
 
-                data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Type", &keyTypeUtf16);
-                data->pFuncs->pfnUtf8ToUtf16("vrdp", &valueTypeUtf16);
+            VBOX_UTF8_TO_UTF16("FRONTEND/Type", &keyTypeUtf16);
+            VBOX_UTF8_TO_UTF16("vrdp", &valueTypeUtf16);
 
-                machine->vtbl->SetExtraData(machine, keyTypeUtf16, valueTypeUtf16);
+            machine->vtbl->SetExtraData(machine, keyTypeUtf16, valueTypeUtf16);
 
-                data->pFuncs->pfnUtf16Free(keyTypeUtf16);
-                data->pFuncs->pfnUtf16Free(valueTypeUtf16);
+            VBOX_UTF16_FREE(keyTypeUtf16);
+            VBOX_UTF16_FREE(valueTypeUtf16);
 
-            } else if ((guiPresent == 0) && (sdlPresent == 1)) {
-                /* store extradata key that frontend is set to sdl */
-                PRUnichar *keyTypeUtf16      = NULL;
-                PRUnichar *valueTypeUtf16    = NULL;
-                PRUnichar *keyDislpayUtf16   = NULL;
-                PRUnichar *valueDisplayUtf16 = NULL;
+        } else if ((guiPresent == 0) && (sdlPresent == 1)) {
+            /* store extradata key that frontend is set to sdl */
+            PRUnichar *keyTypeUtf16      = NULL;
+            PRUnichar *valueTypeUtf16    = NULL;
+            PRUnichar *keyDislpayUtf16   = NULL;
+            PRUnichar *valueDisplayUtf16 = NULL;
 
-                data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Type", &keyTypeUtf16);
-                data->pFuncs->pfnUtf8ToUtf16("sdl", &valueTypeUtf16);
+            VBOX_UTF8_TO_UTF16("FRONTEND/Type", &keyTypeUtf16);
+            VBOX_UTF8_TO_UTF16("sdl", &valueTypeUtf16);
 
-                machine->vtbl->SetExtraData(machine, keyTypeUtf16, valueTypeUtf16);
+            machine->vtbl->SetExtraData(machine, keyTypeUtf16, valueTypeUtf16);
 
-                data->pFuncs->pfnUtf16Free(keyTypeUtf16);
-                data->pFuncs->pfnUtf16Free(valueTypeUtf16);
+            VBOX_UTF16_FREE(keyTypeUtf16);
+            VBOX_UTF16_FREE(valueTypeUtf16);
 
-                if (sdlDisplay) {
-                    data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Display", &keyDislpayUtf16);
-                    data->pFuncs->pfnUtf8ToUtf16(sdlDisplay, &valueDisplayUtf16);
+            if (sdlDisplay) {
+                VBOX_UTF8_TO_UTF16("FRONTEND/Display", &keyDislpayUtf16);
+                VBOX_UTF8_TO_UTF16(sdlDisplay, &valueDisplayUtf16);
 
-                    machine->vtbl->SetExtraData(machine, keyDislpayUtf16, valueDisplayUtf16);
+                machine->vtbl->SetExtraData(machine, keyDislpayUtf16, valueDisplayUtf16);
 
-                    data->pFuncs->pfnUtf16Free(keyDislpayUtf16);
-                    data->pFuncs->pfnUtf16Free(valueDisplayUtf16);
-                }
+                VBOX_UTF16_FREE(keyDislpayUtf16);
+                VBOX_UTF16_FREE(valueDisplayUtf16);
+            }
 
-            } else {
-                /* if all are set then default is gui, with vrdp turned on */
-                PRUnichar *keyTypeUtf16      = NULL;
-                PRUnichar *valueTypeUtf16    = NULL;
-                PRUnichar *keyDislpayUtf16   = NULL;
-                PRUnichar *valueDisplayUtf16 = NULL;
+        } else {
+            /* if all are set then default is gui, with vrdp turned on */
+            PRUnichar *keyTypeUtf16      = NULL;
+            PRUnichar *valueTypeUtf16    = NULL;
+            PRUnichar *keyDislpayUtf16   = NULL;
+            PRUnichar *valueDisplayUtf16 = NULL;
 
-                data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Type", &keyTypeUtf16);
-                data->pFuncs->pfnUtf8ToUtf16("gui", &valueTypeUtf16);
+            VBOX_UTF8_TO_UTF16("FRONTEND/Type", &keyTypeUtf16);
+            VBOX_UTF8_TO_UTF16("gui", &valueTypeUtf16);
 
-                machine->vtbl->SetExtraData(machine, keyTypeUtf16, valueTypeUtf16);
+            machine->vtbl->SetExtraData(machine, keyTypeUtf16, valueTypeUtf16);
 
-                data->pFuncs->pfnUtf16Free(keyTypeUtf16);
-                data->pFuncs->pfnUtf16Free(valueTypeUtf16);
+            VBOX_UTF16_FREE(keyTypeUtf16);
+            VBOX_UTF16_FREE(valueTypeUtf16);
 
-                if (guiDisplay) {
-                    data->pFuncs->pfnUtf8ToUtf16("FRONTEND/Display", &keyDislpayUtf16);
-                    data->pFuncs->pfnUtf8ToUtf16(guiDisplay, &valueDisplayUtf16);
+            if (guiDisplay) {
+                VBOX_UTF8_TO_UTF16("FRONTEND/Display", &keyDislpayUtf16);
+                VBOX_UTF8_TO_UTF16(guiDisplay, &valueDisplayUtf16);
 
-                    machine->vtbl->SetExtraData(machine, keyDislpayUtf16, valueDisplayUtf16);
+                machine->vtbl->SetExtraData(machine, keyDislpayUtf16, valueDisplayUtf16);
 
-                    data->pFuncs->pfnUtf16Free(keyDislpayUtf16);
-                    data->pFuncs->pfnUtf16Free(valueDisplayUtf16);
-                }
+                VBOX_UTF16_FREE(keyDislpayUtf16);
+                VBOX_UTF16_FREE(valueDisplayUtf16);
             }
+        }
 
-            VIR_FREE(guiDisplay);
-            VIR_FREE(sdlDisplay);
+        VIR_FREE(guiDisplay);
+        VIR_FREE(sdlDisplay);
 
-        }   /* Finished:Block to attach the Remote Display to VM */
+    }   /* Finished:Block to attach the Remote Display to VM */
 
-        {   /* Started:Block to attach USB Devices to VM */
-            if (def->nhostdevs > 0) {
-                IUSBController *USBController = NULL;
-                int i = 0, isUSB = 0;
-                /* Loop through the devices first and see if you
-                 * have a USB Device, only if you have one then
-                 * start the USB controller else just proceed as
-                 * usual
-                 */
-                for (i = 0; i < def->nhostdevs; i++) {
-                    if (def->hostdevs[i]->mode ==
-                            VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
-                        if (def->hostdevs[i]->source.subsys.type ==
-                                VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
-                            if (def->hostdevs[i]->source.subsys.u.usb.vendor ||
-                                def->hostdevs[i]->source.subsys.u.usb.product) {
-                                DEBUG("USB Device detected, VendorId:0x%x, ProductId:0x%x",
-                                      def->hostdevs[i]->source.subsys.u.usb.vendor,
-                                      def->hostdevs[i]->source.subsys.u.usb.product);
-                                isUSB++;
-                            }
+    {   /* Started:Block to attach USB Devices to VM */
+        if (def->nhostdevs > 0) {
+            IUSBController *USBController = NULL;
+            int i = 0, isUSB = 0;
+            /* Loop through the devices first and see if you
+             * have a USB Device, only if you have one then
+             * start the USB controller else just proceed as
+             * usual
+             */
+            for (i = 0; i < def->nhostdevs; i++) {
+                if (def->hostdevs[i]->mode ==
+                        VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
+                    if (def->hostdevs[i]->source.subsys.type ==
+                            VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+                        if (def->hostdevs[i]->source.subsys.u.usb.vendor ||
+                            def->hostdevs[i]->source.subsys.u.usb.product) {
+                            DEBUG("USB Device detected, VendorId:0x%x, ProductId:0x%x",
+                                  def->hostdevs[i]->source.subsys.u.usb.vendor,
+                                  def->hostdevs[i]->source.subsys.u.usb.product);
+                            isUSB++;
                         }
                     }
                 }
+            }
 
-                if (isUSB > 0) {
-                    /* First Start the USB Controller and then loop
-                     * to attach USB Devices to it
-                     */
-                    machine->vtbl->GetUSBController(machine, &USBController);
-                    if (USBController) {
-                        USBController->vtbl->SetEnabled(USBController, 1);
-                        USBController->vtbl->SetEnabledEhci(USBController, 1);
-
-                        for (i = 0; i < def->nhostdevs; i++) {
-                            if (def->hostdevs[i]->mode ==
-                                    VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
-                                if (def->hostdevs[i]->source.subsys.type ==
-                                        VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
-
-                                    char filtername[11]        = {0};
-                                    PRUnichar *filternameUtf16 = NULL;
-                                    IUSBDeviceFilter *filter   = NULL;
-
-                                    /* Assuming can't have more then 9999 devices so
-                                     * restricting to %04d
-                                     */
-                                    sprintf(filtername, "filter%04d", i);
-                                    data->pFuncs->pfnUtf8ToUtf16(filtername, &filternameUtf16);
-
-                                    USBController->vtbl->CreateDeviceFilter(USBController,
-                                                                            filternameUtf16,
-                                                                            &filter);
-                                    data->pFuncs->pfnUtf16Free(filternameUtf16);
-
-                                    if (filter &&
-                                        (def->hostdevs[i]->source.subsys.u.usb.vendor ||
-                                        def->hostdevs[i]->source.subsys.u.usb.product)) {
-
-                                        PRUnichar *vendorIdUtf16  = NULL;
-                                        char vendorId[40]         = {0};
-                                        PRUnichar *productIdUtf16 = NULL;
-                                        char productId[40]        = {0};
-
-                                        if (def->hostdevs[i]->source.subsys.u.usb.vendor) {
-                                            sprintf(vendorId, "%x", def->hostdevs[i]->source.subsys.u.usb.vendor);
-                                            data->pFuncs->pfnUtf8ToUtf16(vendorId, &vendorIdUtf16);
-                                            filter->vtbl->SetVendorId(filter, vendorIdUtf16);
-                                            data->pFuncs->pfnUtf16Free(vendorIdUtf16);
-                                        }
-                                        if (def->hostdevs[i]->source.subsys.u.usb.product) {
-                                            sprintf(productId, "%x", def->hostdevs[i]->source.subsys.u.usb.product);
-                                            data->pFuncs->pfnUtf8ToUtf16(productId, &productIdUtf16);
-                                            filter->vtbl->SetProductId(filter, productIdUtf16);
-                                            data->pFuncs->pfnUtf16Free(productIdUtf16);
-                                        }
-                                        filter->vtbl->SetActive(filter, 1);
-                                        USBController->vtbl->InsertDeviceFilter(USBController,
-                                                                                i,
-                                                                                filter);
-                                        filter->vtbl->nsisupports.Release((nsISupports *)filter);
-                                    }
+            if (isUSB > 0) {
+                /* First Start the USB Controller and then loop
+                 * to attach USB Devices to it
+                 */
+                machine->vtbl->GetUSBController(machine, &USBController);
+                if (USBController) {
+                    USBController->vtbl->SetEnabled(USBController, 1);
+                    USBController->vtbl->SetEnabledEhci(USBController, 1);
 
+                    for (i = 0; i < def->nhostdevs; i++) {
+                        if (def->hostdevs[i]->mode ==
+                                VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
+                            if (def->hostdevs[i]->source.subsys.type ==
+                                    VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+
+                                char filtername[11]        = {0};
+                                PRUnichar *filternameUtf16 = NULL;
+                                IUSBDeviceFilter *filter   = NULL;
+
+                                /* Assuming can't have more then 9999 devices so
+                                 * restricting to %04d
+                                 */
+                                sprintf(filtername, "filter%04d", i);
+                                VBOX_UTF8_TO_UTF16(filtername, &filternameUtf16);
+
+                                USBController->vtbl->CreateDeviceFilter(USBController,
+                                                                        filternameUtf16,
+                                                                        &filter);
+                                VBOX_UTF16_FREE(filternameUtf16);
+
+                                if (filter &&
+                                    (def->hostdevs[i]->source.subsys.u.usb.vendor ||
+                                    def->hostdevs[i]->source.subsys.u.usb.product)) {
+
+                                    PRUnichar *vendorIdUtf16  = NULL;
+                                    char vendorId[40]         = {0};
+                                    PRUnichar *productIdUtf16 = NULL;
+                                    char productId[40]        = {0};
+
+                                    if (def->hostdevs[i]->source.subsys.u.usb.vendor) {
+                                        sprintf(vendorId, "%x", def->hostdevs[i]->source.subsys.u.usb.vendor);
+                                        VBOX_UTF8_TO_UTF16(vendorId, &vendorIdUtf16);
+                                        filter->vtbl->SetVendorId(filter, vendorIdUtf16);
+                                        VBOX_UTF16_FREE(vendorIdUtf16);
+                                    }
+                                    if (def->hostdevs[i]->source.subsys.u.usb.product) {
+                                        sprintf(productId, "%x", def->hostdevs[i]->source.subsys.u.usb.product);
+                                        VBOX_UTF8_TO_UTF16(productId, &productIdUtf16);
+                                        filter->vtbl->SetProductId(filter, productIdUtf16);
+                                        VBOX_UTF16_FREE(productIdUtf16);
+                                    }
+                                    filter->vtbl->SetActive(filter, 1);
+                                    USBController->vtbl->InsertDeviceFilter(USBController,
+                                                                            i,
+                                                                            filter);
+                                    VBOX_RELEASE(filter);
                                 }
+
                             }
                         }
-                        USBController->vtbl->nsisupports.Release((nsISupports *)USBController);
                     }
+                    VBOX_RELEASE(USBController);
                 }
             }
-        }   /* Finished:Block to attach USB Devices to VM */
+        }
+    }   /* Finished:Block to attach USB Devices to VM */
 
-        /* Save the machine settings made till now and close the
-         * session. also free up the mchiid variable used.
-         */
-        rc = machine->vtbl->SaveSettings(machine);
-        data->vboxSession->vtbl->Close(data->vboxSession);
-        vboxIIDUnalloc(mchiid);
+    /* Save the machine settings made till now and close the
+     * session. also free up the mchiid variable used.
+     */
+    rc = machine->vtbl->SaveSettings(machine);
+    data->vboxSession->vtbl->Close(data->vboxSession);
+    vboxIIDUnalloc(mchiid);
 
-        dom = virGetDomain(conn, def->name, def->uuid);
-        if(machine) {
-            machine->vtbl->nsisupports.Release((nsISupports *)machine);
-            machine = NULL;
-        }
-    }
+    ret = virGetDomain(conn, def->name, def->uuid);
+    VBOX_RELEASE(machine);
+    machine = NULL;
 
     vboxIIDFree(iid);
     virDomainDefFree(def);
 
-    return dom;
+    return ret;
 
 cleanup:
-    if(machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
+    VBOX_RELEASE(machine);
     vboxIIDFree(iid);
     virDomainDefFree(def);
     return NULL;
 }
 
 static int vboxDomainUndefine(virDomainPtr dom) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
-    int ret = -1;
+    nsresult rc;
 
 #if VBOX_API_VERSION == 2002
     if (VIR_ALLOC(iid) < 0) {
@@ -4016,67 +4562,120 @@ static int vboxDomainUndefine(virDomainPtr dom) {
     }
 #endif
 
-    if(data->vboxObj) {
-        vboxIIDFromUUID(dom->uuid, iid);
+    vboxIIDFromUUID(dom->uuid, iid);
 
-        /* Block for checking if HDD's are attched to VM.
-         * considering just IDE bus for now. Also skipped
-         * chanel=1 and device=0 (Secondary Master) as currenlty
-         * it is allocated to CD/DVD Drive bt default
-         */
-        {
-            PRUnichar *hddcnameUtf16 = NULL;
+    /* Block for checking if HDD's are attched to VM.
+     * considering just IDE bus for now. Also skipped
+     * chanel=1 and device=0 (Secondary Master) as currenlty
+     * it is allocated to CD/DVD Drive bt default
+     */
+    {
+        PRUnichar *hddcnameUtf16 = NULL;
 
-            char *hddcname = strdup("IDE");
-            data->pFuncs->pfnUtf8ToUtf16(hddcname, &hddcnameUtf16);
-            VIR_FREE(hddcname);
+        char *hddcname = strdup("IDE");
+        VBOX_UTF8_TO_UTF16(hddcname, &hddcnameUtf16);
+        VIR_FREE(hddcname);
 
-            /* Open a Session for the machine */
-            rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
-            if (NS_SUCCEEDED(rc)) {
-                rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-                if (NS_SUCCEEDED(rc) && machine) {
+        /* Open a Session for the machine */
+        rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
+        if (NS_SUCCEEDED(rc)) {
+            rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
+            if (NS_SUCCEEDED(rc) && machine) {
+
+#if VBOX_API_VERSION < 3001
+                /* Disconnect all the drives if present */
+                machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 0, 0);
+                machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 0, 1);
+                machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 1, 1);
+#else  /* VBOX_API_VERSION >= 3001 */
+                /* get all the controller first, then the attachments and
+                 * remove them all so that the machine can be undefined
+                 */
+                PRUint32 strCtlSize = 0;
+                IStorageController **aStrCtls = NULL;
+                int i = 0, j = 0;
+
+                machine->vtbl->GetStorageControllers(machine,
+                                                     &strCtlSize,
+                                                     &aStrCtls);
+
+                for (i = 0; i < strCtlSize; i++) {
+                    IStorageController *strCtl = aStrCtls[i];
+                    PRUnichar *strCtlName = NULL;
+                    PRUint32   medAttSize = 0;
+                    IMediumAttachment **aMedAtts = NULL;
+
+                    if (!strCtl)
+                        continue;
+
+                    strCtl->vtbl->GetName(strCtl, &strCtlName);
+                    machine->vtbl->GetMediumAttachmentsOfController(machine,
+                                                                    strCtlName,
+                                                                    &medAttSize,
+                                                                    &aMedAtts);
+
+                    for (j = 0; j < medAttSize; j++) {
+                        IMediumAttachment *medAtt = aMedAtts[j];
+                        PRInt32 port = ~0U;
+                        PRInt32 device = ~0U;
+
+                        if (!medAtt)
+                            continue;
+
+                        medAtt->vtbl->GetPort(medAtt, &port);
+                        medAtt->vtbl->GetDevice(medAtt, &device);
+
+                        if ((port != ~0U) && (device != ~0U)) {
+                            machine->vtbl->DetachDevice(machine,
+                                                        strCtlName,
+                                                        port,
+                                                        device);
+                        }
 
-                    /* Disconnect all the drives if present */
-                    machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 0, 0);
-                    machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 0, 1);
-                    machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 1, 1);
+                        VBOX_RELEASE(medAtt);
+                    }
 
-                    machine->vtbl->SaveSettings(machine);
+                    VBOX_RELEASE(strCtl);
+                    machine->vtbl->RemoveStorageController(machine, strCtlName);
+                    VBOX_UTF16_FREE(strCtlName);
                 }
-                data->vboxSession->vtbl->Close(data->vboxSession);
+#endif /* VBOX_API_VERSION >= 3001 */
+
+                machine->vtbl->SaveSettings(machine);
             }
-            data->pFuncs->pfnUtf16Free(hddcnameUtf16);
+            data->vboxSession->vtbl->Close(data->vboxSession);
         }
+        VBOX_UTF16_FREE(hddcnameUtf16);
+    }
 
-        rc = data->vboxObj->vtbl->UnregisterMachine(data->vboxObj, iid, &machine);
-        DEBUGIID("UUID of machine being undefined", iid);
+    rc = data->vboxObj->vtbl->UnregisterMachine(data->vboxObj, iid, &machine);
+    DEBUGIID("UUID of machine being undefined", iid);
 
-        if (NS_SUCCEEDED(rc) && machine){
-            machine->vtbl->DeleteSettings(machine);
-            ret = 0;
-        }
+    if (NS_SUCCEEDED(rc) && machine){
+        machine->vtbl->DeleteSettings(machine);
+        ret = 0;
+    } else {
+        vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                  "could not delete the domain", (unsigned)rc);
     }
 
 #if VBOX_API_VERSION == 2002
 cleanup:
 #endif
     vboxIIDFree(iid);
-    if (machine)
-        machine->vtbl->nsisupports.Release((nsISupports *)machine);
+    VBOX_RELEASE(machine);
 
     return ret;
 }
 
 static int vboxDomainAttachDevice(virDomainPtr dom, const char *xml) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     PRUint32 state       = MachineState_Null;
     virDomainDefPtr def  = NULL;
     virDomainDeviceDefPtr dev  = NULL;
-    int ret = -1;
+    nsresult rc;
 
     if (VIR_ALLOC(def) < 0) {
         virReportOOMError(dom->conn);
@@ -4106,156 +4705,157 @@ static int vboxDomainAttachDevice(virDomainPtr dom, const char *xml) {
 
     vboxIIDFromUUID(dom->uuid, iid);
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,"no domain with matching uuid");
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN,"no domain with matching uuid");
+        goto cleanup;
+    }
 
-        if (machine) {
-            machine->vtbl->GetState(machine, &state);
+    if (machine) {
+        machine->vtbl->GetState(machine, &state);
 
-            if ((state == MachineState_Running) ||
-                (state == MachineState_Paused)) {
-                rc = data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-            } else {
-                rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
-            }
-            if (NS_SUCCEEDED(rc)) {
-                rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-                if (NS_SUCCEEDED(rc) && machine) {
-                    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
-                        if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
-                            if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                                IDVDDrive *dvdDrive = NULL;
-                                /* Currently CDROM/DVD Drive is always IDE
-                                 * Secondary Master so neglecting the following
-                                 * parameter dev->data.disk->bus
-                                 */
-                                machine->vtbl->GetDVDDrive(machine, &dvdDrive);
-                                if (dvdDrive) {
-                                    IDVDImage *dvdImage          = NULL;
-                                    PRUnichar *dvdfileUtf16      = NULL;
-                                    vboxIID   *dvduuid           = NULL;
+        if ((state == MachineState_Running) ||
+            (state == MachineState_Paused)) {
+            rc = data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+        } else {
+            rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
+        }
+        if (NS_SUCCEEDED(rc)) {
+            rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
+            if (NS_SUCCEEDED(rc) && machine) {
+                if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
+#if VBOX_API_VERSION < 3001
+                    if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
+                        if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                            IDVDDrive *dvdDrive = NULL;
+                            /* Currently CDROM/DVD Drive is always IDE
+                             * Secondary Master so neglecting the following
+                             * parameter dev->data.disk->bus
+                             */
+                            machine->vtbl->GetDVDDrive(machine, &dvdDrive);
+                            if (dvdDrive) {
+                                IDVDImage *dvdImage          = NULL;
+                                PRUnichar *dvdfileUtf16      = NULL;
+                                vboxIID   *dvduuid           = NULL;
 #if VBOX_API_VERSION == 2002
-                                    nsID dvdemptyuuid;
+                                nsID dvdemptyuuid;
 
-                                    memset(&dvdemptyuuid, 0, sizeof(dvdemptyuuid));
+                                memset(&dvdemptyuuid, 0, sizeof(dvdemptyuuid));
 #else
-                                    PRUnichar *dvdemptyuuidUtf16 = NULL;
+                                PRUnichar *dvdemptyuuidUtf16 = NULL;
 #endif
 
-                                    data->pFuncs->pfnUtf8ToUtf16(dev->data.disk->src, &dvdfileUtf16);
+                                VBOX_UTF8_TO_UTF16(dev->data.disk->src, &dvdfileUtf16);
 
-                                    data->vboxObj->vtbl->FindDVDImage(data->vboxObj, dvdfileUtf16, &dvdImage);
-                                    if (!dvdImage) {
+                                data->vboxObj->vtbl->FindDVDImage(data->vboxObj, dvdfileUtf16, &dvdImage);
+                                if (!dvdImage) {
 #if VBOX_API_VERSION == 2002
-                                        data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, &dvdemptyuuid, &dvdImage);
+                                    data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, &dvdemptyuuid, &dvdImage);
 #else
-                                        data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, dvdemptyuuidUtf16, &dvdImage);
+                                    data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, dvdemptyuuidUtf16, &dvdImage);
 #endif
-                                    }
-                                    if (dvdImage) {
-                                        rc = dvdImage->vtbl->imedium.GetId((IMedium *)dvdImage, &dvduuid);
+                                }
+                                if (dvdImage) {
+                                    rc = dvdImage->vtbl->imedium.GetId((IMedium *)dvdImage, &dvduuid);
+                                    if (NS_FAILED(rc)) {
+                                        vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                                                  "can't get the uuid of the file to be attached to cdrom",
+                                                  dev->data.disk->src, (unsigned)rc);
+                                    } else {
+                                        /* unmount the previous mounted image */
+                                        dvdDrive->vtbl->Unmount(dvdDrive);
+                                        rc = dvdDrive->vtbl->MountImage(dvdDrive, dvduuid);
                                         if (NS_FAILED(rc)) {
                                             vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                      "can't get the uuid of the file to be attached to cdrom",
+                                                      "could not attach the file to cdrom",
                                                       dev->data.disk->src, (unsigned)rc);
                                         } else {
-                                            /* unmount the previous mounted image */
-                                            dvdDrive->vtbl->Unmount(dvdDrive);
-                                            rc = dvdDrive->vtbl->MountImage(dvdDrive, dvduuid);
-                                            if (NS_FAILED(rc)) {
-                                                vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                          "could not attach the file to cdrom",
-                                                          dev->data.disk->src, (unsigned)rc);
-                                            } else {
-                                                ret = 0;
-                                                DEBUGIID("CD/DVD Image UUID:", dvduuid);
-                                            }
+                                            ret = 0;
+                                            DEBUGIID("CD/DVD Image UUID:", dvduuid);
                                         }
-
-                                        dvdImage->vtbl->imedium.nsisupports.Release((nsISupports *)dvdImage);
                                     }
-                                    vboxIIDUnalloc(dvduuid);
-                                    data->pFuncs->pfnUtf16Free(dvdfileUtf16);
-                                    dvdDrive->vtbl->nsisupports.Release((nsISupports *)dvdDrive);
+
+                                    VBOX_MEDIUM_RELEASE(dvdImage);
                                 }
-                            } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                                vboxIIDUnalloc(dvduuid);
+                                VBOX_UTF16_FREE(dvdfileUtf16);
+                                VBOX_RELEASE(dvdDrive);
                             }
-                        } else if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
-                            if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                                IFloppyDrive *floppyDrive;
-                                machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
-                                if (floppyDrive) {
-                                    rc = floppyDrive->vtbl->SetEnabled(floppyDrive, 1);
-                                    if (NS_SUCCEEDED(rc)) {
-                                        IFloppyImage *floppyImage   = NULL;
-                                        PRUnichar *fdfileUtf16      = NULL;
-                                        vboxIID *fduuid             = NULL;
+                        } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                        }
+                    } else if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
+                        if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                            IFloppyDrive *floppyDrive;
+                            machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
+                            if (floppyDrive) {
+                                rc = floppyDrive->vtbl->SetEnabled(floppyDrive, 1);
+                                if (NS_SUCCEEDED(rc)) {
+                                    IFloppyImage *floppyImage   = NULL;
+                                    PRUnichar *fdfileUtf16      = NULL;
+                                    vboxIID *fduuid             = NULL;
 #if VBOX_API_VERSION == 2002
-                                        nsID fdemptyuuid;
+                                    nsID fdemptyuuid;
 
-                                        memset(&fdemptyuuid, 0, sizeof(fdemptyuuid));
+                                    memset(&fdemptyuuid, 0, sizeof(fdemptyuuid));
 #else
-                                        PRUnichar *fdemptyuuidUtf16 = NULL;
+                                    PRUnichar *fdemptyuuidUtf16 = NULL;
 #endif
-                                        data->pFuncs->pfnUtf8ToUtf16(dev->data.disk->src, &fdfileUtf16);
-                                        rc = data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
-                                                                                  fdfileUtf16,
-                                                                                  &floppyImage);
-
-                                        if (!floppyImage) {
-                                            data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
-                                                                                 fdfileUtf16,
+                                    VBOX_UTF8_TO_UTF16(dev->data.disk->src, &fdfileUtf16);
+                                    rc = data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
+                                                                              fdfileUtf16,
+                                                                              &floppyImage);
+
+                                    if (!floppyImage) {
+                                        data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
+                                                                             fdfileUtf16,
 #if VBOX_API_VERSION == 2002
-                                                                                 &fdemptyuuid,
+                                                                             &fdemptyuuid,
 #else
-                                                                                 fdemptyuuidUtf16,
+                                                                             fdemptyuuidUtf16,
 #endif
-                                                                                 &floppyImage);
-                                        }
+                                                                             &floppyImage);
+                                    }
 
-                                        if (floppyImage) {
-                                            rc = floppyImage->vtbl->imedium.GetId((IMedium *)floppyImage, &fduuid);
+                                    if (floppyImage) {
+                                        rc = floppyImage->vtbl->imedium.GetId((IMedium *)floppyImage, &fduuid);
+                                        if (NS_FAILED(rc)) {
+                                            vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                                                      "can't get the uuid of the file to be attached to floppy drive",
+                                                      dev->data.disk->src, (unsigned)rc);
+                                        } else {
+                                            rc = floppyDrive->vtbl->MountImage(floppyDrive, fduuid);
                                             if (NS_FAILED(rc)) {
                                                 vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                          "can't get the uuid of the file to be attached to floppy drive",
+                                                          "could not attach the file to floppy drive",
                                                           dev->data.disk->src, (unsigned)rc);
                                             } else {
-                                                rc = floppyDrive->vtbl->MountImage(floppyDrive, fduuid);
-                                                if (NS_FAILED(rc)) {
-                                                    vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                                                              "could not attach the file to floppy drive",
-                                                              dev->data.disk->src, (unsigned)rc);
-                                                } else {
-                                                    ret = 0;
-                                                    DEBUGIID("attached floppy, UUID:", fduuid);
-                                                }
+                                                ret = 0;
+                                                DEBUGIID("attached floppy, UUID:", fduuid);
                                             }
-                                            floppyImage->vtbl->imedium.nsisupports.Release((nsISupports *)floppyImage);
                                         }
-                                        vboxIIDUnalloc(fduuid);
-                                        data->pFuncs->pfnUtf16Free(fdfileUtf16);
+                                        VBOX_MEDIUM_RELEASE(floppyImage);
                                     }
-                                    floppyDrive->vtbl->nsisupports.Release((nsISupports *)floppyDrive);
+                                    vboxIIDUnalloc(fduuid);
+                                    VBOX_UTF16_FREE(fdfileUtf16);
                                 }
-                            } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                                VBOX_RELEASE(floppyDrive);
                             }
+                        } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
                         }
-                    } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
-                    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
-                        if (dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
-                            if (dev->data.hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
-                            }
+                    }
+#else  /* VBOX_API_VERSION >= 3001 */
+#endif /* VBOX_API_VERSION >= 3001 */
+                } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
+                } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
+                    if (dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
+                        if (dev->data.hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
                         }
                     }
-                    machine->vtbl->SaveSettings(machine);
-                    machine->vtbl->nsisupports.Release((nsISupports *)machine);
                 }
-                data->vboxSession->vtbl->Close(data->vboxSession);
+                machine->vtbl->SaveSettings(machine);
+                VBOX_RELEASE(machine);
             }
+            data->vboxSession->vtbl->Close(data->vboxSession);
         }
     }
 
@@ -4267,14 +4867,13 @@ cleanup:
 }
 
 static int vboxDomainDetachDevice(virDomainPtr dom, const char *xml) {
-    nsresult rc;
-    vboxGlobalData *data = dom->conn->privateData;
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
     IMachine *machine    = NULL;
     vboxIID  *iid        = NULL;
     PRUint32 state       = MachineState_Null;
     virDomainDefPtr def  = NULL;
     virDomainDeviceDefPtr dev  = NULL;
-    int ret = -1;
+    nsresult rc;
 
     if (VIR_ALLOC(def) < 0) {
         virReportOOMError(dom->conn);
@@ -4304,88 +4903,89 @@ static int vboxDomainDetachDevice(virDomainPtr dom, const char *xml) {
 
     vboxIIDFromUUID(dom->uuid, iid);
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
-        if (NS_FAILED(rc)) {
-            vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
-            goto cleanup;
-        }
+    rc = data->vboxObj->vtbl->GetMachine(data->vboxObj, iid, &machine);
+    if (NS_FAILED(rc)) {
+        vboxError(dom->conn, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
+        goto cleanup;
+    }
 
-        if (machine) {
-            machine->vtbl->GetState(machine, &state);
+    if (machine) {
+        machine->vtbl->GetState(machine, &state);
 
-            if ((state == MachineState_Running) ||
-                (state == MachineState_Paused)) {
-                rc = data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
-            } else {
-                rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
-            }
+        if ((state == MachineState_Running) ||
+            (state == MachineState_Paused)) {
+            rc = data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid);
+        } else {
+            rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid);
+        }
 
-            if (NS_SUCCEEDED(rc)) {
-                rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-                if (NS_SUCCEEDED(rc) && machine) {
-                    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
-                        if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
-                            if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                                IDVDDrive *dvdDrive = NULL;
-                                /* Currently CDROM/DVD Drive is always IDE
-                                 * Secondary Master so neglecting the following
-                                 * parameter dev->data.disk->bus
-                                 */
-                                machine->vtbl->GetDVDDrive(machine, &dvdDrive);
-                                if (dvdDrive) {
-                                    rc = dvdDrive->vtbl->Unmount(dvdDrive);
+        if (NS_SUCCEEDED(rc)) {
+            rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
+            if (NS_SUCCEEDED(rc) && machine) {
+                if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
+#if VBOX_API_VERSION < 3001
+                    if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
+                        if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                            IDVDDrive *dvdDrive = NULL;
+                            /* Currently CDROM/DVD Drive is always IDE
+                             * Secondary Master so neglecting the following
+                             * parameter dev->data.disk->bus
+                             */
+                            machine->vtbl->GetDVDDrive(machine, &dvdDrive);
+                            if (dvdDrive) {
+                                rc = dvdDrive->vtbl->Unmount(dvdDrive);
+                                if (NS_FAILED(rc)) {
+                                    vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
+                                              "could not de-attach the mounted ISO",
+                                              (unsigned)rc);
+                                } else {
+                                    ret = 0;
+                                }
+                                VBOX_RELEASE(dvdDrive);
+                            }
+                        } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                        }
+                    } else if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
+                        if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
+                            IFloppyDrive *floppyDrive;
+                            machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
+                            if (floppyDrive) {
+                                PRBool enabled = PR_FALSE;
+
+                                floppyDrive->vtbl->GetEnabled(floppyDrive, &enabled);
+                                if (enabled) {
+                                    rc = floppyDrive->vtbl->Unmount(floppyDrive);
                                     if (NS_FAILED(rc)) {
                                         vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                                                  "could not de-attach the mounted ISO",
+                                                  "could not attach the file to floppy drive",
                                                   (unsigned)rc);
                                     } else {
                                         ret = 0;
                                     }
-                                    dvdDrive->vtbl->nsisupports.Release((nsISupports *)dvdDrive);
-                                }
-                            } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
-                            }
-                        } else if (dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
-                            if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_FILE) {
-                                IFloppyDrive *floppyDrive;
-                                machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
-                                if (floppyDrive) {
-                                    PRBool enabled = PR_FALSE;
-
-                                    floppyDrive->vtbl->GetEnabled(floppyDrive, &enabled);
-                                    if (enabled) {
-                                        rc = floppyDrive->vtbl->Unmount(floppyDrive);
-                                        if (NS_FAILED(rc)) {
-                                            vboxError(dom->conn, VIR_ERR_INTERNAL_ERROR,"%s, rc=%08x",
-                                                      "could not attach the file to floppy drive",
-                                                      (unsigned)rc);
-                                        } else {
-                                            ret = 0;
-                                        }
-                                    } else {
-                                        /* If you are here means floppy drive is already unmounted
-                                         * so don't flag error, just say everything is fine and quit
-                                         */
-                                        ret = 0;
-                                    }
-                                    floppyDrive->vtbl->nsisupports.Release((nsISupports *)floppyDrive);
+                                } else {
+                                    /* If you are here means floppy drive is already unmounted
+                                     * so don't flag error, just say everything is fine and quit
+                                     */
+                                    ret = 0;
                                 }
-                            } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
+                                VBOX_RELEASE(floppyDrive);
                             }
+                        } else if (dev->data.disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
                         }
-                    } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
-                    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
-                        if (dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
-                            if (dev->data.hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
-                            }
+                    }
+#else  /* VBOX_API_VERSION >= 3001 */
+#endif /* VBOX_API_VERSION >= 3001 */
+                } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
+                } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
+                    if (dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) {
+                        if (dev->data.hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
                         }
                     }
-                    machine->vtbl->SaveSettings(machine);
-                    machine->vtbl->nsisupports.Release((nsISupports *)machine);
                 }
-                data->vboxSession->vtbl->Close(data->vboxSession);
+                machine->vtbl->SaveSettings(machine);
+                VBOX_RELEASE(machine);
             }
+            data->vboxSession->vtbl->Close(data->vboxSession);
         }
     }
 
@@ -4508,6 +5108,7 @@ static nsresult vboxCallbackOnExtraDataChange (IVirtualBoxCallback *pThis,
     return NS_OK;
 }
 
+#if VBOX_API_VERSION < 3001
 static nsresult vboxCallbackOnMediaRegistered (IVirtualBoxCallback *pThis,
                                                PRUnichar * mediaId,
                                                PRUint32 mediaType,
@@ -4518,6 +5119,8 @@ static nsresult vboxCallbackOnMediaRegistered (IVirtualBoxCallback *pThis,
 
     return NS_OK;
 }
+#else  /* VBOX_API_VERSION >= 3001 */
+#endif /* VBOX_API_VERSION >= 3001 */
 
 static nsresult vboxCallbackOnMachineRegistered (IVirtualBoxCallback *pThis,
                                                  PRUnichar * machineId,
@@ -4698,7 +5301,10 @@ static IVirtualBoxCallback *vboxAllocCallbackObj (virConnectPtr conn) {
         vboxCallback->vtbl->OnMachineDataChange         = &vboxCallbackOnMachineDataChange;
         vboxCallback->vtbl->OnExtraDataCanChange        = &vboxCallbackOnExtraDataCanChange;
         vboxCallback->vtbl->OnExtraDataChange           = &vboxCallbackOnExtraDataChange;
+#if VBOX_API_VERSION < 3001
         vboxCallback->vtbl->OnMediaRegistered           = &vboxCallbackOnMediaRegistered;
+#else  /* VBOX_API_VERSION >= 3001 */
+#endif /* VBOX_API_VERSION >= 3001 */
         vboxCallback->vtbl->OnMachineRegistered         = &vboxCallbackOnMachineRegistered;
         vboxCallback->vtbl->OnSessionStateChange        = &vboxCallbackOnSessionStateChange;
         vboxCallback->vtbl->OnSnapshotTaken             = &vboxCallbackOnSnapshotTaken;
@@ -4732,53 +5338,50 @@ static int vboxDomainEventRegister (virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freecb) {
-    nsresult rc;
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, int, -1);
     int vboxRet          = -1;
-    int ret              = -1;
+    nsresult rc;
 
     /* Locking has to be there as callbacks are not
      * really fully thread safe
      */
     vboxDriverLock(data);
 
-    if(data->vboxObj) {
-        if (data->vboxCallback == NULL) {
-            data->vboxCallback = vboxAllocCallbackObj(conn);
-            if (data->vboxCallback != NULL) {
-                rc = data->vboxObj->vtbl->RegisterCallback(data->vboxObj, data->vboxCallback);
-                if (NS_SUCCEEDED(rc)) {
-                    vboxRet = 0;
-                }
+    if (data->vboxCallback == NULL) {
+        data->vboxCallback = vboxAllocCallbackObj(conn);
+        if (data->vboxCallback != NULL) {
+            rc = data->vboxObj->vtbl->RegisterCallback(data->vboxObj, data->vboxCallback);
+            if (NS_SUCCEEDED(rc)) {
+                vboxRet = 0;
             }
-        } else {
-            vboxRet = 0;
         }
+    } else {
+        vboxRet = 0;
+    }
 
-        /* Get the vbox file handle and add a event handle to it
-         * so that the events can be passed down to the user
-         */
-        if (vboxRet == 0) {
-            if (data->fdWatch < 0) {
-                PRInt32 vboxFileHandle;
-                vboxFileHandle = data->vboxQueue->vtbl->GetEventQueueSelectFD(data->vboxQueue);
+    /* Get the vbox file handle and add a event handle to it
+     * so that the events can be passed down to the user
+     */
+    if (vboxRet == 0) {
+        if (data->fdWatch < 0) {
+            PRInt32 vboxFileHandle;
+            vboxFileHandle = data->vboxQueue->vtbl->GetEventQueueSelectFD(data->vboxQueue);
 
-                data->fdWatch = virEventAddHandle(vboxFileHandle, VIR_EVENT_HANDLE_READABLE, vboxReadCallback, NULL, NULL);
-            }
+            data->fdWatch = virEventAddHandle(vboxFileHandle, VIR_EVENT_HANDLE_READABLE, vboxReadCallback, NULL, NULL);
+        }
 
-            if (data->fdWatch >= 0) {
-                /* Once a callback is registered with virtualbox, use a list
-                 * to store the callbacks registered with libvirt so that
-                 * later you can iterate over them
-                 */
+        if (data->fdWatch >= 0) {
+            /* Once a callback is registered with virtualbox, use a list
+             * to store the callbacks registered with libvirt so that
+             * later you can iterate over them
+             */
 
-                ret = virDomainEventCallbackListAdd(conn, data->domainEventCallbacks,
-                                                    callback, opaque, freecb);
-                DEBUG("virDomainEventCallbackListAdd (ret = %d) ( conn: %p, "
-                      "data->domainEventCallbacks: %p, callback: %p, opaque: %p, "
-                      "freecb: %p )", ret, conn, data->domainEventCallbacks, callback,
-                      opaque, freecb);
-            }
+            ret = virDomainEventCallbackListAdd(conn, data->domainEventCallbacks,
+                                                callback, opaque, freecb);
+            DEBUG("virDomainEventCallbackListAdd (ret = %d) ( conn: %p, "
+                  "data->domainEventCallbacks: %p, callback: %p, opaque: %p, "
+                  "freecb: %p )", ret, conn, data->domainEventCallbacks, callback,
+                  opaque, freecb);
         }
     }
 
@@ -4796,8 +5399,7 @@ static int vboxDomainEventRegister (virConnectPtr conn,
 
 static int vboxDomainEventDeregister (virConnectPtr conn,
                                       virConnectDomainEventCallback callback) {
-    vboxGlobalData *data = conn->privateData;
-    int ret              = -1;
+    VBOX_OBJECT_CHECK(conn, int, -1);
 
     /* Locking has to be there as callbacks are not
      * really fully thread safe
@@ -4811,7 +5413,7 @@ static int vboxDomainEventDeregister (virConnectPtr conn,
         ret = virDomainEventCallbackListRemove(conn, data->domainEventCallbacks,
                                                callback);
 
-    if(data->vboxObj && data->vboxCallback) {
+    if(data->vboxCallback) {
         /* check count here of how many times register was called
          * and only on the last de-register do the un-register call
          */
@@ -4819,7 +5421,7 @@ static int vboxDomainEventDeregister (virConnectPtr conn,
             int i = 0;
 
             data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback);
-            data->vboxCallback->vtbl->nsisupports.Release((nsISupports *)data->vboxCallback);
+            VBOX_RELEASE(data->vboxCallback);
 
             /* Remove the Event file handle on which we are listening as well */
             virEventRemoveHandle(data->fdWatch);
@@ -4874,214 +5476,169 @@ static int vboxNetworkClose(virConnectPtr conn) {
 }
 
 static int vboxNumOfNetworks(virConnectPtr conn) {
-    vboxGlobalData *data = conn->privateData;
-    int numActive = 0;
+    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
+    PRUint32 networkInterfacesSize = 0;
+    IHostNetworkInterface **networkInterfaces = NULL;
+    int i = 0;
 
-    if (data->vboxObj) {
-        IHost *host = NULL;
-
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            int i = 0;
-            PRUint32 networkInterfacesSize = 0;
-            IHostNetworkInterface **networkInterfaces = NULL;
+    host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
 
-            host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
+    for (i = 0; i < networkInterfacesSize; i++) {
+        if (networkInterfaces[i]) {
+            PRUint32 interfaceType = 0;
 
-            for (i = 0; i < networkInterfacesSize; i++) {
-                if (networkInterfaces[i]) {
-                    PRUint32 interfaceType = 0;
+            networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
+            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
 
-                    networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
-                    if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                        PRUint32 status = HostNetworkInterfaceStatus_Unknown;
-
-                        networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
-
-                        if (status == HostNetworkInterfaceStatus_Up) {
-                            numActive++;
-                        }
-                    }
+                networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
 
-                    networkInterfaces[i]->vtbl->nsisupports.Release((nsISupports *) networkInterfaces[i]);
-                }
+                if (status == HostNetworkInterfaceStatus_Up)
+                    ret++;
             }
 
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+            VBOX_RELEASE(networkInterfaces[i]);
         }
     }
 
-    DEBUG("numActive: %d", numActive);
-    return numActive;
+    VBOX_RELEASE(host);
+
+    DEBUG("numActive: %d", ret);
+    return ret;
 }
 
 static int vboxListNetworks(virConnectPtr conn, char **const names, int nnames) {
-    vboxGlobalData *data = conn->privateData;
-    int numActive = 0;
+    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
+    PRUint32 networkInterfacesSize = 0;
+    IHostNetworkInterface **networkInterfaces = NULL;
+    int i = 0;
 
-    if (data->vboxObj) {
-        IHost *host = NULL;
+    host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
 
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            int i = 0;
-            PRUint32 networkInterfacesSize = 0;
-            IHostNetworkInterface **networkInterfaces = NULL;
-
-            host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
+    for (i = 0; (ret < nnames) && (i < networkInterfacesSize); i++) {
+        if (networkInterfaces[i]) {
+            PRUint32 interfaceType = 0;
 
-            for (i = 0; (numActive < nnames) && (i < networkInterfacesSize); i++) {
-                if (networkInterfaces[i]) {
-                    PRUint32 interfaceType = 0;
+            networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
 
-                    networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
+            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
 
-                    if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                        PRUint32 status = HostNetworkInterfaceStatus_Unknown;
+                networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
 
-                        networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
-
-                        if (status == HostNetworkInterfaceStatus_Up) {
-                            char *nameUtf8       = NULL;
-                            PRUnichar *nameUtf16 = NULL;
+                if (status == HostNetworkInterfaceStatus_Up) {
+                    char *nameUtf8       = NULL;
+                    PRUnichar *nameUtf16 = NULL;
 
-                            networkInterfaces[i]->vtbl->GetName(networkInterfaces[i], &nameUtf16);
-                            data->pFuncs->pfnUtf16ToUtf8(nameUtf16, &nameUtf8);
+                    networkInterfaces[i]->vtbl->GetName(networkInterfaces[i], &nameUtf16);
+                    VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
 
-                            DEBUG("nnames[%d]: %s", numActive, nameUtf8);
-                            names[numActive] = strdup(nameUtf8);
-                            if (names[numActive] == NULL) {
-                                vboxError(conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
-                            } else {
-                                numActive++;
-                            }
-
-                            data->pFuncs->pfnUtf8Free(nameUtf8);
-                            data->pFuncs->pfnUtf16Free(nameUtf16);
-                        }
+                    DEBUG("nnames[%d]: %s", ret, nameUtf8);
+                    names[ret] = strdup(nameUtf8);
+                    if (names[ret] == NULL) {
+                        vboxError(conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
+                    } else {
+                        ret++;
                     }
-                }
-            }
 
-            for (i = 0; i < networkInterfacesSize; i++) {
-                if (networkInterfaces[i]) {
-                    networkInterfaces[i]->vtbl->nsisupports.Release((nsISupports *) networkInterfaces[i]);
+                    VBOX_UTF8_FREE(nameUtf8);
+                    VBOX_UTF16_FREE(nameUtf16);
                 }
             }
-
-            host->vtbl->nsisupports.Release((nsISupports *) host);
         }
     }
 
-    return numActive;
-}
+    for (i = 0; i < networkInterfacesSize; i++)
+        VBOX_RELEASE(networkInterfaces[i]);
 
-static int vboxNumOfDefinedNetworks(virConnectPtr conn) {
-    vboxGlobalData *data = conn->privateData;
-    int numActive = 0;
-
-    if (data->vboxObj) {
-        IHost *host = NULL;
+    VBOX_RELEASE(host);
 
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            int i = 0;
-            PRUint32 networkInterfacesSize = 0;
-            IHostNetworkInterface **networkInterfaces = NULL;
+    return ret;
+}
 
-            host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
+static int vboxNumOfDefinedNetworks(virConnectPtr conn) {
+    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
+    PRUint32 networkInterfacesSize = 0;
+    IHostNetworkInterface **networkInterfaces = NULL;
+    int i = 0;
 
-            for (i = 0; i < networkInterfacesSize; i++) {
-                if (networkInterfaces[i]) {
-                    PRUint32 interfaceType = 0;
+    host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
 
-                    networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
-                    if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                        PRUint32 status = HostNetworkInterfaceStatus_Unknown;
+    for (i = 0; i < networkInterfacesSize; i++) {
+        if (networkInterfaces[i]) {
+            PRUint32 interfaceType = 0;
 
-                        networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
+            networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
+            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
 
-                        if (status == HostNetworkInterfaceStatus_Down) {
-                            numActive++;
-                        }
-                    }
+                networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
 
-                    networkInterfaces[i]->vtbl->nsisupports.Release((nsISupports *) networkInterfaces[i]);
-                }
+                if (status == HostNetworkInterfaceStatus_Down)
+                    ret++;
             }
 
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+            VBOX_RELEASE(networkInterfaces[i]);
         }
     }
 
-    DEBUG("numActive: %d", numActive);
-    return numActive;
+    VBOX_RELEASE(host);
+
+    DEBUG("numActive: %d", ret);
+    return ret;
 }
 
 static int vboxListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
-    vboxGlobalData *data = conn->privateData;
-    int numActive = 0;
-
-    if (data->vboxObj) {
-        IHost *host = NULL;
-
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            int i = 0;
-            PRUint32 networkInterfacesSize = 0;
-            IHostNetworkInterface **networkInterfaces = NULL;
+    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
+    PRUint32 networkInterfacesSize = 0;
+    IHostNetworkInterface **networkInterfaces = NULL;
+    int i = 0;
 
-            host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
+    host->vtbl->GetNetworkInterfaces(host, &networkInterfacesSize, &networkInterfaces);
 
-            for (i = 0; (numActive < nnames) && (i < networkInterfacesSize); i++) {
-                if (networkInterfaces[i]) {
-                    PRUint32 interfaceType = 0;
+    for (i = 0; (ret < nnames) && (i < networkInterfacesSize); i++) {
+        if (networkInterfaces[i]) {
+            PRUint32 interfaceType = 0;
 
-                    networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
+            networkInterfaces[i]->vtbl->GetInterfaceType(networkInterfaces[i], &interfaceType);
 
-                    if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                        PRUint32 status = HostNetworkInterfaceStatus_Unknown;
+            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
 
-                        networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
+                networkInterfaces[i]->vtbl->GetStatus(networkInterfaces[i], &status);
 
-                        if (status == HostNetworkInterfaceStatus_Down) {
-                            char *nameUtf8       = NULL;
-                            PRUnichar *nameUtf16 = NULL;
-
-                            networkInterfaces[i]->vtbl->GetName(networkInterfaces[i], &nameUtf16);
-                            data->pFuncs->pfnUtf16ToUtf8(nameUtf16, &nameUtf8);
+                if (status == HostNetworkInterfaceStatus_Down) {
+                    char *nameUtf8       = NULL;
+                    PRUnichar *nameUtf16 = NULL;
 
-                            DEBUG("nnames[%d]: %s", numActive, nameUtf8);
-                            names[numActive] = strdup(nameUtf8);
-                            if (names[numActive] == NULL) {
-                                vboxError(conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
-                            } else {
-                                numActive++;
-                            }
+                    networkInterfaces[i]->vtbl->GetName(networkInterfaces[i], &nameUtf16);
+                    VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
 
-                            data->pFuncs->pfnUtf8Free(nameUtf8);
-                            data->pFuncs->pfnUtf16Free(nameUtf16);
-                        }
+                    DEBUG("nnames[%d]: %s", ret, nameUtf8);
+                    names[ret] = strdup(nameUtf8);
+                    if (names[ret] == NULL) {
+                        vboxError(conn, VIR_ERR_SYSTEM_ERROR, "%s", "strdup failed");
+                    } else {
+                        ret++;
                     }
-                }
-            }
 
-            for (i = 0; i < networkInterfacesSize; i++) {
-                if (networkInterfaces[i]) {
-                    networkInterfaces[i]->vtbl->nsisupports.Release((nsISupports *) networkInterfaces[i]);
+                    VBOX_UTF8_FREE(nameUtf8);
+                    VBOX_UTF16_FREE(nameUtf16);
                 }
             }
-
-            host->vtbl->nsisupports.Release((nsISupports *) host);
         }
     }
 
-    return numActive;
+    for (i = 0; i < networkInterfacesSize; i++)
+        VBOX_RELEASE(networkInterfaces[i]);
+
+    VBOX_RELEASE(host);
+
+    return ret;
 }
 
 static virNetworkPtr vboxNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid) {
-    vboxGlobalData *data = conn->privateData;
-    virNetworkPtr ret    = NULL;
+    VBOX_OBJECT_HOST_CHECK(conn, virNetworkPtr, NULL);
     vboxIID *iid         = NULL;
 
 #if VBOX_API_VERSION == 2002
@@ -5096,42 +5653,35 @@ static virNetworkPtr vboxNetworkLookupByUUID(virConnectPtr conn, const unsigned
     /* TODO: "internal" networks are just strings and
      * thus can't do much with them
      */
-    if (data->vboxObj) {
-        IHost *host = NULL;
-
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            IHostNetworkInterface *networkInterface = NULL;
-
-            host->vtbl->FindHostNetworkInterfaceById(host, iid, &networkInterface);
-            if (networkInterface) {
-                PRUint32 interfaceType = 0;
-
-                networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
+    IHostNetworkInterface *networkInterface = NULL;
 
-                if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                    char *nameUtf8       = NULL;
-                    PRUnichar *nameUtf16 = NULL;
+    host->vtbl->FindHostNetworkInterfaceById(host, iid, &networkInterface);
+    if (networkInterface) {
+        PRUint32 interfaceType = 0;
 
-                    networkInterface->vtbl->GetName(networkInterface, &nameUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(nameUtf16, &nameUtf8);
+        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
 
-                    ret = virGetNetwork(conn, nameUtf8, uuid);
+        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+            char *nameUtf8       = NULL;
+            PRUnichar *nameUtf16 = NULL;
 
-                    DEBUG("Network Name: %s", nameUtf8);
-                    DEBUGIID("Network UUID", iid);
+            networkInterface->vtbl->GetName(networkInterface, &nameUtf16);
+            VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
 
-                    data->pFuncs->pfnUtf8Free(nameUtf8);
-                    data->pFuncs->pfnUtf16Free(nameUtf16);
-                }
+            ret = virGetNetwork(conn, nameUtf8, uuid);
 
-                networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
-            }
+            DEBUG("Network Name: %s", nameUtf8);
+            DEBUGIID("Network UUID", iid);
 
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+            VBOX_UTF8_FREE(nameUtf8);
+            VBOX_UTF16_FREE(nameUtf16);
         }
+
+        VBOX_RELEASE(networkInterface);
     }
 
+    VBOX_RELEASE(host);
+
 #if VBOX_API_VERSION == 2002
 cleanup:
 #endif
@@ -5140,214 +5690,198 @@ cleanup:
 }
 
 static virNetworkPtr vboxNetworkLookupByName(virConnectPtr conn, const char *name) {
-    vboxGlobalData *data = conn->privateData;
-    virNetworkPtr ret    = NULL;
-
-    if (data->vboxObj) {
-        IHost *host = NULL;
+    VBOX_OBJECT_HOST_CHECK(conn, virNetworkPtr, NULL);
+    PRUnichar *nameUtf16                    = NULL;
+    IHostNetworkInterface *networkInterface = NULL;
 
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            PRUnichar *nameUtf16                    = NULL;
-            IHostNetworkInterface *networkInterface = NULL;
+    VBOX_UTF8_TO_UTF16(name, &nameUtf16);
 
-            data->pFuncs->pfnUtf8ToUtf16(name, &nameUtf16);
+    host->vtbl->FindHostNetworkInterfaceByName(host, nameUtf16, &networkInterface);
 
-            host->vtbl->FindHostNetworkInterfaceByName(host, nameUtf16, &networkInterface);
+    if (networkInterface) {
+        PRUint32 interfaceType = 0;
 
-            if (networkInterface) {
-                PRUint32 interfaceType = 0;
+        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
 
-                networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
+        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+            unsigned char uuid[VIR_UUID_BUFLEN];
+            vboxIID *iid = NULL;
 
-                if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                    unsigned char uuid[VIR_UUID_BUFLEN];
-                    vboxIID *iid = NULL;
+            networkInterface->vtbl->GetId(networkInterface, &iid);
+            vboxIIDToUUID(uuid, iid);
+            ret = virGetNetwork(conn, name, uuid);
+            DEBUG("Network Name: %s", name);
 
-                    networkInterface->vtbl->GetId(networkInterface, &iid);
-                    vboxIIDToUUID(uuid, iid);
-                    ret = virGetNetwork(conn, name, uuid);
-                    DEBUG("Network Name: %s", name);
-
-                    DEBUGIID("Network UUID", iid);
-                    vboxIIDUnalloc(iid);
-                }
-
-                networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
-            }
-
-            data->pFuncs->pfnUtf16Free(nameUtf16);
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+            DEBUGIID("Network UUID", iid);
+            vboxIIDUnalloc(iid);
         }
+
+        VBOX_RELEASE(networkInterface);
     }
 
+    VBOX_UTF16_FREE(nameUtf16);
+    VBOX_RELEASE(host);
+
     return ret;
 }
 
 static virNetworkPtr vboxNetworkDefineCreateXML(virConnectPtr conn, const char *xml, bool start) {
-    vboxGlobalData *data  = conn->privateData;
-    virNetworkDefPtr def  = NULL;
-    virNetworkPtr ret     = NULL;
-
-    if ((def = virNetworkDefParseString(conn, xml)) == NULL)
-        goto cleanup;
+    VBOX_OBJECT_HOST_CHECK(conn, virNetworkPtr, NULL);
+    PRUnichar *networkInterfaceNameUtf16    = NULL;
+    char      *networkInterfaceNameUtf8     = NULL;
+    IHostNetworkInterface *networkInterface = NULL;
 
-    if ((data->vboxObj) && (def->forwardType == VIR_NETWORK_FORWARD_NONE)) {
-        IHost *host = NULL;
+    virNetworkDefPtr def = virNetworkDefParseString(conn, xml);
 
-        /* the current limitation of hostonly network is that you can't
-         * assign a name to it and it defaults to vboxnet*, for e.g:
-         * vboxnet0, vboxnet1, etc. Also the UUID is assigned to it
-         * automatically depending on the mac address and thus both
-         * these paramters are ignore here for now.
-         */
+    if (   (!def)
+        || (def->forwardType != VIR_NETWORK_FORWARD_NONE))
+        goto cleanup;
 
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            PRUnichar *networkInterfaceNameUtf16    = NULL;
-            char      *networkInterfaceNameUtf8     = NULL;
-            IHostNetworkInterface *networkInterface = NULL;
+    /* the current limitation of hostonly network is that you can't
+     * assign a name to it and it defaults to vboxnet*, for e.g:
+     * vboxnet0, vboxnet1, etc. Also the UUID is assigned to it
+     * automatically depending on the mac address and thus both
+     * these paramters are ignored here for now.
+     */
 
 #if VBOX_API_VERSION == 2002
-            if STREQ(def->name, "vboxnet0") {
-                PRUint32 interfaceType = 0;
+    if STREQ(def->name, "vboxnet0") {
+        PRUint32 interfaceType = 0;
 
-                data->pFuncs->pfnUtf8ToUtf16(def->name, &networkInterfaceNameUtf16);
-                host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
+        VBOX_UTF8_TO_UTF16(def->name, &networkInterfaceNameUtf16);
+        host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
 
-                networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
-                if (interfaceType != HostNetworkInterfaceType_HostOnly) {
-                    networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
-                    networkInterface = NULL;
-                }
-            }
+        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
+        if (interfaceType != HostNetworkInterfaceType_HostOnly) {
+            VBOX_RELEASE(networkInterface);
+            networkInterface = NULL;
+        }
+    }
 #else /* VBOX_API_VERSION != 2002 */
-            IProgress *progress = NULL;
-            host->vtbl->CreateHostOnlyNetworkInterface(host, &networkInterface, &progress);
+    IProgress *progress = NULL;
+    host->vtbl->CreateHostOnlyNetworkInterface(host, &networkInterface, &progress);
 
-            if (progress) {
-                progress->vtbl->WaitForCompletion(progress, -1);
-                progress->vtbl->nsisupports.Release((nsISupports *)progress);
-            }
+    if (progress) {
+        progress->vtbl->WaitForCompletion(progress, -1);
+        VBOX_RELEASE(progress);
+    }
 #endif /* VBOX_API_VERSION != 2002 */
 
-            if (networkInterface) {
-                unsigned char uuid[VIR_UUID_BUFLEN];
-                char      *networkNameUtf8  = NULL;
-                PRUnichar *networkNameUtf16 = NULL;
-                vboxIID   *vboxnetiid       = NULL;
-
-                networkInterface->vtbl->GetName(networkInterface, &networkInterfaceNameUtf16);
-                if (networkInterfaceNameUtf16) {
-                    data->pFuncs->pfnUtf16ToUtf8(networkInterfaceNameUtf16, &networkInterfaceNameUtf8);
-
-                    if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", networkInterfaceNameUtf8) < 0) {
-                        host->vtbl->nsisupports.Release((nsISupports *)host);
-                        networkInterface->vtbl->nsisupports.Release((nsISupports *)networkInterface);
-                        virReportOOMError(conn);
-                        goto cleanup;
-                    }
-                }
-
-                data->pFuncs->pfnUtf8ToUtf16(networkNameUtf8 , &networkNameUtf16);
-
-                /* Currently support only one dhcp server per network
-                 * with contigious address space from start to end
-                 */
-                if ((def->nranges >= 1) &&
-                    (def->ranges[0].start) &&
-                    (def->ranges[0].end)) {
-                    IDHCPServer *dhcpServer = NULL;
-
-                    data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
-                                                                     networkNameUtf16,
-                                                                     &dhcpServer);
-                    if (!dhcpServer) {
-                        /* create a dhcp server */
-                        data->vboxObj->vtbl->CreateDHCPServer(data->vboxObj,
-                                                              networkNameUtf16,
-                                                              &dhcpServer);
-                        DEBUG0("couldn't find dhcp server so creating one");
-                    }
-                    if (dhcpServer) {
-                        PRUnichar *ipAddressUtf16     = NULL;
-                        PRUnichar *networkMaskUtf16   = NULL;
-                        PRUnichar *fromIPAddressUtf16 = NULL;
-                        PRUnichar *toIPAddressUtf16   = NULL;
-                        PRUnichar *trunkTypeUtf16     = NULL;
-
-
-                        data->pFuncs->pfnUtf8ToUtf16(def->ipAddress, &ipAddressUtf16);
-                        data->pFuncs->pfnUtf8ToUtf16(def->netmask, &networkMaskUtf16);
-                        data->pFuncs->pfnUtf8ToUtf16(def->ranges[0].start, &fromIPAddressUtf16);
-                        data->pFuncs->pfnUtf8ToUtf16(def->ranges[0].end, &toIPAddressUtf16);
-                        data->pFuncs->pfnUtf8ToUtf16("netflt", &trunkTypeUtf16);
-
-                        dhcpServer->vtbl->SetEnabled(dhcpServer, PR_TRUE);
-
-                        dhcpServer->vtbl->SetConfiguration(dhcpServer,
-                                                           ipAddressUtf16,
-                                                           networkMaskUtf16,
-                                                           fromIPAddressUtf16,
-                                                           toIPAddressUtf16);
-
-                        if (start)
-                            dhcpServer->vtbl->Start(dhcpServer,
-                                                    networkNameUtf16,
-                                                    networkInterfaceNameUtf16,
-                                                    trunkTypeUtf16);
-
-                        data->pFuncs->pfnUtf16Free(ipAddressUtf16);
-                        data->pFuncs->pfnUtf16Free(networkMaskUtf16);
-                        data->pFuncs->pfnUtf16Free(fromIPAddressUtf16);
-                        data->pFuncs->pfnUtf16Free(toIPAddressUtf16);
-                        data->pFuncs->pfnUtf16Free(trunkTypeUtf16);
-                        dhcpServer->vtbl->nsisupports.Release((nsISupports *) dhcpServer);
-                    }
-                }
+    if (networkInterface) {
+        unsigned char uuid[VIR_UUID_BUFLEN];
+        char      *networkNameUtf8  = NULL;
+        PRUnichar *networkNameUtf16 = NULL;
+        vboxIID   *vboxnetiid       = NULL;
+
+        networkInterface->vtbl->GetName(networkInterface, &networkInterfaceNameUtf16);
+        if (networkInterfaceNameUtf16) {
+            VBOX_UTF16_TO_UTF8(networkInterfaceNameUtf16, &networkInterfaceNameUtf8);
+
+            if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", networkInterfaceNameUtf8) < 0) {
+                VBOX_RELEASE(host);
+                VBOX_RELEASE(networkInterface);
+                virReportOOMError(conn);
+                goto cleanup;
+            }
+        }
 
-                if ((def->nhosts >= 1) &&
-                    (def->hosts[0].ip)) {
-                    PRUnichar *ipAddressUtf16   = NULL;
-                    PRUnichar *networkMaskUtf16 = NULL;
+        VBOX_UTF8_TO_UTF16(networkNameUtf8 , &networkNameUtf16);
 
-                    data->pFuncs->pfnUtf8ToUtf16(def->netmask, &networkMaskUtf16);
-                    data->pFuncs->pfnUtf8ToUtf16(def->hosts[0].ip, &ipAddressUtf16);
+        /* Currently support only one dhcp server per network
+         * with contigious address space from start to end
+         */
+        if ((def->nranges >= 1) &&
+            (def->ranges[0].start) &&
+            (def->ranges[0].end)) {
+            IDHCPServer *dhcpServer = NULL;
+
+            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
+                                                             networkNameUtf16,
+                                                             &dhcpServer);
+            if (!dhcpServer) {
+                /* create a dhcp server */
+                data->vboxObj->vtbl->CreateDHCPServer(data->vboxObj,
+                                                      networkNameUtf16,
+                                                      &dhcpServer);
+                DEBUG0("couldn't find dhcp server so creating one");
+            }
+            if (dhcpServer) {
+                PRUnichar *ipAddressUtf16     = NULL;
+                PRUnichar *networkMaskUtf16   = NULL;
+                PRUnichar *fromIPAddressUtf16 = NULL;
+                PRUnichar *toIPAddressUtf16   = NULL;
+                PRUnichar *trunkTypeUtf16     = NULL;
+
+
+                VBOX_UTF8_TO_UTF16(def->ipAddress, &ipAddressUtf16);
+                VBOX_UTF8_TO_UTF16(def->netmask, &networkMaskUtf16);
+                VBOX_UTF8_TO_UTF16(def->ranges[0].start, &fromIPAddressUtf16);
+                VBOX_UTF8_TO_UTF16(def->ranges[0].end, &toIPAddressUtf16);
+                VBOX_UTF8_TO_UTF16("netflt", &trunkTypeUtf16);
+
+                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_TRUE);
+
+                dhcpServer->vtbl->SetConfiguration(dhcpServer,
+                                                   ipAddressUtf16,
+                                                   networkMaskUtf16,
+                                                   fromIPAddressUtf16,
+                                                   toIPAddressUtf16);
+
+                if (start)
+                    dhcpServer->vtbl->Start(dhcpServer,
+                                            networkNameUtf16,
+                                            networkInterfaceNameUtf16,
+                                            trunkTypeUtf16);
+
+                VBOX_UTF16_FREE(ipAddressUtf16);
+                VBOX_UTF16_FREE(networkMaskUtf16);
+                VBOX_UTF16_FREE(fromIPAddressUtf16);
+                VBOX_UTF16_FREE(toIPAddressUtf16);
+                VBOX_UTF16_FREE(trunkTypeUtf16);
+                VBOX_RELEASE(dhcpServer);
+            }
+        }
 
-                    /* Current drawback is that since EnableStaticIpConfig() sets
-                     * IP and enables the interface so even if the dhcpserver is not
-                     * started the interface is still up and running
-                     */
-                    networkInterface->vtbl->EnableStaticIpConfig(networkInterface,
-                                                                 ipAddressUtf16,
-                                                                 networkMaskUtf16);
+        if ((def->nhosts >= 1) &&
+            (def->hosts[0].ip)) {
+            PRUnichar *ipAddressUtf16   = NULL;
+            PRUnichar *networkMaskUtf16 = NULL;
 
-                    data->pFuncs->pfnUtf16Free(ipAddressUtf16);
-                    data->pFuncs->pfnUtf16Free(networkMaskUtf16);
-                } else {
-                    networkInterface->vtbl->EnableDynamicIpConfig(networkInterface);
-                    networkInterface->vtbl->DhcpRediscover(networkInterface);
-                }
+            VBOX_UTF8_TO_UTF16(def->netmask, &networkMaskUtf16);
+            VBOX_UTF8_TO_UTF16(def->hosts[0].ip, &ipAddressUtf16);
 
-                networkInterface->vtbl->GetId(networkInterface, &vboxnetiid);
-                if (vboxnetiid) {
-                    vboxIIDToUUID(uuid, vboxnetiid);
-                    DEBUGIID("Real Network UUID", vboxnetiid);
-                    vboxIIDUnalloc(vboxnetiid);
-                    ret = virGetNetwork(conn, networkInterfaceNameUtf8, uuid);
-                }
+            /* Current drawback is that since EnableStaticIpConfig() sets
+             * IP and enables the interface so even if the dhcpserver is not
+             * started the interface is still up and running
+             */
+            networkInterface->vtbl->EnableStaticIpConfig(networkInterface,
+                                                         ipAddressUtf16,
+                                                         networkMaskUtf16);
 
-                VIR_FREE(networkNameUtf8);
-                data->pFuncs->pfnUtf16Free(networkNameUtf16);
-                networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
-            }
+            VBOX_UTF16_FREE(ipAddressUtf16);
+            VBOX_UTF16_FREE(networkMaskUtf16);
+        } else {
+            networkInterface->vtbl->EnableDynamicIpConfig(networkInterface);
+            networkInterface->vtbl->DhcpRediscover(networkInterface);
+        }
 
-            data->pFuncs->pfnUtf8Free(networkInterfaceNameUtf8);
-            data->pFuncs->pfnUtf16Free(networkInterfaceNameUtf16);
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+        networkInterface->vtbl->GetId(networkInterface, &vboxnetiid);
+        if (vboxnetiid) {
+            vboxIIDToUUID(uuid, vboxnetiid);
+            DEBUGIID("Real Network UUID", vboxnetiid);
+            vboxIIDUnalloc(vboxnetiid);
+            ret = virGetNetwork(conn, networkInterfaceNameUtf8, uuid);
         }
+
+        VIR_FREE(networkNameUtf8);
+        VBOX_UTF16_FREE(networkNameUtf16);
+        VBOX_RELEASE(networkInterface);
     }
 
+    VBOX_UTF8_FREE(networkInterfaceNameUtf8);
+    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
+    VBOX_RELEASE(host);
+
 cleanup:
     virNetworkDefFree(def);
     return ret;
@@ -5362,9 +5896,8 @@ static virNetworkPtr vboxNetworkDefineXML(virConnectPtr conn, const char *xml) {
 }
 
 static int vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterface) {
-    vboxGlobalData *data  = network->conn->privateData;
+    VBOX_OBJECT_HOST_CHECK(network->conn, int, -1);
     char *networkNameUtf8 = NULL;
-    int ret = -1;
 
     /* Current limitation of the function for VirtualBox 2.2.* is
      * that you can't delete the default hostonly adaptor namely:
@@ -5379,77 +5912,69 @@ static int vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterfac
         goto cleanup;
     }
 
-    if (data->vboxObj) {
-        IHost *host = NULL;
-
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            PRUnichar *networkInterfaceNameUtf16    = NULL;
-            IHostNetworkInterface *networkInterface = NULL;
+    PRUnichar *networkInterfaceNameUtf16    = NULL;
+    IHostNetworkInterface *networkInterface = NULL;
 
-            data->pFuncs->pfnUtf8ToUtf16(network->name, &networkInterfaceNameUtf16);
+    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
 
-            host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
+    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
 
-            if (networkInterface) {
-                PRUint32 interfaceType = 0;
+    if (networkInterface) {
+        PRUint32 interfaceType = 0;
 
-                networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
+        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
 
-                if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                    PRUnichar *networkNameUtf16 = NULL;
-                    IDHCPServer *dhcpServer     = NULL;
+        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+            PRUnichar *networkNameUtf16 = NULL;
+            IDHCPServer *dhcpServer     = NULL;
 
 #if VBOX_API_VERSION != 2002
-                    if (removeinterface) {
-                        PRUnichar *iidUtf16 = NULL;
-                        IProgress *progress = NULL;
+            if (removeinterface) {
+                PRUnichar *iidUtf16 = NULL;
+                IProgress *progress = NULL;
 
-                        networkInterface->vtbl->GetId(networkInterface, &iidUtf16);
+                networkInterface->vtbl->GetId(networkInterface, &iidUtf16);
 
-                        if (iidUtf16) {
+                if (iidUtf16) {
 #if VBOX_API_VERSION == 3000
-                            IHostNetworkInterface *netInt = NULL;
-                            host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &netInt, &progress);
-                            if (netInt)
-                                netInt->vtbl->nsisupports.Release((nsISupports *) netInt);
+                    IHostNetworkInterface *netInt = NULL;
+                    host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &netInt, &progress);
+                    VBOX_RELEASE(netInt);
 #else  /* VBOX_API_VERSION > 3000 */
-                            host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &progress);
+                    host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &progress);
 #endif /* VBOX_API_VERSION > 3000 */
-                            data->pFuncs->pfnUtf16Free(iidUtf16);
-                        }
+                    VBOX_UTF16_FREE(iidUtf16);
+                }
 
-                        if (progress) {
-                            progress->vtbl->WaitForCompletion(progress, -1);
-                            progress->vtbl->nsisupports.Release((nsISupports *)progress);
-                        }
-                    }
+                if (progress) {
+                    progress->vtbl->WaitForCompletion(progress, -1);
+                    VBOX_RELEASE(progress);
+                }
+            }
 #endif /* VBOX_API_VERSION != 2002 */
 
-                    data->pFuncs->pfnUtf8ToUtf16(networkNameUtf8 , &networkNameUtf16);
-
-                    data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
-                                                                     networkNameUtf16,
-                                                                     &dhcpServer);
-                    if (dhcpServer) {
-                        dhcpServer->vtbl->SetEnabled(dhcpServer, PR_FALSE);
-                        dhcpServer->vtbl->Stop(dhcpServer);
-                        if (removeinterface)
-                            data->vboxObj->vtbl->RemoveDHCPServer(data->vboxObj, dhcpServer);
-                        dhcpServer->vtbl->nsisupports.Release((nsISupports *) dhcpServer);
-                    }
-
-                    data->pFuncs->pfnUtf16Free(networkNameUtf16);
-
-                }
-                networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
+            VBOX_UTF8_TO_UTF16(networkNameUtf8 , &networkNameUtf16);
+
+            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
+                                                             networkNameUtf16,
+                                                             &dhcpServer);
+            if (dhcpServer) {
+                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_FALSE);
+                dhcpServer->vtbl->Stop(dhcpServer);
+                if (removeinterface)
+                    data->vboxObj->vtbl->RemoveDHCPServer(data->vboxObj, dhcpServer);
+                VBOX_RELEASE(dhcpServer);
             }
 
-            data->pFuncs->pfnUtf16Free(networkInterfaceNameUtf16);
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+            VBOX_UTF16_FREE(networkNameUtf16);
+
         }
+        VBOX_RELEASE(networkInterface);
     }
 
+    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
+    VBOX_RELEASE(host);
+
     ret = 0;
 
 cleanup:
@@ -5462,9 +5987,8 @@ static int vboxNetworkUndefine(virNetworkPtr network) {
 }
 
 static int vboxNetworkCreate(virNetworkPtr network) {
-    vboxGlobalData *data  = network->conn->privateData;
+    VBOX_OBJECT_HOST_CHECK(network->conn, int, -1);
     char *networkNameUtf8 = NULL;
-    int ret = -1;
 
     /* Current limitation of the function for VirtualBox 2.2.* is
      * that the default hostonly network "vboxnet0" is always active
@@ -5478,60 +6002,53 @@ static int vboxNetworkCreate(virNetworkPtr network) {
         goto cleanup;
     }
 
-    if (data->vboxObj) {
-        IHost *host = NULL;
-
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            PRUnichar *networkInterfaceNameUtf16    = NULL;
-            IHostNetworkInterface *networkInterface = NULL;
-
-            data->pFuncs->pfnUtf8ToUtf16(network->name, &networkInterfaceNameUtf16);
-
-            host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
+    PRUnichar *networkInterfaceNameUtf16    = NULL;
+    IHostNetworkInterface *networkInterface = NULL;
 
-            if (networkInterface) {
-                PRUint32 interfaceType = 0;
+    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
 
-                networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
+    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
 
-                if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                    PRUnichar *networkNameUtf16 = NULL;
-                    IDHCPServer *dhcpServer     = NULL;
+    if (networkInterface) {
+        PRUint32 interfaceType = 0;
 
+        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
 
-                    data->pFuncs->pfnUtf8ToUtf16(networkNameUtf8 , &networkNameUtf16);
+        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+            PRUnichar *networkNameUtf16 = NULL;
+            IDHCPServer *dhcpServer     = NULL;
 
-                    data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
-                                                                     networkNameUtf16,
-                                                                     &dhcpServer);
-                    if (dhcpServer) {
-                        PRUnichar *trunkTypeUtf16 = NULL;
 
-                        dhcpServer->vtbl->SetEnabled(dhcpServer, PR_TRUE);
+            VBOX_UTF8_TO_UTF16(networkNameUtf8 , &networkNameUtf16);
 
-                        data->pFuncs->pfnUtf8ToUtf16("netflt", &trunkTypeUtf16);
+            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
+                                                             networkNameUtf16,
+                                                             &dhcpServer);
+            if (dhcpServer) {
+                PRUnichar *trunkTypeUtf16 = NULL;
 
-                        dhcpServer->vtbl->Start(dhcpServer,
-                                                networkNameUtf16,
-                                                networkInterfaceNameUtf16,
-                                                trunkTypeUtf16);
+                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_TRUE);
 
-                        data->pFuncs->pfnUtf16Free(trunkTypeUtf16);
-                        dhcpServer->vtbl->nsisupports.Release((nsISupports *) dhcpServer);
-                    }
+                VBOX_UTF8_TO_UTF16("netflt", &trunkTypeUtf16);
 
-                    data->pFuncs->pfnUtf16Free(networkNameUtf16);
-                }
+                dhcpServer->vtbl->Start(dhcpServer,
+                                        networkNameUtf16,
+                                        networkInterfaceNameUtf16,
+                                        trunkTypeUtf16);
 
-                networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
+                VBOX_UTF16_FREE(trunkTypeUtf16);
+                VBOX_RELEASE(dhcpServer);
             }
 
-            data->pFuncs->pfnUtf16Free(networkInterfaceNameUtf16);
-            host->vtbl->nsisupports.Release((nsISupports *) host);
+            VBOX_UTF16_FREE(networkNameUtf16);
         }
+
+        VBOX_RELEASE(networkInterface);
     }
 
+    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
+    VBOX_RELEASE(host);
+
     ret = 0;
 
 cleanup:
@@ -5544,9 +6061,8 @@ static int vboxNetworkDestroy(virNetworkPtr network) {
 }
 
 static char *vboxNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
-    vboxGlobalData *data  = network->conn->privateData;
+    VBOX_OBJECT_HOST_CHECK(network->conn, char *, NULL);
     virNetworkDefPtr def  = NULL;
-    char *ret             = NULL;
     char *networkNameUtf8 = NULL;
 
     if (VIR_ALLOC(def) < 0) {
@@ -5559,127 +6075,120 @@ static char *vboxNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSE
         goto cleanup;
     }
 
-    if (data->vboxObj) {
-        IHost *host = NULL;
+    PRUnichar *networkInterfaceNameUtf16    = NULL;
+    IHostNetworkInterface *networkInterface = NULL;
 
-        data->vboxObj->vtbl->GetHost(data->vboxObj, &host);
-        if (host) {
-            PRUnichar *networkInterfaceNameUtf16    = NULL;
-            IHostNetworkInterface *networkInterface = NULL;
+    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
 
-            data->pFuncs->pfnUtf8ToUtf16(network->name, &networkInterfaceNameUtf16);
+    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
 
-            host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
+    if (networkInterface) {
+        PRUint32 interfaceType = 0;
 
-            if (networkInterface) {
-                PRUint32 interfaceType = 0;
+        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
 
-                networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
-
-                if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-                    def->name = strdup(network->name);
-                    if (def->name != NULL) {
-                        PRUnichar *networkNameUtf16 = NULL;
-                        IDHCPServer *dhcpServer     = NULL;
-                        vboxIID *vboxnet0IID        = NULL;
-
-                        networkInterface->vtbl->GetId(networkInterface, &vboxnet0IID);
-                        vboxIIDToUUID(def->uuid, vboxnet0IID);
-
-                        data->pFuncs->pfnUtf8ToUtf16(networkNameUtf8 , &networkNameUtf16);
-
-                        def->forwardType = VIR_NETWORK_FORWARD_NONE;
-
-                        data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
-                                                                         networkNameUtf16,
-                                                                         &dhcpServer);
-                        if (dhcpServer) {
-                            def->nranges = 1;
-                            if (VIR_ALLOC_N(def->ranges, def->nranges) >=0 ) {
-                                PRUnichar *ipAddressUtf16     = NULL;
-                                PRUnichar *networkMaskUtf16   = NULL;
-                                PRUnichar *fromIPAddressUtf16 = NULL;
-                                PRUnichar *toIPAddressUtf16   = NULL;
+        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
+            def->name = strdup(network->name);
+            if (def->name != NULL) {
+                PRUnichar *networkNameUtf16 = NULL;
+                IDHCPServer *dhcpServer     = NULL;
+                vboxIID *vboxnet0IID        = NULL;
 
-                                dhcpServer->vtbl->GetIPAddress(dhcpServer, &ipAddressUtf16);
-                                dhcpServer->vtbl->GetNetworkMask(dhcpServer, &networkMaskUtf16);
-                                dhcpServer->vtbl->GetLowerIP(dhcpServer, &fromIPAddressUtf16);
-                                dhcpServer->vtbl->GetUpperIP(dhcpServer, &toIPAddressUtf16);
-                                /* Currently virtualbox supports only one dhcp server per network
-                                 * with contigious address space from start to end
-                                 */
-                                data->pFuncs->pfnUtf16ToUtf8(ipAddressUtf16, &def->ipAddress);
-                                data->pFuncs->pfnUtf16ToUtf8(networkMaskUtf16, &def->netmask);
-                                data->pFuncs->pfnUtf16ToUtf8(fromIPAddressUtf16, &def->ranges[0].start);
-                                data->pFuncs->pfnUtf16ToUtf8(toIPAddressUtf16, &def->ranges[0].end);
-
-                                data->pFuncs->pfnUtf16Free(ipAddressUtf16);
-                                data->pFuncs->pfnUtf16Free(networkMaskUtf16);
-                                data->pFuncs->pfnUtf16Free(fromIPAddressUtf16);
-                                data->pFuncs->pfnUtf16Free(toIPAddressUtf16);
-                            } else {
-                                def->nranges = 0;
-                                virReportOOMError(network->conn);
-                            }
+                networkInterface->vtbl->GetId(networkInterface, &vboxnet0IID);
+                vboxIIDToUUID(def->uuid, vboxnet0IID);
 
-                            def->nhosts = 1;
-                            if (VIR_ALLOC_N(def->hosts, def->nhosts) >=0 ) {
-                                def->hosts[0].name = strdup(network->name);
-                                if (def->hosts[0].name == NULL) {
-                                    VIR_FREE(def->hosts);
-                                    def->nhosts = 0;
-                                    vboxError(network->conn,
-                                              VIR_ERR_SYSTEM_ERROR,
-                                              "%s", "strdup failed");
-                                } else {
-                                    PRUnichar *macAddressUtf16 = NULL;
-                                    PRUnichar *ipAddressUtf16  = NULL;
+                VBOX_UTF8_TO_UTF16(networkNameUtf8 , &networkNameUtf16);
 
-                                    networkInterface->vtbl->GetHardwareAddress(networkInterface, &macAddressUtf16);
-                                    networkInterface->vtbl->GetIPAddress(networkInterface, &ipAddressUtf16);
+                def->forwardType = VIR_NETWORK_FORWARD_NONE;
 
-                                    data->pFuncs->pfnUtf16ToUtf8(macAddressUtf16, &def->hosts[0].mac);
-                                    data->pFuncs->pfnUtf16ToUtf8(ipAddressUtf16, &def->hosts[0].ip);
+                data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
+                                                                 networkNameUtf16,
+                                                                 &dhcpServer);
+                if (dhcpServer) {
+                    def->nranges = 1;
+                    if (VIR_ALLOC_N(def->ranges, def->nranges) >=0 ) {
+                        PRUnichar *ipAddressUtf16     = NULL;
+                        PRUnichar *networkMaskUtf16   = NULL;
+                        PRUnichar *fromIPAddressUtf16 = NULL;
+                        PRUnichar *toIPAddressUtf16   = NULL;
 
-                                    data->pFuncs->pfnUtf16Free(macAddressUtf16);
-                                    data->pFuncs->pfnUtf16Free(ipAddressUtf16);
-                                }
-                            } else {
-                                def->nhosts = 0;
-                            }
+                        dhcpServer->vtbl->GetIPAddress(dhcpServer, &ipAddressUtf16);
+                        dhcpServer->vtbl->GetNetworkMask(dhcpServer, &networkMaskUtf16);
+                        dhcpServer->vtbl->GetLowerIP(dhcpServer, &fromIPAddressUtf16);
+                        dhcpServer->vtbl->GetUpperIP(dhcpServer, &toIPAddressUtf16);
+                        /* Currently virtualbox supports only one dhcp server per network
+                         * with contigious address space from start to end
+                         */
+                        VBOX_UTF16_TO_UTF8(ipAddressUtf16, &def->ipAddress);
+                        VBOX_UTF16_TO_UTF8(networkMaskUtf16, &def->netmask);
+                        VBOX_UTF16_TO_UTF8(fromIPAddressUtf16, &def->ranges[0].start);
+                        VBOX_UTF16_TO_UTF8(toIPAddressUtf16, &def->ranges[0].end);
+
+                        VBOX_UTF16_FREE(ipAddressUtf16);
+                        VBOX_UTF16_FREE(networkMaskUtf16);
+                        VBOX_UTF16_FREE(fromIPAddressUtf16);
+                        VBOX_UTF16_FREE(toIPAddressUtf16);
+                    } else {
+                        def->nranges = 0;
+                        virReportOOMError(network->conn);
+                    }
 
-                            dhcpServer->vtbl->nsisupports.Release((nsISupports *) dhcpServer);
+                    def->nhosts = 1;
+                    if (VIR_ALLOC_N(def->hosts, def->nhosts) >=0 ) {
+                        def->hosts[0].name = strdup(network->name);
+                        if (def->hosts[0].name == NULL) {
+                            VIR_FREE(def->hosts);
+                            def->nhosts = 0;
+                            vboxError(network->conn,
+                                      VIR_ERR_SYSTEM_ERROR,
+                                      "%s", "strdup failed");
                         } else {
-                            PRUnichar *networkMaskUtf16 = NULL;
-                            PRUnichar *ipAddressUtf16   = NULL;
+                            PRUnichar *macAddressUtf16 = NULL;
+                            PRUnichar *ipAddressUtf16  = NULL;
 
-                            networkInterface->vtbl->GetNetworkMask(networkInterface, &networkMaskUtf16);
+                            networkInterface->vtbl->GetHardwareAddress(networkInterface, &macAddressUtf16);
                             networkInterface->vtbl->GetIPAddress(networkInterface, &ipAddressUtf16);
 
-                            data->pFuncs->pfnUtf16ToUtf8(networkMaskUtf16, &def->netmask);
-                            data->pFuncs->pfnUtf16ToUtf8(ipAddressUtf16, &def->ipAddress);
+                            VBOX_UTF16_TO_UTF8(macAddressUtf16, &def->hosts[0].mac);
+                            VBOX_UTF16_TO_UTF8(ipAddressUtf16, &def->hosts[0].ip);
 
-                            data->pFuncs->pfnUtf16Free(networkMaskUtf16);
-                            data->pFuncs->pfnUtf16Free(ipAddressUtf16);
+                            VBOX_UTF16_FREE(macAddressUtf16);
+                            VBOX_UTF16_FREE(ipAddressUtf16);
                         }
-
-                        DEBUGIID("Network UUID", vboxnet0IID);
-                        vboxIIDUnalloc(vboxnet0IID);
-                        data->pFuncs->pfnUtf16Free(networkNameUtf16);
                     } else {
-                        vboxError(network->conn, VIR_ERR_SYSTEM_ERROR,
-                                   "%s", "strdup failed");
+                        def->nhosts = 0;
                     }
+
+                    VBOX_RELEASE(dhcpServer);
+                } else {
+                    PRUnichar *networkMaskUtf16 = NULL;
+                    PRUnichar *ipAddressUtf16   = NULL;
+
+                    networkInterface->vtbl->GetNetworkMask(networkInterface, &networkMaskUtf16);
+                    networkInterface->vtbl->GetIPAddress(networkInterface, &ipAddressUtf16);
+
+                    VBOX_UTF16_TO_UTF8(networkMaskUtf16, &def->netmask);
+                    VBOX_UTF16_TO_UTF8(ipAddressUtf16, &def->ipAddress);
+
+                    VBOX_UTF16_FREE(networkMaskUtf16);
+                    VBOX_UTF16_FREE(ipAddressUtf16);
                 }
 
-                networkInterface->vtbl->nsisupports.Release((nsISupports *) networkInterface);
+                DEBUGIID("Network UUID", vboxnet0IID);
+                vboxIIDUnalloc(vboxnet0IID);
+                VBOX_UTF16_FREE(networkNameUtf16);
+            } else {
+                vboxError(network->conn, VIR_ERR_SYSTEM_ERROR,
+                           "%s", "strdup failed");
             }
-
-            data->pFuncs->pfnUtf16Free(networkInterfaceNameUtf16);
-            host->vtbl->nsisupports.Release((nsISupports *) host);
         }
+
+        VBOX_RELEASE(networkInterface);
     }
 
+    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
+    VBOX_RELEASE(host);
+
     ret = virNetworkDefFormat(network->conn, def);
 
 cleanup:
@@ -5761,309 +6270,311 @@ static virStoragePoolPtr vboxStoragePoolLookupByName(virConnectPtr conn, const c
 }
 
 static int vboxStoragePoolNumOfVolumes(virStoragePoolPtr pool) {
-    vboxGlobalData *data        = pool->conn->privateData;
+    VBOX_OBJECT_CHECK(pool->conn, int, -1);
     IHardDisk **hardDisks       = NULL;
     PRUint32 hardDiskCount      = 0;
     PRUint32 hardDiskAccessible = 0;
     nsresult rc;
     int i;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetHardDisks(data->vboxObj, &hardDiskCount, &hardDisks);
-        if (NS_SUCCEEDED(rc)) {
-            for (i = 0; i < hardDiskCount; ++i) {
-                IHardDisk *hardDisk = hardDisks[i];
-                if (hardDisk) {
-                    PRUint32 hddstate;
+    rc = data->vboxObj->vtbl->GetHardDisks(data->vboxObj, &hardDiskCount, &hardDisks);
+    if (NS_SUCCEEDED(rc)) {
+        for (i = 0; i < hardDiskCount; ++i) {
+            IHardDisk *hardDisk = hardDisks[i];
+            if (hardDisk) {
+                PRUint32 hddstate;
 
-                    hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                    if (hddstate != MediaState_Inaccessible)
-                        hardDiskAccessible++;
+                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+                if (hddstate != MediaState_Inaccessible)
+                    hardDiskAccessible++;
 
-                    hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
-                }
+                VBOX_MEDIUM_RELEASE(hardDisk);
             }
-            hardDiskCount = 0;
-        } else {
-            hardDiskCount = -1;
-            vboxError(pool->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                      "could not get number of volumes in the pool",
-                      pool->name, (unsigned)rc);
         }
+        hardDiskCount = 0;
+    } else {
+        hardDiskCount = -1;
+        vboxError(pool->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                  "could not get number of volumes in the pool",
+                  pool->name, (unsigned)rc);
     }
 
     if (hardDiskAccessible)
-        return hardDiskAccessible;
+        ret = hardDiskAccessible;
     else
-        return hardDiskCount;
+        ret = hardDiskCount;
+
+    return ret;
 }
 
 static int vboxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names, int nnames) {
-    vboxGlobalData *data   = pool->conn->privateData;
+    VBOX_OBJECT_CHECK(pool->conn, int, -1);
     IHardDisk **hardDisks  = NULL;
     PRUint32 hardDiskCount = 0;
     PRUint32 numActive     = 0;
     nsresult rc;
     int i;
 
-    if(data->vboxObj) {
-        rc = data->vboxObj->vtbl->GetHardDisks(data->vboxObj, &hardDiskCount, &hardDisks);
-        if (NS_SUCCEEDED(rc)) {
-            for (i = 0; i < hardDiskCount && numActive < nnames; ++i) {
-                IHardDisk *hardDisk = hardDisks[i];
-
-                if (hardDisk) {
-                    PRUint32 hddstate;
+    rc = data->vboxObj->vtbl->GetHardDisks(data->vboxObj, &hardDiskCount, &hardDisks);
+    if (NS_SUCCEEDED(rc)) {
+        for (i = 0; i < hardDiskCount && numActive < nnames; ++i) {
+            IHardDisk *hardDisk = hardDisks[i];
 
-                    hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                    if (hddstate != MediaState_Inaccessible) {
-                        char      *nameUtf8  = NULL;
-                        PRUnichar *nameUtf16 = NULL;
+            if (hardDisk) {
+                PRUint32 hddstate;
+                char      *nameUtf8  = NULL;
+                PRUnichar *nameUtf16 = NULL;
 
-                        hardDisk->vtbl->imedium.GetName((IMedium *)hardDisk, &nameUtf16);
-                        data->pFuncs->pfnUtf16ToUtf8(nameUtf16, &nameUtf8);
+                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+                if (hddstate != MediaState_Inaccessible) {
+                    VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &nameUtf16);
 
-                        if (nameUtf8) {
-                            DEBUG("nnames[%d]: %s", numActive, nameUtf8);
-                            names[numActive] = strdup(nameUtf8);
-                            if (names[numActive] == NULL) {
-                                virReportOOMError(pool->conn);
-                            } else {
-                                numActive++;
-                            }
+                    VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
+                    VBOX_UTF16_FREE(nameUtf16);
 
-                            data->pFuncs->pfnUtf8Free(nameUtf8);
+                    if (nameUtf8) {
+                        DEBUG("nnames[%d]: %s", numActive, nameUtf8);
+                        names[numActive] = strdup(nameUtf8);
+                        if (names[numActive] == NULL) {
+                            virReportOOMError(pool->conn);
+                        } else {
+                            numActive++;
                         }
-                        data->pFuncs->pfnUtf16Free(nameUtf16);
+
+                        VBOX_UTF8_FREE(nameUtf8);
                     }
-                    hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
                 }
+                VBOX_MEDIUM_RELEASE(hardDisk);
             }
-            hardDiskCount = 0;
-        } else {
-            hardDiskCount = -1;
-            vboxError(pool->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
-                      "could not get the volume list in the pool",
-                      pool->name, (unsigned)rc);
         }
+        hardDiskCount = 0;
+    } else {
+        hardDiskCount = -1;
+        vboxError(pool->conn, VIR_ERR_INTERNAL_ERROR,"%s:%s, rc=%08x",
+                  "could not get the volume list in the pool",
+                  pool->name, (unsigned)rc);
     }
 
     if (numActive)
-        return numActive;
+        ret = numActive;
     else
-        return hardDiskCount;
+        ret = hardDiskCount;
+
+    return ret;
 }
 
 static virStorageVolPtr vboxStorageVolLookupByName(virStoragePoolPtr pool, const char *name) {
-    vboxGlobalData *data         = pool->conn->privateData;
+    VBOX_OBJECT_CHECK(pool->conn, virStorageVolPtr, NULL);
     IHardDisk **hardDisks        = NULL;
-    virStorageVolPtr ret         = NULL;
     PRUint32 hardDiskCount       = 0;
     nsresult rc;
     int i;
 
-    if(data->vboxObj && name) {
-        rc = data->vboxObj->vtbl->GetHardDisks(data->vboxObj, &hardDiskCount, &hardDisks);
-        if (NS_SUCCEEDED(rc)) {
-            for (i = 0; i < hardDiskCount; ++i) {
-                IHardDisk *hardDisk = hardDisks[i];
-
-                if (hardDisk) {
-                    PRUint32 hddstate;
+    if(!name)
+        return ret;
 
-                    hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                    if (hddstate != MediaState_Inaccessible) {
-                        char      *nameUtf8  = NULL;
-                        PRUnichar *nameUtf16 = NULL;
+    rc = data->vboxObj->vtbl->GetHardDisks(data->vboxObj, &hardDiskCount, &hardDisks);
+    if (NS_SUCCEEDED(rc)) {
+        for (i = 0; i < hardDiskCount; ++i) {
+            IHardDisk *hardDisk = hardDisks[i];
 
-                        hardDisk->vtbl->imedium.GetName((IMedium *)hardDisk, &nameUtf16);
+            if (hardDisk) {
+                PRUint32 hddstate;
+                char      *nameUtf8  = NULL;
+                PRUnichar *nameUtf16 = NULL;
 
-                        if (nameUtf16) {
-                            data->pFuncs->pfnUtf16ToUtf8(nameUtf16, &nameUtf8);
-                            data->pFuncs->pfnUtf16Free(nameUtf16);
-                        }
+                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+                if (hddstate != MediaState_Inaccessible) {
+                    VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &nameUtf16);
 
-                        if (nameUtf8 && STREQ(nameUtf8, name)) {
-                            vboxIID *hddIID     = NULL;
-                            char    *hddIIDUtf8 = NULL;
+                    if (nameUtf16) {
+                        VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
+                        VBOX_UTF16_FREE(nameUtf16);
+                    }
 
-                            hardDisk->vtbl->imedium.GetId((IMedium *)hardDisk, &hddIID);
+                    if (nameUtf8 && STREQ(nameUtf8, name)) {
+                        vboxIID *hddIID     = NULL;
+                        char    *hddIIDUtf8 = NULL;
 
-                            if (hddIID) {
-                                vboxIIDtoUtf8(pool->conn, hddIID, &hddIIDUtf8);
-                                vboxIIDUnalloc(hddIID);
-                            }
+                        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetId, &hddIID);
 
-                            if (hddIIDUtf8) {
+                        if (hddIID) {
+                            vboxIIDtoUtf8(pool->conn, hddIID, &hddIIDUtf8);
+                            vboxIIDUnalloc(hddIID);
+                        }
 
-                                ret = virGetStorageVol(pool->conn, pool->name, name, hddIIDUtf8);
+                        if (hddIIDUtf8) {
 
-                                DEBUG("virStorageVolPtr: %p", ret);
-                                DEBUG("Storage Volume Name: %s", name);
-                                DEBUG("Storage Volume key : %s", hddIIDUtf8);
-                                DEBUG("Storage Volume Pool: %s", pool->name);
+                            ret = virGetStorageVol(pool->conn, pool->name, name, hddIIDUtf8);
 
-                                vboxIIDUtf8Free(hddIIDUtf8);
-                            }
+                            DEBUG("virStorageVolPtr: %p", ret);
+                            DEBUG("Storage Volume Name: %s", name);
+                            DEBUG("Storage Volume key : %s", hddIIDUtf8);
+                            DEBUG("Storage Volume Pool: %s", pool->name);
 
-                            data->pFuncs->pfnUtf8Free(nameUtf8);
-                            break;
+                            vboxIIDUtf8Free(hddIIDUtf8);
                         }
 
-                        if (nameUtf8)
-                            data->pFuncs->pfnUtf8Free(nameUtf8);
+                        VBOX_UTF8_FREE(nameUtf8);
+                        break;
                     }
+
+                    if (nameUtf8)
+                        VBOX_UTF8_FREE(nameUtf8);
                 }
             }
-
-            for (i = 0; i < hardDiskCount; ++i)
-                if (hardDisks[i])
-                    hardDisks[i]->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisks[i]);
         }
+
+        for (i = 0; i < hardDiskCount; ++i)
+            VBOX_MEDIUM_RELEASE(hardDisks[i]);
     }
 
     return ret;
 }
 
 static virStorageVolPtr vboxStorageVolLookupByKey(virConnectPtr conn, const char *key) {
-    vboxGlobalData *data = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, virStorageVolPtr, NULL);
     vboxIID   *hddIID    = NULL;
     IHardDisk *hardDisk  = NULL;
-    virStorageVolPtr ret = NULL;
     nsresult rc;
 
+    if (!key)
+        return ret;
+
 #if VBOX_API_VERSION == 2002
+
     if (VIR_ALLOC(hddIID) < 0) {
         virReportOOMError(conn);
         goto cleanup;
     }
 
-    if(data->vboxObj && key) {
-        unsigned char hddUUID[VIR_UUID_BUFLEN];
+    unsigned char hddUUID[VIR_UUID_BUFLEN];
+
+    virUUIDParse(key, hddUUID);
+    vboxIIDFromUUID(hddUUID, hddIID);
 
-        virUUIDParse(key, hddUUID);
-        vboxIIDFromUUID(hddUUID, hddIID);
-        {
 #else  /* VBOX_API_VERSION != 2002 */
-    if(data->vboxObj && key) {
-        data->pFuncs->pfnUtf8ToUtf16(key, &hddIID);
-        if (hddIID) {
-#endif /* VBOX_API_VERSION != 2002 */
 
-            rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                PRUint32 hddstate;
+    VBOX_UTF8_TO_UTF16(key, &hddIID);
+    if (!hddIID)
+        return ret;
 
-                hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                if (hddstate != MediaState_Inaccessible) {
-                    PRUnichar *hddNameUtf16 = NULL;
-                    char      *hddNameUtf8  = NULL;
+#endif /* VBOX_API_VERSION != 2002 */
 
-                    hardDisk->vtbl->imedium.GetName((IMedium *)hardDisk, &hddNameUtf16);
-                    data->pFuncs->pfnUtf16ToUtf8(hddNameUtf16, &hddNameUtf8);
+    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
+    if (NS_SUCCEEDED(rc)) {
+        PRUint32 hddstate;
 
-                    if (hddNameUtf8) {
-                        if (vboxStorageNumOfPools(conn) == 1) {
-                            ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, key);
-                            DEBUG("Storage Volume Pool: %s", "default-pool");
-                        } else {
-                            /* TODO: currently only one default pool and thus
-                             * nothing here, change it when pools are supported
-                             */
-                        }
+        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+        if (hddstate != MediaState_Inaccessible) {
+            PRUnichar *hddNameUtf16 = NULL;
+            char      *hddNameUtf8  = NULL;
 
-                        DEBUG("Storage Volume Name: %s", key);
-                        DEBUG("Storage Volume key : %s", hddNameUtf8);
+            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &hddNameUtf16);
+            VBOX_UTF16_TO_UTF8(hddNameUtf16, &hddNameUtf8);
 
-                        data->pFuncs->pfnUtf8Free(hddNameUtf8);
-                        data->pFuncs->pfnUtf16Free(hddNameUtf16);
-                    }
+            if (hddNameUtf8) {
+                if (vboxStorageNumOfPools(conn) == 1) {
+                    ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, key);
+                    DEBUG("Storage Volume Pool: %s", "default-pool");
+                } else {
+                    /* TODO: currently only one default pool and thus
+                     * nothing here, change it when pools are supported
+                     */
                 }
 
-                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
+                DEBUG("Storage Volume Name: %s", key);
+                DEBUG("Storage Volume key : %s", hddNameUtf8);
+
+                VBOX_UTF8_FREE(hddNameUtf8);
+                VBOX_UTF16_FREE(hddNameUtf16);
             }
         }
+
+        VBOX_MEDIUM_RELEASE(hardDisk);
     }
 
 #if VBOX_API_VERSION == 2002
 cleanup:
-#endif
+#endif  /* VBOX_API_VERSION == 2002 */
     vboxIIDFree(hddIID);
     return ret;
 }
 
 static virStorageVolPtr vboxStorageVolLookupByPath(virConnectPtr conn, const char *path) {
-    vboxGlobalData *data    = conn->privateData;
+    VBOX_OBJECT_CHECK(conn, virStorageVolPtr, NULL);
     PRUnichar *hddPathUtf16 = NULL;
     IHardDisk *hardDisk     = NULL;
-    virStorageVolPtr ret    = NULL;
     nsresult rc;
 
-    if(data->vboxObj && path) {
-        data->pFuncs->pfnUtf8ToUtf16(path, &hddPathUtf16);
-
-        if (hddPathUtf16) {
-            rc = data->vboxObj->vtbl->FindHardDisk(data->vboxObj, hddPathUtf16, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                PRUint32 hddstate;
+    if (!path)
+        return ret;
 
-                hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                if (hddstate != MediaState_Inaccessible) {
-                    PRUnichar *hddNameUtf16 = NULL;
-                    char      *hddNameUtf8  = NULL;
-                    vboxIID   *hddIID       = NULL;
-                    char      *hddIIDUtf8   = NULL;
+    VBOX_UTF8_TO_UTF16(path, &hddPathUtf16);
 
-                    hardDisk->vtbl->imedium.GetName((IMedium *)hardDisk, &hddNameUtf16);
-                    hardDisk->vtbl->imedium.GetId((IMedium *)hardDisk, &hddIID);
+    if (!hddPathUtf16)
+        return ret;
 
-                    if (hddNameUtf16) {
-                        data->pFuncs->pfnUtf16ToUtf8(hddNameUtf16, &hddNameUtf8);
-                        data->pFuncs->pfnUtf16Free(hddNameUtf16);
-                    }
+    rc = data->vboxObj->vtbl->FindHardDisk(data->vboxObj, hddPathUtf16, &hardDisk);
+    if (NS_SUCCEEDED(rc)) {
+        PRUint32 hddstate;
 
-                    if (hddIID) {
-                        vboxIIDtoUtf8(conn, hddIID, &hddIIDUtf8);
-                        vboxIIDUnalloc(hddIID);
-                    }
+        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+        if (hddstate != MediaState_Inaccessible) {
+            PRUnichar *hddNameUtf16 = NULL;
+            char      *hddNameUtf8  = NULL;
+            vboxIID   *hddIID       = NULL;
+            char      *hddIIDUtf8   = NULL;
 
-                    if (hddIIDUtf8 && hddNameUtf8) {
+            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &hddNameUtf16);
+            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetId, &hddIID);
 
-                        /* TODO: currently only one default pool and thus
-                         * the check below, change it when pools are supported
-                         */
-                        if (vboxStorageNumOfPools(conn) == 1)
-                            ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, hddIIDUtf8);
+            if (hddNameUtf16) {
+                VBOX_UTF16_TO_UTF8(hddNameUtf16, &hddNameUtf8);
+                VBOX_UTF16_FREE(hddNameUtf16);
+            }
 
-                        DEBUG("Storage Volume Pool: %s", "default-pool");
-                        DEBUG("Storage Volume Name: %s", hddNameUtf8);
-                        DEBUG("Storage Volume key : %s", hddIIDUtf8);
+            if (hddIID) {
+                vboxIIDtoUtf8(conn, hddIID, &hddIIDUtf8);
+                vboxIIDUnalloc(hddIID);
+            }
 
-                    }
+            if (hddIIDUtf8 && hddNameUtf8) {
 
-                    if (hddNameUtf8)
-                        data->pFuncs->pfnUtf8Free(hddNameUtf8);
+                /* TODO: currently only one default pool and thus
+                 * the check below, change it when pools are supported
+                 */
+                if (vboxStorageNumOfPools(conn) == 1)
+                    ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, hddIIDUtf8);
 
-                    if (hddIIDUtf8)
-                        vboxIIDUtf8Free(hddIIDUtf8);
-                }
+                DEBUG("Storage Volume Pool: %s", "default-pool");
+                DEBUG("Storage Volume Name: %s", hddNameUtf8);
+                DEBUG("Storage Volume key : %s", hddIIDUtf8);
 
-                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
             }
 
-            data->pFuncs->pfnUtf16Free(hddPathUtf16);
+            if (hddNameUtf8)
+                VBOX_UTF8_FREE(hddNameUtf8);
+
+            if (hddIIDUtf8)
+                vboxIIDUtf8Free(hddIIDUtf8);
         }
+
+        VBOX_MEDIUM_RELEASE(hardDisk);
     }
 
+    VBOX_UTF16_FREE(hddPathUtf16);
+
     return ret;
 }
 
 static virStorageVolPtr vboxStorageVolCreateXML(virStoragePoolPtr pool,
                                                 const char *xml,
                                                 unsigned int flags ATTRIBUTE_UNUSED) {
-    vboxGlobalData      *data = pool->conn->privateData;
-    virStorageVolPtr     ret  = NULL;
+    VBOX_OBJECT_CHECK(pool->conn, virStorageVolPtr, NULL);
     virStorageVolDefPtr  def  = NULL;
+    PRUnichar *hddFormatUtf16 = NULL;
+    PRUnichar *hddNameUtf16   = NULL;
     virStoragePoolDef poolDef;
     nsresult rc;
 
@@ -6078,360 +6589,353 @@ static virStorageVolPtr vboxStorageVolCreateXML(virStoragePoolPtr pool,
     if ((def = virStorageVolDefParseString(pool->conn, &poolDef, xml)) == NULL)
         goto cleanup;
 
-    if ((data->vboxObj) && def->name && def->type == VIR_STORAGE_VOL_FILE) {
-        PRUnichar *hddFormatUtf16 = NULL;
-        PRUnichar *hddNameUtf16   = NULL;
+    if (   !def->name
+        || (def->type != VIR_STORAGE_VOL_FILE))
+        goto cleanup;
 
-        /* TODO: for now only the vmdk, vpc and vdi type harddisk
-         * variants can be created, also since there is no vdi
-         * type in enum virStorageFileFormat {} the default
-         * will be to create vdi if nothing is specified in
-         * def->target.format
-         */
+    /* TODO: for now only the vmdk, vpc and vdi type harddisk
+     * variants can be created, also since there is no vdi
+     * type in enum virStorageFileFormat {} the default
+     * will be to create vdi if nothing is specified in
+     * def->target.format
+     */
 
-        if (def->target.format == VIR_STORAGE_FILE_VMDK) {
-            data->pFuncs->pfnUtf8ToUtf16("VMDK", &hddFormatUtf16);
-        } else if (def->target.format == VIR_STORAGE_FILE_VPC) {
-            data->pFuncs->pfnUtf8ToUtf16("VHD", &hddFormatUtf16);
-        } else {
-            data->pFuncs->pfnUtf8ToUtf16("VDI", &hddFormatUtf16);
-        }
+    if (def->target.format == VIR_STORAGE_FILE_VMDK) {
+        VBOX_UTF8_TO_UTF16("VMDK", &hddFormatUtf16);
+    } else if (def->target.format == VIR_STORAGE_FILE_VPC) {
+        VBOX_UTF8_TO_UTF16("VHD", &hddFormatUtf16);
+    } else {
+        VBOX_UTF8_TO_UTF16("VDI", &hddFormatUtf16);
+    }
 
-        data->pFuncs->pfnUtf8ToUtf16(def->name, &hddNameUtf16);
+    VBOX_UTF8_TO_UTF16(def->name, &hddNameUtf16);
 
-        if (hddFormatUtf16 && hddNameUtf16) {
-            IHardDisk *hardDisk = NULL;
+    if (hddFormatUtf16 && hddNameUtf16) {
+        IHardDisk *hardDisk = NULL;
 
-            rc = data->vboxObj->vtbl->CreateHardDisk(data->vboxObj, hddFormatUtf16, hddNameUtf16, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                IProgress *progress    = NULL;
-                PRUint64   logicalSize = def->capacity / 1024 / 1024;
-                PRUint32   variant     = HardDiskVariant_Standard;
+        rc = data->vboxObj->vtbl->CreateHardDisk(data->vboxObj, hddFormatUtf16, hddNameUtf16, &hardDisk);
+        if (NS_SUCCEEDED(rc)) {
+            IProgress *progress    = NULL;
+            PRUint64   logicalSize = def->capacity / 1024 / 1024;
+            PRUint32   variant     = HardDiskVariant_Standard;
 
-                if (def->capacity == def->allocation)
-                    variant = HardDiskVariant_Fixed;
+            if (def->capacity == def->allocation)
+                variant = HardDiskVariant_Fixed;
 
-                rc = hardDisk->vtbl->CreateBaseStorage(hardDisk, logicalSize, variant, &progress);
-                if (NS_SUCCEEDED(rc) && progress) {
-                    vboxIID *hddIID = NULL;
+            rc = hardDisk->vtbl->CreateBaseStorage(hardDisk, logicalSize, variant, &progress);
+            if (NS_SUCCEEDED(rc) && progress) {
+                vboxIID *hddIID = NULL;
 #if VBOX_API_VERSION == 2002
-                    nsresult resultCode;
+                nsresult resultCode;
 #else
-                    PRInt32  resultCode;
+                PRInt32  resultCode;
 #endif
 
-                    progress->vtbl->WaitForCompletion(progress, -1);
-                    progress->vtbl->GetResultCode(progress, &resultCode);
+                progress->vtbl->WaitForCompletion(progress, -1);
+                progress->vtbl->GetResultCode(progress, &resultCode);
 
-                    if (NS_SUCCEEDED(resultCode)) {
+                if (NS_SUCCEEDED(resultCode)) {
 
-                        rc = hardDisk->vtbl->imedium.GetId((IMedium *)hardDisk, &hddIID);
-                        if (NS_SUCCEEDED(rc)) {
-                            char *hddKey = NULL;
+                    rc = VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetId, &hddIID);
+                    if (NS_SUCCEEDED(rc)) {
+                        char *hddKey = NULL;
 
-                            vboxIIDtoUtf8(pool->conn, hddIID, &hddKey);
+                        vboxIIDtoUtf8(pool->conn, hddIID, &hddKey);
 
-                            if (hddKey)
-                                ret = virGetStorageVol(pool->conn, pool->name, def->name, hddKey);
+                        if (hddKey)
+                            ret = virGetStorageVol(pool->conn, pool->name, def->name, hddKey);
 
-                            vboxIIDUtf8Free(hddKey);
-                            vboxIIDUnalloc(hddIID);
-                        }
+                        vboxIIDUtf8Free(hddKey);
+                        vboxIIDUnalloc(hddIID);
                     }
-
-                    progress->vtbl->nsisupports.Release((nsISupports *)progress);
                 }
 
+                VBOX_RELEASE(progress);
             }
-        }
 
-        if (hddFormatUtf16)
-            data->pFuncs->pfnUtf16Free(hddFormatUtf16);
-        if (hddNameUtf16)
-            data->pFuncs->pfnUtf16Free(hddNameUtf16);
+        }
     }
 
+    VBOX_UTF16_FREE(hddFormatUtf16);
+    VBOX_UTF16_FREE(hddNameUtf16);
+
 cleanup:
     virStorageVolDefFree(def);
     return ret;
 }
 
-
 static int vboxStorageVolDelete(virStorageVolPtr vol,
                                 unsigned int flags ATTRIBUTE_UNUSED) {
-    vboxGlobalData *data = vol->conn->privateData;
+    VBOX_OBJECT_CHECK(vol->conn, int, -1);
     vboxIID   *hddIID    = NULL;
     IHardDisk *hardDisk  = NULL;
-    int ret = -1, i = 0, j = 0;
     int deregister = 0;
     nsresult rc;
+    int i = 0;
+    int j = 0;
 
-    if(data->vboxObj && vol->key) {
-
-        vboxUtf8toIID(vol->conn, vol->key, &hddIID);
-        if (hddIID) {
-
-            rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                PRUint32 hddstate;
-
-                hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                if (hddstate != MediaState_Inaccessible) {
-                    PRUint32  machineIdsSize = 0;
-                    vboxIID **machineIds     = NULL;
-
-                    hardDisk->vtbl->imedium.GetMachineIds((IMedium *)hardDisk, &machineIdsSize, &machineIds);
-
-                    for (i = 0; i < machineIdsSize; i++) {
-                        IMachine *machine = NULL;
-
-                        rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, machineIds[i]);
-                        if (NS_SUCCEEDED(rc)) {
-
-                            rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
-                            if (NS_SUCCEEDED(rc)) {
-                                PRUint32 hddAttachSize = 0;
-                                IHardDiskAttachment **hddAttachments = NULL;
-
-                                machine->vtbl->GetHardDiskAttachments(machine, &hddAttachSize, &hddAttachments);
-                                for (j = 0; j < hddAttachSize; j++) {
-                                    IHardDiskAttachment *hddAttachment = hddAttachments[j];
-
-                                    if (hddAttachment) {
-                                        IHardDisk *hdd = NULL;
-
-                                        rc = hddAttachment->vtbl->GetHardDisk(hddAttachment, &hdd);
-                                        if (NS_SUCCEEDED(rc) && hdd) {
-                                            vboxIID *iid = NULL;
+    if (!vol->key)
+        return ret;
 
-                                            hdd->vtbl->imedium.GetId((IMedium *)hdd, &iid);
-                                            if (iid) {
+    vboxUtf8toIID(vol->conn, vol->key, &hddIID);
+    if (!hddIID)
+        return ret;
 
-                                                    DEBUGIID("HardDisk (to delete) UUID", hddIID);
-                                                    DEBUGIID("HardDisk (currently processing) UUID", iid);
+    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
+    if (NS_SUCCEEDED(rc)) {
+        PRUint32 hddstate;
 
-                                                if (vboxIIDEqual(hddIID, iid)) {
-                                                    PRUnichar *controller = NULL;
-                                                    PRInt32    port       = 0;
-                                                    PRInt32    device     = 0;
+        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+        if (hddstate != MediaState_Inaccessible) {
+            PRUint32  machineIdsSize = 0;
+            vboxIID **machineIds     = NULL;
 
-                                                    DEBUGIID("Found HardDisk to delete, UUID", hddIID);
+            VBOX_MEDIUM_FUNC_ARG2(hardDisk, GetMachineIds, &machineIdsSize, &machineIds);
 
-                                                    hddAttachment->vtbl->GetController(hddAttachment, &controller);
-                                                    hddAttachment->vtbl->GetPort(hddAttachment, &port);
-                                                    hddAttachment->vtbl->GetDevice(hddAttachment, &device);
+            for (i = 0; i < machineIdsSize; i++) {
+                IMachine *machine = NULL;
 
-                                                    rc = machine->vtbl->DetachHardDisk(machine, controller, port, device);
-                                                    if (NS_SUCCEEDED(rc)) {
-                                                        rc = machine->vtbl->SaveSettings(machine);
-                                                        DEBUG0("saving machine settings");
-                                                    }
+                rc = data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, machineIds[i]);
+                if (NS_SUCCEEDED(rc)) {
 
-                                                    if (NS_SUCCEEDED(rc)) {
-                                                        deregister++;
-                                                        DEBUG("deregistering hdd:%d", deregister);
-                                                    }
+                    rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
+                    if (NS_SUCCEEDED(rc)) {
+                        PRUint32 hddAttachSize = 0;
+                        IHardDiskAttachment **hddAttachments = NULL;
+
+#if VBOX_API_VERSION < 3001
+                        machine->vtbl->GetHardDiskAttachments(machine, &hddAttachSize, &hddAttachments);
+#else  /* VBOX_API_VERSION >= 3001 */
+                        machine->vtbl->GetMediumAttachments(machine, &hddAttachSize, &hddAttachments);
+#endif /* VBOX_API_VERSION >= 3001 */
+                        for (j = 0; j < hddAttachSize; j++) {
+                            IHardDiskAttachment *hddAttachment = hddAttachments[j];
+
+                            if (hddAttachment) {
+                                IHardDisk *hdd = NULL;
+
+#if VBOX_API_VERSION < 3001
+                                rc = hddAttachment->vtbl->GetHardDisk(hddAttachment, &hdd);
+#else  /* VBOX_API_VERSION >= 3001 */
+                                rc = hddAttachment->vtbl->GetMedium(hddAttachment, &hdd);
+#endif /* VBOX_API_VERSION >= 3001 */
+                                if (NS_SUCCEEDED(rc) && hdd) {
+                                    vboxIID *iid = NULL;
+
+                                    VBOX_MEDIUM_FUNC_ARG1(hdd, GetId, &iid);
+                                    if (iid) {
+
+                                            DEBUGIID("HardDisk (to delete) UUID", hddIID);
+                                            DEBUGIID("HardDisk (currently processing) UUID", iid);
+
+                                        if (vboxIIDEqual(hddIID, iid)) {
+                                            PRUnichar *controller = NULL;
+                                            PRInt32    port       = 0;
+                                            PRInt32    device     = 0;
+
+                                            DEBUGIID("Found HardDisk to delete, UUID", hddIID);
+
+                                            hddAttachment->vtbl->GetController(hddAttachment, &controller);
+                                            hddAttachment->vtbl->GetPort(hddAttachment, &port);
+                                            hddAttachment->vtbl->GetDevice(hddAttachment, &device);
+
+#if VBOX_API_VERSION < 3001
+                                            rc = machine->vtbl->DetachHardDisk(machine, controller, port, device);
+#else  /* VBOX_API_VERSION >= 3001 */
+                                            rc = machine->vtbl->DetachDevice(machine, controller, port, device);
+#endif /* VBOX_API_VERSION >= 3001 */
+                                            if (NS_SUCCEEDED(rc)) {
+                                                rc = machine->vtbl->SaveSettings(machine);
+                                                DEBUG0("saving machine settings");
+                                            }
 
-                                                    if (controller)
-                                                        data->pFuncs->pfnUtf16Free(controller);
-                                                }
-                                                vboxIIDUnalloc(iid);
+                                            if (NS_SUCCEEDED(rc)) {
+                                                deregister++;
+                                                DEBUG("deregistering hdd:%d", deregister);
                                             }
-                                            hdd->vtbl->imedium.nsisupports.Release((nsISupports *)hdd);
+
+                                            if (controller)
+                                                VBOX_UTF16_FREE(controller);
                                         }
-                                        hddAttachment->vtbl->nsisupports.Release((nsISupports *)hddAttachment);
+                                        vboxIIDUnalloc(iid);
                                     }
+                                    VBOX_MEDIUM_RELEASE(hdd);
                                 }
-                                machine->vtbl->nsisupports.Release((nsISupports *)machine);
+                                VBOX_RELEASE(hddAttachment);
                             }
-                            data->vboxSession->vtbl->Close(data->vboxSession);
                         }
+                        VBOX_RELEASE(machine);
                     }
+                    data->vboxSession->vtbl->Close(data->vboxSession);
+                }
+            }
 
-                    for (i = 0; i < machineIdsSize; i++)
-                        if (machineIds[i])
-                            vboxIIDUnalloc(machineIds[i]);
-
-                    if (machineIdsSize == 0 || machineIdsSize == deregister) {
-                        IProgress *progress = NULL;
+            for (i = 0; i < machineIdsSize; i++)
+                if (machineIds[i])
+                    vboxIIDUnalloc(machineIds[i]);
 
-                        rc = hardDisk->vtbl->DeleteStorage(hardDisk, &progress);
+            if (machineIdsSize == 0 || machineIdsSize == deregister) {
+                IProgress *progress = NULL;
 
-                        if (NS_SUCCEEDED(rc) && progress) {
-                            progress->vtbl->WaitForCompletion(progress, -1);
-                            progress->vtbl->nsisupports.Release((nsISupports *)progress);
-                            DEBUGIID("HardDisk deleted, UUID", hddIID);
-                            ret = 0;
-                        }
-                    }
+                rc = hardDisk->vtbl->DeleteStorage(hardDisk, &progress);
 
+                if (NS_SUCCEEDED(rc) && progress) {
+                    progress->vtbl->WaitForCompletion(progress, -1);
+                    VBOX_RELEASE(progress);
+                    DEBUGIID("HardDisk deleted, UUID", hddIID);
+                    ret = 0;
                 }
-
-                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
             }
 
-            vboxIIDUtf16Free(hddIID);
         }
+
+        VBOX_MEDIUM_RELEASE(hardDisk);
     }
 
+    vboxIIDUtf16Free(hddIID);
+
     return ret;
 }
 
 static int vboxStorageVolGetInfo(virStorageVolPtr vol, virStorageVolInfoPtr info) {
-    vboxGlobalData *data = vol->conn->privateData;
+    VBOX_OBJECT_CHECK(vol->conn, int, -1);
     IHardDisk *hardDisk  = NULL;
     vboxIID   *hddIID    = NULL;
-    int ret = -1;
     nsresult rc;
 
-    if(data->vboxObj && vol->key && info) {
-
-        vboxUtf8toIID(vol->conn, vol->key, &hddIID);
-        if (hddIID) {
-
-            rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                PRUint32 hddstate;
+    if (   !vol->key
+        || !info)
+        return ret;
 
-                hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                if (hddstate != MediaState_Inaccessible) {
-                    PRUint32 hddType;
-                    PRUint64 hddLogicalSize;
-                    PRUint64 hddActualSize;
+    vboxUtf8toIID(vol->conn, vol->key, &hddIID);
+    if (!hddIID)
+        return ret;
 
-                    hardDisk->vtbl->GetType(hardDisk, &hddType);
-                    if (hddType == HardDiskType_Writethrough)
-                        info->type = VIR_STORAGE_VOL_BLOCK;
-                    else
-                        info->type = VIR_STORAGE_VOL_FILE;
+    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
+    if (NS_SUCCEEDED(rc)) {
+        PRUint32 hddstate;
 
-                    hardDisk->vtbl->GetLogicalSize(hardDisk, &hddLogicalSize);
-                    info->capacity = hddLogicalSize * 1024 * 1024; /* MB => Bytes */
+        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+        if (hddstate != MediaState_Inaccessible) {
+            PRUint64 hddLogicalSize;
+            PRUint64 hddActualSize;
 
-                    hardDisk->vtbl->imedium.GetSize((IMedium *)hardDisk, &hddActualSize);
-                    info->allocation = hddActualSize;
+            info->type = VIR_STORAGE_VOL_FILE;
 
-                    ret = 0;
+            hardDisk->vtbl->GetLogicalSize(hardDisk, &hddLogicalSize);
+            info->capacity = hddLogicalSize * 1024 * 1024; /* MB => Bytes */
 
-                    DEBUG("Storage Volume Name: %s", vol->name);
-                    DEBUG("Storage Volume Type: %s", info->type == VIR_STORAGE_VOL_BLOCK ? "Block" : "File");
-                    DEBUG("Storage Volume Capacity: %llu", info->capacity);
-                    DEBUG("Storage Volume Allocation: %llu", info->allocation);
-                }
+            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetSize, &hddActualSize);
+            info->allocation = hddActualSize;
 
-                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
-            }
+            ret = 0;
 
-            vboxIIDUtf16Free(hddIID);
+            DEBUG("Storage Volume Name: %s", vol->name);
+            DEBUG("Storage Volume Type: %s", info->type == VIR_STORAGE_VOL_BLOCK ? "Block" : "File");
+            DEBUG("Storage Volume Capacity: %llu", info->capacity);
+            DEBUG("Storage Volume Allocation: %llu", info->allocation);
         }
+
+        VBOX_MEDIUM_RELEASE(hardDisk);
     }
 
+    vboxIIDUtf16Free(hddIID);
+
     return ret;
 }
 
 static char *vboxStorageVolGetXMLDesc(virStorageVolPtr vol, unsigned int flags ATTRIBUTE_UNUSED) {
-    vboxGlobalData *data = vol->conn->privateData;
+    VBOX_OBJECT_CHECK(vol->conn, char *, NULL);
     IHardDisk *hardDisk  = NULL;
     vboxIID   *hddIID    = NULL;
-    char      *ret       = NULL;
     virStoragePoolDef pool;
     virStorageVolDef def;
     int defOk = 0;
     nsresult rc;
 
+    if (!vol->key)
+        return ret;
+
     memset(&pool, 0, sizeof(pool));
     memset(&def, 0, sizeof(def));
 
-    if(data->vboxObj && vol->key) {
-
-        vboxUtf8toIID(vol->conn, vol->key, &hddIID);
-        if (hddIID) {
-
-            rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                PRUint32 hddstate;
-
-                rc = hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                if (NS_SUCCEEDED(rc) && hddstate != MediaState_Inaccessible) {
-                    PRUnichar *hddFormatUtf16 = NULL;
-                    PRUint64 hddLogicalSize;
-                    PRUint64 hddActualSize;
-                    PRUint32 hddType;
-
-                    /* since there is currently one default pool now
-                     * and virStorageVolDefFormat() just checks it type
-                     * so just assign it for now, change the behaviour
-                     * when vbox supports pools.
-                     */
-                    pool.type = VIR_STORAGE_POOL_DIR;
-
-                    rc = hardDisk->vtbl->GetType(hardDisk, &hddType);
-                    if (NS_SUCCEEDED(rc)) {
-                        if (hddType == HardDiskType_Writethrough)
-                            def.type = VIR_STORAGE_VOL_BLOCK;
-                        else
-                            def.type = VIR_STORAGE_VOL_FILE;
-                        defOk = 1;
-                    } else {
-                        defOk = 0;
-                    }
-
-                    rc = hardDisk->vtbl->GetLogicalSize(hardDisk, &hddLogicalSize);
-                    if (NS_SUCCEEDED(rc) && defOk)
-                        def.capacity = hddLogicalSize * 1024 * 1024; /* MB => Bytes */
-                    else
-                        defOk = 0;
+    vboxUtf8toIID(vol->conn, vol->key, &hddIID);
+    if (!hddIID)
+        return ret;
 
-                    rc = hardDisk->vtbl->imedium.GetSize((IMedium *)hardDisk, &hddActualSize);
-                    if (NS_SUCCEEDED(rc) && defOk)
-                        def.allocation = hddActualSize;
+    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
+    if (NS_SUCCEEDED(rc)) {
+        PRUint32 hddstate;
+
+        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+        if (NS_SUCCEEDED(rc) && hddstate != MediaState_Inaccessible) {
+            PRUnichar *hddFormatUtf16 = NULL;
+            PRUint64 hddLogicalSize;
+            PRUint64 hddActualSize;
+
+            /* since there is currently one default pool now
+             * and virStorageVolDefFormat() just checks it type
+             * so just assign it for now, change the behaviour
+             * when vbox supports pools.
+             */
+            pool.type = VIR_STORAGE_POOL_DIR;
+            def.type = VIR_STORAGE_VOL_FILE;
+            defOk = 1;
+
+            rc = hardDisk->vtbl->GetLogicalSize(hardDisk, &hddLogicalSize);
+            if (NS_SUCCEEDED(rc) && defOk)
+                def.capacity = hddLogicalSize * 1024 * 1024; /* MB => Bytes */
+            else
+                defOk = 0;
+
+            rc = VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetSize, &hddActualSize);
+            if (NS_SUCCEEDED(rc) && defOk)
+                def.allocation = hddActualSize;
+            else
+                defOk = 0;
+
+            def.name = strdup(vol->name);
+            if (!(def.name && defOk))
+                defOk = 0;
+
+            def.key = strdup(vol->key);
+            if (!(def.key && defOk))
+                defOk = 0;
+
+            rc = hardDisk->vtbl->GetFormat(hardDisk, &hddFormatUtf16);
+            if (NS_SUCCEEDED(rc) && defOk) {
+                char *hddFormatUtf8 = NULL;
+
+                VBOX_UTF16_TO_UTF8(hddFormatUtf16, &hddFormatUtf8);
+                if (hddFormatUtf8) {
+
+                    DEBUG("Storage Volume Format: %s", hddFormatUtf8);
+
+                    if (STRCASEEQ("vmdk", hddFormatUtf8))
+                        def.target.format = VIR_STORAGE_FILE_VMDK;
+                    else if (STRCASEEQ("vhd", hddFormatUtf8))
+                        def.target.format = VIR_STORAGE_FILE_VPC;
                     else
-                        defOk = 0;
-
-                    def.name = strdup(vol->name);
-                    if (!(def.name && defOk))
-                        defOk = 0;
-
-                    def.key = strdup(vol->key);
-                    if (!(def.key && defOk))
-                        defOk = 0;
-
-                    rc = hardDisk->vtbl->GetFormat(hardDisk, &hddFormatUtf16);
-                    if (NS_SUCCEEDED(rc) && defOk) {
-                        char *hddFormatUtf8 = NULL;
+                        def.target.format = VIR_STORAGE_FILE_RAW;
 
-                        data->pFuncs->pfnUtf16ToUtf8(hddFormatUtf16, &hddFormatUtf8);
-                        if (hddFormatUtf8) {
-
-                            DEBUG("Storage Volume Format: %s", hddFormatUtf8);
-
-                            if (STRCASEEQ("vmdk", hddFormatUtf8))
-                                def.target.format = VIR_STORAGE_FILE_VMDK;
-                            else if (STRCASEEQ("vhd", hddFormatUtf8))
-                                def.target.format = VIR_STORAGE_FILE_VPC;
-                            else
-                                def.target.format = VIR_STORAGE_FILE_RAW;
-
-                            /* TODO: need to add vdi to enum virStorageFileFormat {}
-                             * and then add it here
-                             */
-
-                            data->pFuncs->pfnUtf8Free(hddFormatUtf8);
-                        }
+                    /* TODO: need to add vdi to enum virStorageFileFormat {}
+                     * and then add it here
+                     */
 
-                        data->pFuncs->pfnUtf16Free(hddFormatUtf16);
-                    } else {
-                        defOk = 0;
-                    }
+                    VBOX_UTF8_FREE(hddFormatUtf8);
                 }
 
-                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
+                VBOX_UTF16_FREE(hddFormatUtf16);
+            } else {
+                defOk = 0;
             }
-
-            vboxIIDUtf16Free(hddIID);
         }
+
+        VBOX_MEDIUM_RELEASE(hardDisk);
     }
 
+    vboxIIDUtf16Free(hddIID);
+
     if (defOk)
         ret = virStorageVolDefFormat(vol->conn, &pool, &def);
 
@@ -6439,52 +6943,51 @@ static char *vboxStorageVolGetXMLDesc(virStorageVolPtr vol, unsigned int flags A
 }
 
 static char *vboxStorageVolGetPath(virStorageVolPtr vol) {
-    vboxGlobalData *data = vol->conn->privateData;
+    VBOX_OBJECT_CHECK(vol->conn, char *, NULL);
     IHardDisk *hardDisk  = NULL;
     vboxIID   *hddIID    = NULL;
-    char      *ret       = NULL;
     nsresult rc;
 
-    if(data->vboxObj && vol->key) {
-
-        vboxUtf8toIID(vol->conn, vol->key, &hddIID);
-        if (hddIID) {
-
-            rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
-            if (NS_SUCCEEDED(rc)) {
-                PRUint32 hddstate;
+    if (!vol->key)
+        return ret;
 
-                hardDisk->vtbl->imedium.GetState((IMedium *)hardDisk, &hddstate);
-                if (hddstate != MediaState_Inaccessible) {
-                    PRUnichar *hddLocationUtf16 = NULL;
-                    char      *hddLocationUtf8  = NULL;
+    vboxUtf8toIID(vol->conn, vol->key, &hddIID);
+    if (!hddIID)
+        return ret;
 
-                    hardDisk->vtbl->imedium.GetLocation((IMedium *)hardDisk, &hddLocationUtf16);
+    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID, &hardDisk);
+    if (NS_SUCCEEDED(rc)) {
+        PRUint32 hddstate;
 
-                    data->pFuncs->pfnUtf16ToUtf8(hddLocationUtf16, &hddLocationUtf8);
-                    if (hddLocationUtf8) {
+        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
+        if (hddstate != MediaState_Inaccessible) {
+            PRUnichar *hddLocationUtf16 = NULL;
+            char      *hddLocationUtf8  = NULL;
 
-                        ret = strdup(hddLocationUtf8);
-                        if (!ret)
-                            virReportOOMError(vol->conn);
+            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetLocation, &hddLocationUtf16);
 
-                        DEBUG("Storage Volume Name: %s", vol->name);
-                        DEBUG("Storage Volume Path: %s", hddLocationUtf8);
-                        DEBUG("Storage Volume Pool: %s", vol->pool);
+            VBOX_UTF16_TO_UTF8(hddLocationUtf16, &hddLocationUtf8);
+            if (hddLocationUtf8) {
 
-                        data->pFuncs->pfnUtf8Free(hddLocationUtf8);
-                    }
+                ret = strdup(hddLocationUtf8);
+                if (!ret)
+                    virReportOOMError(vol->conn);
 
-                    data->pFuncs->pfnUtf16Free(hddLocationUtf16);
-                }
+                DEBUG("Storage Volume Name: %s", vol->name);
+                DEBUG("Storage Volume Path: %s", hddLocationUtf8);
+                DEBUG("Storage Volume Pool: %s", vol->pool);
 
-                hardDisk->vtbl->imedium.nsisupports.Release((nsISupports *)hardDisk);
+                VBOX_UTF8_FREE(hddLocationUtf8);
             }
 
-            vboxIIDUtf16Free(hddIID);
+            VBOX_UTF16_FREE(hddLocationUtf16);
         }
+
+        VBOX_MEDIUM_RELEASE(hardDisk);
     }
 
+    vboxIIDUtf16Free(hddIID);
+
     return ret;
 }
 
@@ -6629,5 +7132,5 @@ virStorageDriver NAME(StorageDriver) = {
     .volDelete          = vboxStorageVolDelete,
     .volGetInfo         = vboxStorageVolGetInfo,
     .volGetXMLDesc      = vboxStorageVolGetXMLDesc,
-    .volGetPath         = vboxStorageVolGetPath,
+    .volGetPath         = vboxStorageVolGetPath
 };
-- 
1.6.3.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]