The docs for ShellExecute
state (italicized emphasis mine):
Return value
Type: HINSTANCE
If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.
However, obeying them like so:
if ((int)ShellExecuteW(...) <= 32) ...
Yields warnings when compiled with MSVC 2019 (both 32- and 64-bit):
warning C4311: 'type cast': pointer truncation from 'HINSTANCE' to 'int'
warning C4302: 'type cast': truncation from 'HINSTANCE' to 'int'
Using reinterpret_cast<int>
produces similar results.
I can suppress the warnings for that line:
#pragma warning(suppress: 4311 4302)
if ((int)ShellExecuteW(...) <= 32) ...
But I don't generally like doing that unless there are no other options. My question is therefore: Are there other options? Is there some C++ syntax that lets me honor the API documentation without generating warnings?