CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2001
    Posts
    244

    Resolving Compiler warnings C4311 and C4312

    Can someone please help me resolve this warnings? Ive fixed many of the others, its just these ones that I'm stuck at.

    I'm using Visual Studio 2005 Standard w/SP1. MFC Dialog App (non-unicode)


    warning C4311: 'type cast' : pointer truncation from 'HICON' to 'int'
    warning C4312: 'type cast' : conversion from 'unsigned long' to 'HICON' of greater
    Code:
        #define	BTNST_AUTO_GRAY					(HICON)(0xffffffff - 1L)
        // Set icon when the mouse is IN the button
    	hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, nCxDesiredIn, nCyDesiredIn, 0);
    
    
    	switch ((int)hIconIn) // C4311
    	{
    		case NULL:
    			break;
    		case (int)BTNST_AUTO_GRAY: // C4312
    			hIconOut = BTNST_AUTO_GRAY;
    			break;
    		case (int)BTNST_AUTO_DARKER:
    			hIconOut = BTNST_AUTO_DARKER;
    			break;
    		default:
    			hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, nCxDesiredOut, nCyDesiredOut, 0);
    			break;
    	} // switch


    C4311: 'type cast' : pointer truncation from 'LRESULT (__stdcall *)(HWND,UINT,WPARAM,LPARAM)' to 'DWORD'
    C4312: 'type cast' : conversion from 'LONG' to 'WNDPROC' of greater size
    Code:
    WNDPROC oldWindowProc;
    oldWindowProc = (WNDPROC)::SetWindowLong(GetParent()->m_hWnd, GWL_WNDPROC, (DWORD)SubclassWindowProc); // C4311 and 4312
    Last edited by Anarchi; May 14th, 2009 at 05:59 PM.

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Resolving Compiler warnings C4311 and C4312

    Use static_cast or reinterpret_cast and assign the value to a proper type variable. Then use that variable in the switch statement.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: Resolving Compiler warnings C4311 and C4312

    These warnings are generated only when you have the 'detect 64-bit portability issues' flag (/Wp64) set. The issue is that HICON will be 64-bits in Win64 but int will still be 32-bits.

    If you have no future plans to compile your code for Win64 then you may safely ignore these warnings - or turn of the /Wp64 flag so that they are not generated.

    However, it is always good programming practice not to make assumptions about the sizes of particular types so maybe you should deal with these issues now. The same situation occurred years ago with the migration from 16-bit to 32-bit programmers everywhere had to deal with it then.

  4. #4
    Join Date
    Nov 2001
    Posts
    244

    Re: Resolving Compiler warnings C4311 and C4312

    I would like it to be 64bit compatible so I'll fix it now.

    I am new to casting, how do you properly implement reinterpret_cast?

    Should I remove the switch command and use if() instead for the BTNST_AUTO_xxxx checks?
    And create an int version of hIconIn?


    Would this work:

    int hIconInt = reinterpret_cast<int>(hIconIn);
    Last edited by Anarchi; May 14th, 2009 at 06:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured