I have the following code:

HWND hwn= FindWindow(NULL,"CABAL");
HDC hdc = GetDC(hwn);
COLORREF x;
POINT m;
m.x=50;
m.y=50;
ScreenToClient(hwn,&m);
x=gp(hdc,m.x,m.y);
if(x==CLR_INVALID)
cout<<"CLR_INVALID\n";
cout<<"Red: "<<(int)GetRValue(x)<<"\n"<<"Green: "<<(int)GetGValue(x)<<"\n"<<"Blue: "<<(int)GetBValue(x)<<"\n";

The problem is gp( GetPixel ) always returns CLR_INVALID and white (255,255,255).
Also, I find it strange that SetPixel works with any coordonates.
Any ideas?

First off, are you ending that bit of code with ReleaseDC()?
If not that might be a GDI memory leak causing the issue

Secondly (Sorry, premature post), could you post the code to gp()?
Is it

COLORREF gp(HDC hdc,int x,int y){
return GetPixel(hdc,x,y);
}

?

I suggest you to add following defensive code to your code.

1. Check the hwn and hdc for not NULL, if they null output a message.
2. use GetClientRect() API and make sure that your x=50,y=50 client coordinates
are inside the client rectangle.

@wavsyntax: 1st: yes, I am ending with ReleaseDC
2nd: made a copy of gdi32.dll ,renamed it and calling getpixel from there
@NicAx64: 1: i'm 100% sure hwn & hdc aren't NULL , how could SetPixel work if they were?

I will try GetClientRect() tho, hope it works.

RECT rct;
POINT topLeft;
POINT bottomRight;
int l = GetClientRect(hwn, &rct);
cout<<"GetClientRect= "<<l;
topLeft.x = rct.left;
topLeft.y = rct.top;
bottomRight.x = rct.right;
bottomRight.y = rct.bottom;
ScreenToClient(hwn, &topLeft);
ScreenToClient(hwn, &bottomRight);*/
x=gp(hdc,bottomRight.x,bottomRight.y);

Not sure if I used it correctly but if I did, it still doesn't work (still CLR_INVALID)

This article has been dead for over six months. Start a new discussion instead.