Index: trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c	(revision 68483)
+++ trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c	(revision 68550)
@@ -581,5 +581,5 @@
  * @param   cbData      Data size
  */
-static int crVBoxHGCMCall(CRConnection *conn, VBoxGuestHGCMCallInfo *pData, unsigned cbData)
+static int crVBoxHGCMCall(CRConnection *conn, PVBGLIOCHGCMCALL pData, unsigned cbData)
 {
 #ifdef IN_GUEST
@@ -705,5 +705,5 @@
 
         rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
-        callRes = parms.hdr.result;
+        callRes = parms.hdr.Hdr.rc;
     }
     else
@@ -719,5 +719,5 @@
 
         rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
-        callRes = parms.hdr.result;
+        callRes = parms.hdr.Hdr.rc;
     }
 
@@ -759,7 +759,7 @@
     rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
 
-    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.result))
-    {
-        crWarning("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.result);
+    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc))
+    {
+        crWarning("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.Hdr.rc);
         return;
     }
@@ -835,7 +835,7 @@
 
             rc = crVBoxHGCMCall(conn, &wbParms.hdr, sizeof(wbParms));
-            if (RT_FAILURE(rc) || RT_FAILURE(wbParms.hdr.result))
+            if (RT_FAILURE(rc) || RT_FAILURE(wbParms.hdr.Hdr.rc))
             {
-                crError("SHCRGL_GUEST_FN_WRITE_BUFFER (%i) failed with %x %x\n", wbParms.pBuffer.u.Pointer.size, rc, wbParms.hdr.result);
+                crError("SHCRGL_GUEST_FN_WRITE_BUFFER (%i) failed with %x %x\n", wbParms.pBuffer.u.Pointer.size, rc, wbParms.hdr.Hdr.rc);
                 return;
             }
@@ -859,5 +859,5 @@
 
             /*bit of hack to reuse code below*/
-            parms.hdr.result = wrbParms.hdr.result;
+            parms.hdr.Hdr.rc = wrbParms.hdr.Hdr.rc;
             crMemcpy(&parms.cbWriteback, &wrbParms.cbWriteback, sizeof(HGCMFunctionParameter));
             crMemcpy(&parms.pWriteback, &wrbParms.pWriteback, sizeof(HGCMFunctionParameter));
@@ -866,8 +866,8 @@
 #endif
 
-    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.result))
-    {
-
-        if ((VERR_BUFFER_OVERFLOW == parms.hdr.result) && RT_SUCCESS(rc))
+    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc))
+    {
+
+        if ((VERR_BUFFER_OVERFLOW == parms.hdr.Hdr.rc) && RT_SUCCESS(rc))
         {
             /* reallocate buffer and retry */
@@ -887,5 +887,5 @@
         else
         {
-            crWarning("SHCRGL_GUEST_FN_WRITE_READ (%i) failed with %x %x\n", len, rc, parms.hdr.result);
+            crWarning("SHCRGL_GUEST_FN_WRITE_READ (%i) failed with %x %x\n", len, rc, parms.hdr.Hdr.rc);
             return;
         }
@@ -991,7 +991,7 @@
     rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
 
-    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.result))
-    {
-        crDebug("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.result);
+    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc))
+    {
+        crDebug("SHCRGL_GUEST_FN_READ failed with %x %x\n", rc, parms.hdr.Hdr.rc);
         return;
     }
