I have this piece of code

// some function
int px;
void SomeFunction( void *dummy )
{
	...

	RECT rect;
	GetWindowRect(hWnd, &rect);
	px = ((rect.right - rect.left) / 2) - 60;

	...

	// Terminate thread
	_endthread();
}

And my problem is px doesn't get assigned.. it stays 0.. although in the debugger I can see all the values inside rect..
please help me

What are the values of RECT members?

Thank you mcco for providing me the definition of RECT structure

kindly provide me the values (numbers), It seems that your expression evaluates to zero thats why I've asked to provide me the values not the RECT members. :).

I have this piece of code

// some function
int px;
void SomeFunction( void *dummy )
{
	...

	RECT rect;
	GetWindowRect(hWnd, &rect);
	px = ((rect.right - rect.left) / 2) - 60;

	...

	// Terminate thread
	_endthread();
}

And my problem is px doesn't get assigned.. it stays 0.. although in the debugger I can see all the values inside rect..
please help me

What about if you change px from int to long? Does that give you any differemt results?
The thing to take note of here is that px is currently declared as an int. Whereas rect.right and rect.left are both LONG/long variables.

Therefore if the result of your calculation is less than 1.0 (e.g. something like 0.43455664), then because px is an int, the calculated value will be truncated to 0.
Whereas if you alter px to be long, you'll get the actual calculated value (in my example 0.4345564).

I could be wrong, but that looks like the most obvious explanation to me!

oh, hang on.....Looking at that code, another thing strikes me as odd....
px is declared outside of the function, but used inside it..
That's probably your problem right there!
The px used in the function and the px outside of the function might not be the same variable, they might have different scope.
If you could post some more code, we might be able to determine whether or not this is the case!

Cheers for now,
Jas.

commented: Two px's in scope could easily explain it +36

What about if you change px from int to long? Does that give you any differemt results?
The thing to take note of here is that px is currently declared as an int. Whereas rect.right and rect.left are both LONG/long variables.

Therefore if the result of your calculation is less than 1.0 (e.g. something like 0.43455664), then because px is an int, the calculated value will be truncated to 0.
Whereas if you alter px to be long, you'll get the actual calculated value (in my example 0.4345564).

A 'long' variable in this context is still an integer, not a float, so changing px to long will make no difference.
The most likely explanation, as you suggested, is that px has also been declared locally and the OP is examining the global variable.

commented: Well pointed out! Another of my window-licker moments! :) +2

Try
::px = ((rect.right - rect.left) / 2) - 60;

I am assuming there is a local variable hiding the global.

Check out if you have declared a variable inside SomeFunction with the same name as px, in this case the local variable is used and the global variable is cached.

Sorry the global variable is hidden not cached

oh, hang on.....Looking at that code, another thing strikes me as odd....
px is declared outside of the function, but used inside it..
That's probably your problem right there!
The px used in the function and the px outside of the function might not be the same variable, they might have different scope.
If you could post some more code, we might be able to determine whether or not this is the case!

Cheers for now,
Jas.

Thanks! that was my problem!
this function is pretty long and I forgot that I have another px in it.

A 'long' variable in this context is still an integer, not a float, so changing px to long will make no difference.
The most likely explanation, as you suggested, is that px has also been declared locally and the OP is examining the global variable.

Of course...Sorry, another window-licker moment of mine. I think I was confusing long for double, I saw long and my brain was thinking double....What a dumbass! heh heh!

I think some extra coffee is in order!

Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.