This is the code I have:
HWND WebformCreate(HWND hParent, UINT id)
{
return CreateWindowEx(0, WEBFORM_CLASS, _T("about:blank"),
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 0, 0, 100, 100, hParent,
(HMENU)id, GetModuleHandle(NULL), 0);
}
This is the warning I get:
warning C4312: 'type cast' : conversion from 'UINT' to 'HMENU' of greater size
These are the questions I have:
- Why does the compiler think it’s a bad idea to cast to a bigger type?
- What’s the best way to get rid of the warning? (I don’t want to disable it.)
- Doing a double type cast like this:
(HMENU)(UINT_PTR)idgets rid of the warning. Why/how? - Disabling “Detect 64-bit Portability Issues” (Wp64) also gets rid of the warning. Why is Wp64 deprecated? Should I have it on?
You’re casting a 32bit UINT to a 64bit pointer. That’s suicide – you’re trying to point to something but forgot half it’s location! You absolutely MUST take a UINT_PTR. When you cast a pointer to an int, the behaviour is only OK if the int has the same size as the pointer. Else, it’s the end of your application’s runtime courtesy of an access violation.
Edit:
Why does the compiler think it’s a bad idea to cast to a bigger type?
R.E. above
What’s the best way to get rid of the warning? (I don’t want to disable it.)
Fix the problem. This code will almost certainly instacrash.
Doing a double type cast like this: (HMENU)(UINT_PTR)id gets rid of the warning. Why/how?
It happens because casting a UINT to a UINT_PTR is perfectly valid- UINT_PTR is just an integral type, there’s no loss of data.
Disabling “Detect 64-bit Portability Issues” (Wp64) also gets rid of the warning. Why is Wp64 deprecated? Should I have it on?
It’s deprecated because, actually, I can’t quite remember why. I think it warns a little too readily. But for the basic “Don’t cast integral types and pointers”, you should definitely leave it on.