Index: /trunk/src/VBox/Main/cbinding/tstXPCOMCCall.c
===================================================================
--- /trunk/src/VBox/Main/cbinding/tstXPCOMCCall.c	(revision 19767)
+++ /trunk/src/VBox/Main/cbinding/tstXPCOMCCall.c	(revision 19768)
@@ -129,4 +129,6 @@
     printf("OnStateChange: %s\n", GetStateName(state));
     fflush(stdout);
+    if (state == MachineState_PoweredOff)
+        g_fStop = 1;
     return 0;
 }
@@ -276,5 +278,5 @@
     }
 
-    printf("vboxCallback QueryInterface didn't find a matching interface\n");
+    /* printf("vboxCallback QueryInterface didn't find a matching interface\n"); */
     printUUID(iid);
     printUUID(&ivirtualboxCallbackUUID);
@@ -307,7 +309,7 @@
     nsresult rc;
 
-    rc = virtualBox->vtbl->OpenExistingSession(virtualBox, session, machineId);
-    session->vtbl->GetConsole(session, &console);
-    if (console) {
+    rc = session->vtbl->GetConsole(session, &console);
+    if ((NS_SUCCEEDED(rc)) && console)
+    {
         IConsoleCallback *consoleCallback = NULL;
 
@@ -315,6 +317,6 @@
         consoleCallback->vtbl = calloc(1, sizeof(struct IConsoleCallback_vtbl));
 
-        if (consoleCallback && consoleCallback->vtbl) {
-
+        if (consoleCallback && consoleCallback->vtbl)
+        {
             consoleCallback->vtbl->nsisupports.AddRef = &AddRef;
             consoleCallback->vtbl->nsisupports.Release = &Release;
@@ -340,6 +342,6 @@
             g_refcount = 1;
 
-            console->vtbl->RegisterCallback(console, consoleCallback);
-
+            rc = console->vtbl->RegisterCallback(console, consoleCallback);
+            if (NS_SUCCEEDED(rc))
             {
                 /* crude way to show how it works, but any
@@ -348,11 +350,13 @@
                 PRInt32 fd;
 
-                printf("Entering event loop, press Ctrl-C to terminate\n");
+                printf("Entering event loop, PowerOff the machine to exit or press Ctrl-C to terminate\n");
                 fflush(stdout);
                 signal(SIGINT, sigIntHandler);
 
                 fd = queue->vtbl->GetEventQueueSelectFD(queue);
-                if (fd >= 0) {
-                    while (!g_fStop) {
+                if (fd >= 0)
+                {
+                    while (!g_fStop)
+                    {
                         struct pollfd   pfd;
                         pfd.fd = fd;
@@ -362,6 +366,9 @@
                         rc = queue->vtbl->ProcessPendingEvents(queue);
                     }
-                } else {
-                    while (!g_fStop) {
+                }
+                else
+                {
+                    while (!g_fStop)
+                    {
                         PLEvent *pEvent = NULL;
                         rc = queue->vtbl->WaitForEvent(queue, &pEvent);
@@ -376,6 +383,9 @@
             consoleCallback->vtbl->nsisupports.Release((nsISupports *)consoleCallback);
         }
-    }
-    session->vtbl->Close((void *)session);
+        else
+        {
+            printf("Failed while allocating memory for console Callback.\n");
+        }
+    }
 }
 
