From db94233cc4f8741b0c72c3358ea678a9b14e1bfa Mon Sep 17 00:00:00 2001 From: Rom Walton Date: Tue, 12 Jul 2016 20:34:18 -0700 Subject: [PATCH] VBOX: Attempt to wake the console before attempting to take a screenshot, to work around a VirtualBox bug. --- samples/vboxwrapper/vbox_mscom_impl.cpp | 19 ++++++++++++++++--- samples/vboxwrapper/vbox_vboxmanage.cpp | 5 +++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/samples/vboxwrapper/vbox_mscom_impl.cpp b/samples/vboxwrapper/vbox_mscom_impl.cpp index c8e1b1c8fe..dea4742b0c 100644 --- a/samples/vboxwrapper/vbox_mscom_impl.cpp +++ b/samples/vboxwrapper/vbox_mscom_impl.cpp @@ -1612,12 +1612,13 @@ int VBOX_VM::capture_screenshot() { GuestMonitorStatus monitorStatus; string virtual_machine_slot_directory; string screenshot_location; - HRESULT rc; + HRESULT rc, rc2; FILE* f = NULL; SAFEARRAY* pScreenshot = NULL; CComSafeArray aScreenshot; CComPtr pConsole; CComPtr pDisplay; + CComPtr pKeyboard; get_slot_directory(virtual_machine_slot_directory); @@ -1627,15 +1628,24 @@ int VBOX_VM::capture_screenshot() { if (CHECK_ERROR(rc)) { } else { rc = pConsole->get_Display(&pDisplay); - if (CHECK_ERROR(rc)) { + rc2 = pConsole->get_Keyboard(&pKeyboard); + if (CHECK_ERROR(rc) || CHECK_ERROR(rc2)) { } else { + // Due to a recently fixed bug in VirtualBox we are going to attempt to prevent receiving garbage + // by waking up the console. We'll attempt to virtually tap the 'spacebar'. + pKeyboard->PutScancode(0x39); + boinc_sleep(1); + rc = pDisplay->GetScreenResolution(0, &width, &height, &bpp, &xOrigin, &yOrigin, &monitorStatus); if (CHECK_ERROR(rc)) { } else { + vboxlog_msg("Retrieving screenshot from VirtualBox."); rc = pDisplay->TakeScreenShotToArray(0, width, height, BitmapFormat_PNG, &pScreenshot); if (SUCCEEDED(rc)) { aScreenshot.Attach(pScreenshot); + vboxlog_msg("Writing screenshot to disk."); + screenshot_location = virtual_machine_slot_directory; screenshot_location += "/"; screenshot_location += SCREENSHOT_FILENAME; @@ -1647,8 +1657,11 @@ int VBOX_VM::capture_screenshot() { } else { vboxlog_msg("Failed to write screenshot to disk."); } - } + } else { + vboxlog_msg("Failed to retrieving screenshot from VirtualBox."); + } } + } } diff --git a/samples/vboxwrapper/vbox_vboxmanage.cpp b/samples/vboxwrapper/vbox_vboxmanage.cpp index 92af540581..1b763be768 100644 --- a/samples/vboxwrapper/vbox_vboxmanage.cpp +++ b/samples/vboxwrapper/vbox_vboxmanage.cpp @@ -1173,6 +1173,11 @@ int VBOX_VM::capture_screenshot() { vboxlog_msg("Capturing screenshot."); + command = "controlvm \"" + vm_name + "\" "; + command += "keyboardputscancode 0x39"; + vbm_popen(command, output, "put scancode", true, true, 0); + boinc_sleep(1); + command = "controlvm \"" + vm_name + "\" "; command += "screenshotpng \""; command += virtual_machine_slot_directory;