winapi - GetWindowDC(NULL) failing -


i've function calls getwindowdc(null). , function called different threads. see getwindowdc(null) returning 0x0 why ? , how resolve ? function mutex gaurded properly. thats not problem. need sleep() few ms after each call ? , why ?

http://msdn.microsoft.com/en-us/library/dd144947%28v=vs.85%29.aspx

msdn says

return value

if function succeeds, return value handle device context specified window. if function fails, return value null, indicating error or invalid hwnd parameter.

but indicating error means couldn't understand

edit:

and ya releasing dcs , deleting dcs properly
giving immediate call getlasterror retrns 0 tried.

if(hdc == 0x0)     qdebug() << getlasterror() 

here goes source code. works after working more 100 times (35-40 secs) refuses work , 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 msdn shouldn't call deletedc on handle obtained using getdc. should call releasedc instead.


Comments