Thread: Pointer to long (and vice versa) coversion warnings

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    Pointer to long (and vice versa) coversion warnings

    I recently uprgraded to Visual C++ .NET from 6.0. Things went smoothely for the most part, but the compiler is triggering a warning on something that AFAIK, shouldn't be a problem. Here is the code I tried:
    Code:
    MYSTRUCT * dvp = (MYSTRUCT *) GetWindowLong(hWnd, GWL_USERDATA);
    In 6.0 this worked perfectly, but now I'm getting:

    warning C4312: 'type cast' : conversion from 'LONG' to 'MYSTRUCT *' of greater size

    Well, just to make sure I tried a sizeof on LONG, long, MYSTRUCT *, and void *, but they all equalled 4 bytes just as they should. Normally I'd assume this warning could be safely ignored, but I've been experiencing similar warning and weird bugs in builds of one of my programs that I can't attribute to anything else. Can anyone tell me what's causing this problem?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    This is a 64-bit warning. When you will port your code to 64-bit windows this won't work anymore because a pointer will be 64-bit then and a long will stay 32 (or so I heard). Either use GetWindowLongPtr or turn off the 64-bit warnings and watch your code crash in the future.

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Quote Originally Posted by Laserve
    This is a 64-bit warning. When you will port your code to 64-bit windows this won't work anymore because a pointer will be 64-bit then and a long will stay 32 (or so I heard). Either use GetWindowLongPtr or turn off the 64-bit warnings and watch your code crash in the future.
    Eh, really? Somehow I would've thought it wouldn't be too difficult for microsoft to keep versions of their libs in 64bit windows that were reverse compatible with 32 bit progs, but I suppose that's too much to ask for . Well anyway, I switched which functions I was using but the warnings didn't go away.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> Somehow I would've thought it wouldn't be too difficult for microsoft to keep versions of their libs in 64bit windows <<

    The warnings are designed to warn you about problems that may occur when the code is compiled for 64 bit. Even if you don't want a 64 bit version now, you don't want to have to rewrite your code in six months when you decide you do need a 64 bit version.

    >> Well anyway, I switched which functions I was using but the warnings didn't go away. <<

    Unfortunately, due to the fact that the warning system operates on the code after it has been preprocessed and that GetWindowLongPtr is defined something like:
    Code:
    #if defined(_WIN32)
    #define GetWindowLongPtr GetWindowLong
    #elif defined(_WIN64)
    LONG_PTR GetWindowLongPtr(HWND, int);
    #endif
    you will get the warning even if you use GetWindowLongPtr(). You can either turn off 64bit warnings or just turn it off for this function (possibly):
    Code:
    #pragma warning(disable : 4312)
    MYSTRUCT * dvp = (MYSTRUCT *) GetWindowLong(hWnd, GWL_USERDATA);
    #pragma warning(default : 4312)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to ascii and vice versa
    By thenrkst in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2003, 01:17 AM