Jump to content

User32.dll PrintWindow in a disconnected Remote Desktop session


Recommended Posts

Hi all,

I created a script to take periodic screen shots of an inactive background window by using the PrintWindow command (via some code I found elsewhere on the forum). I run this script on my Windows 7 PC at home, but I am usually connected to it through Remote Desktop while at work or even while at home through my laptop. The script works well when I am connected, but the moment I disconnect (e.g. when I am headed home from the office) PrintWindow begins capturing an entirely black bitmap.

I know the background app continues processing while in a disconnected state (just as the script does), so maybe it has something to do with the way the desktop window manager works? I've tried having the script reinitialize the DC/bitmap in the while loop upon detection of the black bitmap, but it didn't have any effect so I took it back out. I've enclosed the relevant snippets of code below. Normally if I didn't connect via remote desktop at all, the session would be just be a normal local PC session (console session) and would run without issue. Perhaps if there were a way my script could convert the disconnected session into a console session that would do the trick too, but I have no idea where to begin with that.

Any help would be greatly appreciated. Thanks!

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <ScreenCapture.au3>
$loopcounter = 0
$hWnd = WinGetHandle("TestWindow")
;Initialize the DC/bitmap
$iWidth = _WinAPI_GetWindowWidth($hWnd)
$iHeight = _WinAPI_GetWindowHeight($hWnd)
$hDDC = _WinAPI_GetDC($hWnd)
$hCDC = _WinAPI_CreateCompatibleDC($hDDC)
$hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)
_WinAPI_SelectObject($hCDC, $hBMP)
While 1
DllCall("User32.dll", "int", "PrintWindow", "hwnd", $hWnd, "hwnd", $hCDC, "int", 0)
_ScreenCapture_SaveImage("c:\testimage" & $loopcounter & ".jpg", $hBMP)
_WinAPI_ReleaseDC($hWnd, $hDDC)
_GDIPlus_ShutDown ()
Edited by tko13
Link to comment
Share on other sites

Didn't figure out why this behavior occurs, but I did come up with a workaround and wanted to post it in case anyone else runs into this in the future. I am now using TSCON.EXE to convert my disconnected rdp session into the console session: tscon.exe 2 /dest:Console. I set the script up to trigger this command after several cycles of detecting a black bitmap through the DC. This way, the script doesn't subvert the normal rdp client behavior of auto-reconnect on a short network interruption. If this is triggered inadvertently in the future, I may go a step further and have it verify that the rdp session is disconnected before converting the session.

Edited by tko13
Link to comment
Share on other sites

Hi. I have problems with tscon, it seems you have to have an administrator account, and I can't get it to work in a user account.

I will try to add the user into admin group, execute the tscon, and remove it from admin, but even if it works it is cumbersome !

Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...