type cast warnings

Started by
5 comments, last by Colin Jeanne 18 years, 8 months ago
I have two warnings when I try to type cast. There probably not a big deal since I used the code from a book. Its when you use a static window procedure and type cast it into the class. The code from the book compiles fine. Is there something in the compiler settings?


SetWindowLong(hWnd, GWL_USERDATA, (long)((LPCREATESTRUCT(lParam))->lpCreateParams));

warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'long'

//AND

(IApplication*)(GetWindowLong(hWnd, GWL_USERDATA));

warning C4312: 'type cast' : conversion from 'LONG' to Impulse::Core::IApplication *' of greater size


If you insist on saying "DUH", try to make a post that makes a little more sense
Advertisement
A pointer may be bigger than a long, e.g. on a 64-bit platform. There's an option to disable that check in the project settings.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I couldn't find anything. I don't know what it would say or where it would be. I am using vs2003. I did fix the first warning with PtrToUlong()
If you insist on saying "DUH", try to make a post that makes a little more sense
You can use #pragma's to disable these sort of warnings. I'd just encapsulate them in #ifdef _WIN32/#endif so you can keep them completely OS independant. 2 lines of code makes life much easier.
It still gives the warning
If you insist on saying "DUH", try to make a post that makes a little more sense
*sigh*

(Solution Explorer)→(Project name)→(Right Click)→Properties→C/C++→General→Detect 64-bit Portability Issues→No.

Or just don't cast your pointers to LONG (size_t is a safer bet).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Use (Set|Get)WindowLongPtr() instead.

This topic is closed to new replies.

Advertisement