• entries
557
1237
• views
423614

# Untitled

108 views

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].

lol steve you resorted to macro hax!!

[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

Quote:
 Original post by jollyjeffers [lol] @ code comments [sick] @ syntax! I'm a fan of C++ style casts, static_cast(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...)

## Create an account

Register a new account