Aracılığıyla paylaş


C6214

warning C6214: cast between semantically different integer types: HRESULT to a Boolean type

This warning indicates that an HRESULT is being cast to a Boolean type. The success value (S_OK) of an HRESULT equals 0. However, 0 indicates failure for a Boolean type. Casting an HRESULT to a Boolean type and then using it in a test expression will yield an incorrect result. Sometimes, this warning occurs if an HRESULT is being stored in a Boolean variable. Any comparison that uses the Boolean variable to test for HRESULT success or failure could lead to incorrect results.

Example

The following code generates this warning:

#include <windows.h>

BOOL f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;
  hr = CoGetMalloc(1, &pMalloc);
  if ((BOOL)hr) // warning 6214
  {
    // success code ...
    return TRUE;
  }
  else
  {
    // failure code ...
    return FALSE;
  }
}

To correct this warning, use the following code:

#include <windows.h>

BOOL f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;

  hr = CoGetMalloc(1, &pMalloc);
  if (SUCCEEDED(hr)) 
  {
    // success code ...
    return TRUE;
  }
  else
  {
    // failure code ...
    return FALSE;
  }
}

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

Usually, the SUCCEEDED or FAILED macro should be used to test the value of an HRESULT.

See Also

Other Resources

SUCCEEDED

SUCCEEDED

FAILED

FAILED