Compilation error SetWindowPos on 64-bit Windows

I am currently modifying my code base to compile it under 64-bit architecture. Most of the changes I need to make are obvious, but it puzzled me. SetWindowPos has a second argument, hWndInsertAfter , which can be either a window handle or one of the predefined values HWND_TOP , HWND_BOTTOM , HWND_TOPMOST and HWND_NOTOPMOST (see here for MSDN information). These values ​​are defined in WinUser.h .

In a 32-bit architecture, using one of them in a call to SetWindowPos works fine, but in a 64-bit compiler it complains in this way:

warning C4306: 'type cast': conversion from 'int' to 'HWND' of larger size

This is because #defines distinguishes integers [32 bits] as HWNDs, for example:

#define HWND_TOPMOST ((HWND)-1)

What do I need to change to make this compilation in 64-bit architecture without a compiler throwing a warning? I can turn off warnings with #pragma warning( disable: 4306 ) or make my own definition using a 64-bit int in #define, but of course, is there a β€œright” way for Microsoft to do this?

+7
c winapi visual-studio-2012
source share
2 answers

Well, after testing MUCH, the problem was that my file was a .c file. I renamed it to .cpp and SetWindowPos, and then compiled without errors (and, conversely, in the new test application that I created to try bare bones, when I renamed the default .cpp file to a .c file, it started to complain).

It seems that the .c files do not want to use 32-bit int values ​​for 64-bit pointers. This makes sense, but does not explain why it works in .cpp files. If anyone has any idea why this is so, note this in the comments ...

+1
source share

The warning is triggered because you pass the 32-bit value of int -1 to the 64-bit pointer type void* without any intermediate cast to 64-bit integer type, for example intptr_t . MSVC should have suppressed the warning in this case, because (A) it was only caused by the extension of the macro provided by the system HWND_TOPMOST and (B), the decimal literal was insulting to the int , but MSVC developers apparently did not think of heuristics.

There is nothing to do in the code to turn off the warning if you are not happy

 #undef HWND_TOPMOST #define HWND_TOPMOST ((HWND)(intptr_t)-1) 

Alternatively, you can try to suppress it in the IDE. This thread offers

Project Settings | C / C ++ | General and disable "Detecting 64-bit portability problems"

or pass /wd4306 on the command line.

+1
source share

All Articles