Desktop screen shot function failed in RDP
-
Hi All, I have one program thats taking the screen shot of desktop and save in a file.But this program is running on remote machine.I connect this machine with RDP.Problem is When i connected to machine program is working fine and taking screen shots ,but when i disconnected from RDP or minimized it it failed. I found that, The Bitblt function of the following sample code is falied with Error 6.Otherwise its working fine when i m connected to it. Error 6 means Invalid handle. Can anybody tell why its failed of any other method of taking screen shot of desktop.? Thanx in advance.. ////////////////////////////////////////// BOOL CBitmapFile::SaveDesktopAsFile(LPTSTR szFile, int nFileType) { CString strFileName, strError = ""; CWnd* pWnd = CWnd::GetDesktopWindow(); CBitmap bitmap; CWindowDC dc(pWnd); CDC memDC; CRect rect; strFileName = (CString)szFile; memDC.CreateCompatibleDC(&dc); pWnd->GetWindowRect(rect); bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() ); CBitmap* pOldBitmap = memDC.SelectObject(&bitmap); memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY); // Create logical palette if device support a palette CPalette pal; if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE ) { UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256); LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; pLP->palVersion = 0x300; pLP->palNumEntries = GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry ); // Create the palette pal.CreatePalette( pLP ); delete[] pLP; } // Convert the bitmap to a DIB HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal ); if( hDIB == NULL ) return FALSE; // DrawGreyScale( &memDC, hDIB ); memDC.SelectObject(pOldBitmap); // Write it to file // Write it to file if(nFileType == BMP_FILE) WriteDIB( szFile, hDIB ); else JpegFromDib(hDIB, 100, strFileName, &strError); // Free the memory allocated by DDBToDIB for the DIB GlobalFree( hDIB ); return TRUE; } //////////////////////////////////////////
-
Hi All, I have one program thats taking the screen shot of desktop and save in a file.But this program is running on remote machine.I connect this machine with RDP.Problem is When i connected to machine program is working fine and taking screen shots ,but when i disconnected from RDP or minimized it it failed. I found that, The Bitblt function of the following sample code is falied with Error 6.Otherwise its working fine when i m connected to it. Error 6 means Invalid handle. Can anybody tell why its failed of any other method of taking screen shot of desktop.? Thanx in advance.. ////////////////////////////////////////// BOOL CBitmapFile::SaveDesktopAsFile(LPTSTR szFile, int nFileType) { CString strFileName, strError = ""; CWnd* pWnd = CWnd::GetDesktopWindow(); CBitmap bitmap; CWindowDC dc(pWnd); CDC memDC; CRect rect; strFileName = (CString)szFile; memDC.CreateCompatibleDC(&dc); pWnd->GetWindowRect(rect); bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() ); CBitmap* pOldBitmap = memDC.SelectObject(&bitmap); memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY); // Create logical palette if device support a palette CPalette pal; if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE ) { UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256); LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; pLP->palVersion = 0x300; pLP->palNumEntries = GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry ); // Create the palette pal.CreatePalette( pLP ); delete[] pLP; } // Convert the bitmap to a DIB HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal ); if( hDIB == NULL ) return FALSE; // DrawGreyScale( &memDC, hDIB ); memDC.SelectObject(pOldBitmap); // Write it to file // Write it to file if(nFileType == BMP_FILE) WriteDIB( szFile, hDIB ); else JpegFromDib(hDIB, 100, strFileName, &strError); // Free the memory allocated by DDBToDIB for the DIB GlobalFree( hDIB ); return TRUE; } //////////////////////////////////////////
Well, my guess would be that the desktop you are making screen shots of simply doesn't exist when there's noone looking at it, understand that as in, no remote desktop connection is made. I think when you connect to the RDP server it will create something like a virtual desktop for you and will close and destroy it once you are done using RDP and log out. So my guess is that you get NULL when you call GetDesktopWindow or maybe you get 0 for its width and height, or you just don't get a DC for it. Does this make any sense?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
-
Well, my guess would be that the desktop you are making screen shots of simply doesn't exist when there's noone looking at it, understand that as in, no remote desktop connection is made. I think when you connect to the RDP server it will create something like a virtual desktop for you and will close and destroy it once you are done using RDP and log out. So my guess is that you get NULL when you call GetDesktopWindow or maybe you get 0 for its width and height, or you just don't get a DC for it. Does this make any sense?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
Code-o-mat wrote:
So my guess is that you get NULL when you call GetDesktopWindow...
So then wouldn't the subsequent call to
GetWindowRect()
fail?"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Well, my guess would be that the desktop you are making screen shots of simply doesn't exist when there's noone looking at it, understand that as in, no remote desktop connection is made. I think when you connect to the RDP server it will create something like a virtual desktop for you and will close and destroy it once you are done using RDP and log out. So my guess is that you get NULL when you call GetDesktopWindow or maybe you get 0 for its width and height, or you just don't get a DC for it. Does this make any sense?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
I have checked it with writing a log file. All pointers and rect are looking normal. But the function bitblt is failed. Why i dont know and the GetlastError gives Error 6. which is invalid handle. Is there any api to check HDC or CDC object is valid or not. bcoz it looks normal handle and pointer. Anyway thanx for reply.