Edit

Share via


Warning C6216

Compiler-inserted cast between semantically different integral types: a Boolean type to HRESULT

A Boolean type is being used as an HRESULT without being explicitly cast.

Remarks

Boolean types indicate success by a non-zero value; success (S_OK) in HRESULT is indicated by a value of 0. A Boolean false value used as an HRESULT would indicate S_OK, which is frequently a mistake.

Code analysis name: COMPILER_INSERTED_CAST_BOOL_TO_HRESULT

Example

The following code generates this warning:

#include <windows.h>
BOOL IsEqual(REFGUID, REFGUID);

HRESULT f( REFGUID riid1, REFGUID riid2 )
{
  // Oops, f() should return S_OK when the values are equal but will
  // return E_FAIL instead because IsEqual returns a c-style boolean values TRUE or FALSE
  return IsEqual(riid1, riid2);
}

To correct this warning, either add the appropriate conversion between the two types or add an explicit cast.

#include <windows.h>
BOOL IsEqual(REFGUID, REFGUID);

HRESULT f( REFGUID riid1, REFGUID riid2 )
{
  // converting because IsEqual returns a c-style TRUE or FALSE
  return IsEqual(riid1, riid2) ? S_OK : E_FAIL;
}

For this warning, the SCODE type is equivalent to HRESULT.

For more information, see SUCCEEDED Macro and FAILED Macro.