Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    557
  • comments
    1237
  • views
    423123

Untitled

Sign in to follow this  
Evil Steve

89 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].
Sign in to follow this  


3 Comments


Recommended Comments

[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

Share this comment


Link to comment
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...)

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!