@@ -1194,5 +1194,5 @@
     if (RT_SUCCESS(rc))
     {
-        rc =  parms.hdr.result;
+        rc =  parms.hdr.Hdr.rc;
         if (RT_SUCCESS(rc))
         {
@@ -1226,5 +1226,5 @@
     if (RT_SUCCESS(rc))
     {
-        rc =  caps.hdr.result;
+        rc = caps.hdr.Hdr.rc;
         if (RT_SUCCESS(rc))
         {
@@ -1251,10 +1251,10 @@
     VBGL_HGCM_HDR_INIT(&parms.hdr, conn->u32ClientID, SHCRGL_GUEST_FN_SET_PID, SHCRGL_CPARMS_SET_PID);
 
-    parms.u64PID.type     = VMMDevHGCMParmType_64bit;
+    parms.u64PID.type      = VMMDevHGCMParmType_64bit;
     parms.u64PID.u.value64 = pid;
 
     rc = crVBoxHGCMCall(conn, &parms.hdr, sizeof(parms));
 
-    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.result))
+    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.Hdr.rc))
     {
         crWarning("SHCRGL_GUEST_FN_SET_PID failed!");
@@ -1273,5 +1273,5 @@
 {
 #ifdef IN_GUEST
-            int rc;
+    int rc;
     VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
     rc = VbglR3InitUser();
@@ -1741,5 +1741,5 @@
 #ifdef DEBUG_misha
             /* catch it here to test the buffer */
-            Assert(RT_SUCCESS(parms->hdr.result) || parms->hdr.result == VERR_BUFFER_OVERFLOW);
+            Assert(RT_SUCCESS(parms->hdr.Hdr.rc) || parms->hdr.Hdr.rc == VERR_BUFFER_OVERFLOW);
 #endif
             _crVBoxHGSMICmdBufferUnlock(pClient);
Index: trunk/src/VBox/GuestHost/OpenGL/util/vboxhgsmi.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/util/vboxhgsmi.c	(revision 68483)
+++ trunk/src/VBox/GuestHost/OpenGL/util/vboxhgsmi.c	(revision 68550)
@@ -173,9 +173,4 @@
     CRNetCloseFuncList   *close_list;
     CRBufferPool *mempool;
-#ifdef RT_OS_WINDOWS
-    HANDLE               hGuestDrv;
-#else
-    int                  iGuestDrv;
-#endif
 } CRVBOXHGSMIDATA;
 
@@ -1200,82 +1195,24 @@
  * Servers go through crVBoxHGCMAccept;
  */
-/*@todo use vbglR3Something here */
 static int crVBoxHGSMIDoConnect( CRConnection *conn )
 {
 #ifdef IN_GUEST
-    VBoxGuestHGCMConnectInfo info;
-
-#ifdef RT_OS_WINDOWS
-    DWORD cbReturned;
+    int rc;
     VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
-
-    if (g_crvboxhgsmi.hGuestDrv == INVALID_HANDLE_VALUE)
-    {
-        /* open VBox guest driver */
-        g_crvboxhgsmi.hGuestDrv = CreateFile(VBOXGUEST_DEVICE_NAME,
-                                        GENERIC_READ | GENERIC_WRITE,
-                                        FILE_SHARE_READ | FILE_SHARE_WRITE,
-                                        NULL,
-                                        OPEN_EXISTING,
-                                        FILE_ATTRIBUTE_NORMAL,
-                                        NULL);
-
-        /* @todo check if we could rollback to softwareopengl */
-        if (g_crvboxhgsmi.hGuestDrv == INVALID_HANDLE_VALUE)
-        {
-            Assert(0);
-            crDebug("could not open VBox Guest Additions driver! rc = %d\n", GetLastError());
-            VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
-            return FALSE;
-        }
-    }
-#else
-    VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
-
-    if (g_crvboxhgsmi.iGuestDrv == INVALID_HANDLE_VALUE)
-    {
-        g_crvboxhgsmi.iGuestDrv = open(VBOXGUEST_USER_DEVICE_NAME, O_RDWR, 0);
-        if (g_crvboxhgsmi.iGuestDrv == INVALID_HANDLE_VALUE)
-        {
-            crDebug("could not open Guest Additions kernel module! rc = %d\n", errno);
-            VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
-            return FALSE;
-        }
-    }
-#endif
-
-    memset (&info, 0, sizeof (info));
-    info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
-    strcpy (info.Loc.u.host.achName, "VBoxSharedCrOpenGL");
-
-#ifdef RT_OS_WINDOWS
-    if (DeviceIoControl(g_crvboxhgsmi.hGuestDrv,
-                        VBOXGUEST_IOCTL_HGCM_CONNECT,
-                        &info, sizeof (info),
-                        &info, sizeof (info),
-                        &cbReturned,
-                        NULL))
-#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
-    VBGLBIGREQ Hdr;
-    Hdr.u32Magic = VBGLBIGREQ_MAGIC;
-    Hdr.cbData = sizeof(info);
-    Hdr.pvDataR3 = &info;
-# if HC_ARCH_BITS == 32
-    Hdr.u32Padding = 0;
-# endif
-    if (ioctl(g_crvboxhgsmi.iGuestDrv, VBOXGUEST_IOCTL_HGCM_CONNECT, &Hdr) >= 0)
-#else
-    if (ioctl(g_crvboxhgsmi.iGuestDrv, VBOXGUEST_IOCTL_HGCM_CONNECT, &info, sizeof (info)) >= 0)
-#endif
-    {
-        if (info.result == VINF_SUCCESS)
-        {
-            int rc;
-            conn->u32ClientID = info.u32ClientID;
+    rc = VbglR3InitUser();
+    if (RT_SUCCESS(rc))
+    {
+        uint32_t idClient;
+        rc = VbglR3HGCMConnect("VBoxSharedCrOpenGL", &idClient);
+        if (RT_SUCCESS(rc))
+        {
+            conn->u32ClientID = idClient;
             crDebug("HGCM connect was successful: client id =0x%x\n", conn->u32ClientID);
 
-            rc = crVBoxHGSMISetVersion(conn, CR_PROTOCOL_VERSION_MAJOR, CR_PROTOCOL_VERSION_MINOR);
-            if (!rc)
+            rc = crVBoxHGCMSetVersion(conn, CR_PROTOCOL_VERSION_MAJOR, CR_PROTOCOL_VERSION_MINOR);
+            if (RT_FAILURE(rc))
             {
+                WARN(("crVBoxHGCMSetVersion failed %d", rc));
+/** @todo r=bird: Someone else messed up the return values here, not me.   */
                 return rc;
             }
@@ -1285,25 +1222,20 @@
             rc = crVBoxHGCMSetPID(conn, crGetPID());
 #endif
+            if (RT_FAILURE(rc))
+                WARN(("crVBoxHGCMSetPID failed %Rrc", rc));
+/** @todo r=bird: Someone else messed up the return values here, not me.   */
             VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
             return rc;
         }
-        else
-        {
-            Assert(0);
-            crDebug("HGCM connect failed with rc=0x%x\n", info.result);
-        }
+
+        crDebug("HGCM connect failed: %Rrc\n", rc);
+        VbglR3Term();
     }
     else
-    {
-#ifdef RT_OS_WINDOWS
-        crDebug("IOCTL for HGCM connect failed with rc=0x%x\n", GetLastError());
-#else
-        crDebug("IOCTL for HGCM connect failed with rc=0x%x\n", errno);
-#endif
-    }
+        crDebug("Failed to initialize VbglR3 library: %Rrc\n", rc);
 
     VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
-
     return FALSE;
+
 #else /*#ifdef IN_GUEST*/
     crError("crVBoxHGSMIDoConnect called on host side!");
@@ -1316,11 +1248,4 @@
 static void crVBoxHGSMIDoDisconnect( CRConnection *conn )
 {
-#ifdef IN_GUEST
-    VBoxGuestHGCMDisconnectInfo info;
-# ifdef RT_OS_WINDOWS
-    DWORD cbReturned;
-# endif
-    int i;
-#endif
     VBOXCRHGSMIPROFILE_FUNC_PROLOGUE();
 
@@ -1355,52 +1280,12 @@
     if (conn->u32ClientID)
     {
-        memset (&info, 0, sizeof (info));
-        info.u32ClientID = conn->u32ClientID;
-
-# ifdef RT_OS_WINDOWS
-        if ( !DeviceIoControl(g_crvboxhgsmi.hGuestDrv,
-                               VBOXGUEST_IOCTL_HGCM_DISCONNECT,
-                               &info, sizeof (info),
-                               &info, sizeof (info),
-                               &cbReturned,
-                               NULL) )
-        {
-            crDebug("Disconnect failed with %x\n", GetLastError());
-        }
-# elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
-        VBGLBIGREQ Hdr;
-        Hdr.u32Magic = VBGLBIGREQ_MAGIC;
-        Hdr.cbData = sizeof(info);
-        Hdr.pvDataR3 = &info;
-#  if HC_ARCH_BITS == 32
-        Hdr.u32Padding = 0;
-#  endif
-        if (ioctl(g_crvboxhgsmi.iGuestDrv, VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Hdr) >= 0)
-# else
-        if (ioctl(g_crvboxhgsmi.iGuestDrv, VBOXGUEST_IOCTL_HGCM_DISCONNECT, &info, sizeof (info)) < 0)
-        {
-            crDebug("Disconnect failed with %x\n", errno);
-        }
-# endif
-
+        int rc = VbglR3HGCMDisconnect(conn->u32ClientID);
+        if (RT_FAILURE(rc))
+            crDebug("Disconnect failed with %Rrc\n", rc);
         conn->u32ClientID = 0;
-    }
-
-    /* see if any connections remain */
-    for (i = 0; i < g_crvboxhgsmi.num_conns; i++)
-        if (g_crvboxhgsmi.conns[i] && g_crvboxhgsmi.conns[i]->type != CR_NO_CONNECTION)
-            break;
-
-    /* close guest additions driver*/
-    if (i>=g_crvboxhgsmi.num_conns)
-    {
-# ifdef RT_OS_WINDOWS
-        CloseHandle(g_crvboxhgsmi.hGuestDrv);
-        g_crvboxhgsmi.hGuestDrv = INVALID_HANDLE_VALUE;
-# else
-        close(g_crvboxhgsmi.iGuestDrv);
-        g_crvboxhgsmi.iGuestDrv = INVALID_HANDLE_VALUE;
-# endif
-    }
+
+        VbglR3Term();
+    }
+
     VBOXCRHGSMIPROFILE_FUNC_EPILOGUE();
 #endif /* IN_GUEST */
