Index: trunk/src/VBox/Main/src-client/GuestProcessImpl.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/GuestProcessImpl.cpp	(revision 45694)
+++ trunk/src/VBox/Main/src-client/GuestProcessImpl.cpp	(revision 45697)
@@ -736,5 +736,6 @@
 
     com::SafeArray<BYTE> data((size_t)dataCb.cbData);
-    data.initFrom((BYTE*)dataCb.pvData, dataCb.cbData);
+    if (dataCb.cbData)
+        data.initFrom((BYTE*)dataCb.pvData, dataCb.cbData);
 
     fireGuestProcessOutputEvent(mEventSource, mSession, this,
@@ -755,9 +756,15 @@
     /* pcbRead is optional. */
 
-    /** @todo Validate uHandle. */
-
     AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
 
-    if (mData.mStatus != ProcessStatus_Started)
+    if (   mData.mStatus != ProcessStatus_Started
+        /* Skip reading if the process wasn't started with the appropriate
+         * flags. */
+        || (   (   uHandle == OUTPUT_HANDLE_ID_STDOUT
+                || uHandle == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED)
+            && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdOut))
+        || (   uHandle == OUTPUT_HANDLE_ID_STDERR
+            && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdErr))
+       )
     {
         if (pcbRead)
@@ -780,14 +787,12 @@
         paParms[i++].setUInt32(0 /* Flags, none set yet. */);
 
+        alock.release(); /* Drop the write lock before sending. */
+
         vrc = sendCommand(HOST_EXEC_GET_OUTPUT, i, paParms);
     }
 
     if (RT_SUCCESS(vrc))
-    {
-        alock.release(); /* Drop the write lock before waiting. */
-
         vrc = waitForOutput(uHandle, uTimeoutMS,
                             pvData, cbData, pcbRead);
-    }
 
     LogFlowFuncLeaveRC(vrc);
@@ -904,4 +909,6 @@
         if (RT_SUCCESS(vrc))
             vrc = mData.mProcess.mEnvironment.BuildEnvironmentBlock(&pvEnv, &cbEnv, NULL /* cEnv */);
+
+        uint32_t uTimeoutMS = mData.mProcess.mTimeoutMS;
 
         if (RT_SUCCESS(vrc))
@@ -939,5 +946,5 @@
                 paParms[i++].setUInt32(UINT32_MAX /* Infinite timeout */);
             else
-                paParms[i++].setUInt32(mData.mProcess.mTimeoutMS);
+                paParms[i++].setUInt32(uTimeoutMS);
             if (uProtocol >= 2)
             {
@@ -950,4 +957,6 @@
             }
 
+            alock.release(); /* Drop the write lock before sending. */
+
             /* Note: Don't hold the write lock in here. */
             vrc = sendCommand(HOST_EXEC_CMD, i, paParms);
@@ -963,12 +972,7 @@
             RTStrFree(pszArgs);
 
-        uint32_t uTimeoutMS = mData.mProcess.mTimeoutMS;
-
-        /* Drop the write lock again before waiting. */
-        alock.release();
-
         if (RT_SUCCESS(vrc))
         {
-            vrc = waitForStatusChange(ProcessWaitForFlag_Start, 30 * 1000 /* Timeout */,
+            vrc = waitForStatusChange(ProcessWaitForFlag_Start, uTimeoutMS,
                                       NULL /* Process status */, pGuestRc);
         }
@@ -1055,16 +1059,13 @@
         paParms[i++].setUInt32(mData.mPID);
 
+        alock.release(); /* Drop the write lock before sending. */
+
         vrc = sendCommand(HOST_EXEC_TERMINATE, i, paParms);
     }
 
     if (RT_SUCCESS(vrc))
-    {
-        alock.release(); /* Drop the write lock before waiting. */
-
         vrc = waitForStatusChange(ProcessWaitForFlag_Terminate,
                                   30 * 1000 /* 30s timeout */,
                                   NULL /* ProcessStatus */, pGuestRc);
-    }
-
     LogFlowFuncLeaveRC(vrc);
     return vrc;
