GetWindowDC(NULL) failing
I've a function that calls GetWindowDC(NULL)
. and that function is called by different threads. sometimes I see GetWindowDC(NULL)
returning 0x0
Why ? and How to resolve ? the function is mutex gaurded properly. So thats not a Problem. Do I need to sleep()
few ms after each call ? and Why ?
http://msdn.microsoft.com/en-us/library/dd144947%28v=vs.85%29.aspx
msdn Says
Return Value
If the function succeeds, the return value is a handle to a device context for the specified window. If the function fails, the return value is NULL, indicating an error or an invalid hWnd parameter.
but What 开发者_运维问答this indicating an error means I couldn't understand
Edit:
and Ya I am releasing all the DCs and also Deleting the DCs properly
Giving an immediate call toGetLastError
retrns 0
tried.
if(hdc == 0x0)
qDebug() << GetLastError()
Here goes my source code. It Works but after working more than a 100 times (35-40 secs) it refuses to work and GetWindowDC(NULL)
returns 0
QPixmap Util::grabScreen(const DG::Rect* rect){
mutex.lock();
HDC hdc=GetWindowDC(NULL);
HWND win=WindowFromDC(hdc);
HDC cdc=CreateCompatibleDC(hdc);
HBITMAP temp=CreateCompatibleBitmap(hdc,rect->width,rect->height);
PAINTSTRUCT ps;
hdc=BeginPaint(win,&ps);
HBITMAP oldb=(HBITMAP)SelectObject(cdc,temp);
BitBlt(cdc,0,0,rect->width,rect->height,hdc,rect->top,rect->left,SRCCOPY);
SelectObject(cdc,oldb);
EndPaint(win,&ps);
char* buff;
buff = new char[rect->size()];
GetBitmapBits(temp,rect->size(),buff);
qDebug() << "temp" << temp;
if(temp == 0x0){
qDebug() << "hdc" << hdc;
}
DeleteDC(cdc);
ReleaseDC(NULL, hdc);
DeleteDC(hdc);
QPixmap pixmap = QPixmap::fromWinHBITMAP(temp);
//QPixmap pixmap = QPixmap::grabWidget(desktopWidget,rect->toQRect());
mutex.unlock();
return pixmap;
}
According to MSDN you shouldn't call DeleteDC
on an handle obtained using GetDC
. You should just call ReleaseDC
instead.
Without know more details on what is going on; the scenario sounds a bit odd to me. However...
Is it possible that code is also processing a WM_PAINT for the same window at the same time you are trying to get the WindowDC ?
I'm not sure if you can obtain a window DC for the same window that is being actively painted.
精彩评论