2

I have a bank of virtual machines (running windows) that I remote into. As such, none of these machines have a monitor attached, they are only accessed by Remote Desktop.

I want to get a screenshot of an application that is running on the desktop. What I have found is that if I am not connected via Remote Desktop, then the screen is not rendering and I am unable capture the screen (the best I've managed is getting a black image).

Is there any way to force the desktop to render for the purpose of my screen grab?

EDIT: OK to be more specific, here is some Python code that takes a screenshot provided I am remoted in to the machines:

import win32ui
import win32gui
hwnd = win32gui.FindWindow(None, window_name)
wDC = win32gui.GetWindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (width, height), dcObj, (0, 0), win32con.SRCCOPY)
dataBitMap.SaveBitmapFile(cDC, image_name)
# Free Resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())

If I run this while I am remoted in, it works fine. As soon as I am not remoted in, I get the following error:

win32ui.error: BitBlt failed

This error is a result of the screen not being rendered when no one is remoted in.

I need a solution that will allow me to get a screenshot in this scenario, when I am not connected via remote desktop.

EDIT 2: To be clear, the code is running on the VM itself. But it is running when no-one is remoted in to the machine.

Brynn McCullagh
  • 4,083
  • 2
  • 17
  • 12
  • There's some chance the instructions here might help: http://superuser.com/questions/62051/is-there-a-way-to-fake-a-dual-second-monitor – adv12 Feb 18 '15 at 15:16
  • This is incredibly off-topic and my knowledge of virtual machines is anything but great. But I would say you might want to look into a Remote Desktop Protocol implementation for these purposes. – tom Feb 18 '15 at 15:20
  • 4
    What did I do that earned this question so many downvotes? I would like to know for future reference... – Brynn McCullagh Feb 18 '15 at 15:46
  • I'm voting to re-open as I know the answer. – Joshua Feb 18 '15 at 16:15
  • It's not clear if you're trying to obtain a screenshot with code running in the VM on the same desktop, running in the same VM but on another desktop, or running outside the VM on the host. – Ross Ridge Feb 18 '15 at 20:06
  • This question seems fine to me. – Jonathan Potter Feb 18 '15 at 21:37
  • Ross, I'm running the code on the VM itself, but doing so while I'm not remoted in. I will update the question to make this absolutely clear. – Brynn McCullagh Feb 19 '15 at 09:35
  • Joshua - The question is open now, I would greatly appreciate it if you put the answer. – Brynn McCullagh Feb 25 '15 at 16:59

1 Answers1

0

Obvious workaround is using 2 virtual machines: master host runs remote session for target one. It also allows input actions like mouse_event or keybd_event. The only one requirement is not to minimize RDP window (or VNC software, it's doesn't matter), though it may be out of focus.

It's widely used method for build/test machines pool. I worked in big testing team several years and never heard about other approaches.

P.S. How about Pillow or pyscreenshot?

Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • 1
    It's an interesting idea. Ideally I'd like to be able to screenshot without being remoted in at all. If I went this route I'd need to be able to automate a one VM establishing a remote desktop connection to all the others, and making sure it maintains the connection (catering for disconnects, rebooots, other users remoting in, etc.), which would be tricky. – Brynn McCullagh Mar 02 '15 at 11:12
  • I've marked this as correct as, ultimately, this is pretty much what we did to solve the problem. Thanks for the response. – Brynn McCullagh Sep 10 '15 at 11:25