@@ -1267,5 +1268,5 @@
     if (SUCCEEDED(hr))
     {
-        LogFlowThisFunc(("Waiting for guest file event(s) (timeout=%RU32ms, %zu events) ...\n",
+        LogFlowThisFunc(("Waiting for guest process event(s) (timeout=%RU32ms, %zu events) ...\n",
                          uTimeoutMS, arrEventTypes.size()));
 
@@ -1427,16 +1428,21 @@
                     ComAssertComRC(hr);
                     size_t cbRead = data.size();
-                    if (   cbRead
-                        && cbRead <= cbData)
+                    if (cbRead)
                     {
-                        memcpy(pvData, data.raw(), data.size());
+                        if (cbRead <= cbData)
+                        {
+                            /* Copy data from event into our buffer. */
+                            memcpy(pvData, data.raw(), data.size());
+                        }
+                        else
+                            vrc = VERR_BUFFER_OVERFLOW;
                     }
-                    else
-                        vrc = VERR_BUFFER_OVERFLOW;
                 }
                 if (pcbRead)
                 {
-                    hr = pProcessEvent->COMGETTER(Processed)((ULONG*)pcbRead);
+                    ULONG cbRead;
+                    hr = pProcessEvent->COMGETTER(Processed)(&cbRead);
                     ComAssertComRC(hr);
+                    *pcbRead = (uint32_t)cbRead;
                 }
 
@@ -1525,4 +1531,6 @@
         paParms[i++].setUInt32(cbData);
 
+        alock.release(); /* Drop the write lock before sending. */
+
         vrc = sendCommand(HOST_EXEC_SET_INPUT, i, paParms);
     }
@@ -1530,6 +1538,4 @@
     if (RT_SUCCESS(vrc))
     {
-        alock.release(); /* Drop the write lock before waiting. */
-
         ProcessInputStatus_T inputStatus;
         uint32_t cbProcessed;
@@ -1593,5 +1599,5 @@
     }
 
-    LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64\n", vrc, cbRead));
+    LogFlowThisFunc(("rc=%Rrc, cbRead=%RU32\n", vrc, cbRead));
 
     LogFlowFuncLeaveRC(vrc);
@@ -1736,8 +1742,10 @@
     if (FAILED(autoCaller.rc())) return autoCaller.rc();
 
+    com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
+
     HRESULT hr = S_OK;
 
-    com::SafeArray<BYTE> data(ComSafeArrayInArg(aData)); int guestRc;
-    int vrc = writeData(aHandle, aFlags, data.raw(), data.size(), aTimeoutMS, (uint32_t*)aWritten, &guestRc);
+    uint32_t cbWritten; int guestRc;
+    int vrc = writeData(aHandle, aFlags, data.raw(), data.size(), aTimeoutMS, &cbWritten, &guestRc);
     if (RT_FAILURE(vrc))
     {
@@ -1756,5 +1764,7 @@
     }
 
-    LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, aWritten));
+    LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, cbWritten));
+
+    *aWritten = (ULONG)cbWritten;
 
     LogFlowFuncLeaveRC(vrc);
Index: trunk/src/VBox/Main/src-client/GuestSessionImpl.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/GuestSessionImpl.cpp	(revision 45694)
+++ trunk/src/VBox/Main/src-client/GuestSessionImpl.cpp	(revision 45697)
@@ -1358,17 +1358,13 @@
                          pCurProc->getObjectID(), mData.mSession.mID, uPID, mData.mProcesses.size() - 1, mData.mNumObjects - 1));
 
-            LogFlowFunc(("1\n"));
             mData.mProcesses.erase(itProcs);
             mData.mNumObjects--;
 
-            LogFlowFunc(("2\n"));
             fireGuestProcessRegisteredEvent(mEventSource, this /* Session */, NULL /* Process */,
                                             uPID, false /* Process unregistered */);
-            LogFlowFunc(("3\n"));
             rc = VINF_SUCCESS;
             break;
         }
 
-        LogFlowFunc(("4\n"));
         itProcs++;
     }
