Dela via


C6219

warning C6219: Implicit cast between semantically different integer types: comparing HRESULT to 1 or TRUE. Consider using SUCCEEDED or FAILED macro instead

This warning indicates that an HRESULT is being compared with an explicit, non-HRESULT value of one (1). This is likely to lead to incorrect results because the typical success value of HRESULT (S_OK) is 0; comparing it to a Boolean type will implicitly convert it to false.

Example

The following code generates this warning because the CoGetMalloc returns an HRESULT, which then is compared to TRUE:

#include <windows.h>

void f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;
  hr = CoGetMalloc(1, &pMalloc);
  
  if (hr == TRUE)
  {
    // success code ...
  }
  else
  {
    // failure code   
  }
}

Most of the time, this warning is caused by the code mistakenly testing an HRESULT against a Boolean. It is generally best to use the SUCCEEDED or FAILED macros to test the value of an HRESULT. To correct this warning, use the following code:

#include <windows.h>

void f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;
  hr = CoGetMalloc(1, &pMalloc);
  
  if (SUCCEEDED(hr))
  {
    // success code ...
  }
  else
  {
    // failure code   
  }
}

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