🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Untitled

posted in DruinkJournal
Published September 11, 2006
Advertisement
LOL POINTER TRUNCATION!!1

Ok, so what is the correct way to use SetWindowLongPtr? Doing this:
SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);
works fine in x64, but gives the warning "warning C4244: 'argument' : conversion from 'LONG_PTR' to 'LONG', possible loss of data" in Win32.
So, I changed it to:
SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG)(LONG_PTR)this);
Expecting LONG to be 64 bits in x64. Wrong.

It worked fine (surprisingly) till shutdown, when I got a couple of access violations (Which didn't crash the app, however), sue to the pointer getting truncated to 32 bits.

So, my fix was:
// Fucking Microsoft headers#ifdef _WIN64#	define CAST_TYPE	(LONG_PTR)#else#	define CAST_TYPE	(LONG)(LONG_PTR)#endif// Later on...SetWindowLongPtr(m_hWnd, GWLP_USERDATA, CAST_TYPE this);


Surely there's a nicer way?

On the plus side, x64 works fine now. Yaaaaaay [smile].
Previous Entry Untitled
Next Entry Untitled
0 likes 3 comments

Comments

Mushu
lol steve you resorted to macro hax!!
September 11, 2006 11:15 AM
jollyjeffers
[lol] @ code comments

[sick] @ syntax!

I'm a fan of C++ style casts, static_cast<type>(expr) for example, but your current macro looks like non-code... without knowing what your macro's definition is I'd read that code as being uncompilably broken [oh]

Jack
September 11, 2006 12:21 PM
Evil Steve
Quote: Original post by jollyjeffers
[lol] @ code comments

[sick] @ syntax!

I'm a fan of C++ style casts, static_cast<type>(expr) for example, but your current macro looks like non-code... without knowing what your macro's definition is I'd read that code as being uncompilably broken [oh]

Jack
Yeah, the comment is because it all resolves down to something that won't compile in Win32 mode, which is somewhat retarded.

I should really learn to use the C++ casts. I'm all self taught, and a lot of it is from reading other peoples code - I taught myself C from Quake C, Allegro stuff, and online tutorials before I actually got a proper C++ book. I think I know how to use C++ casts (static_cast is a cast from related types, reinterpret_cast is mainly int->pointer conversion [unrelated types], and dynamic_cast is the work of the devil. I'll implement RTTI for the classes I need, not everything...)
September 11, 2006 04:35 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement