C6225

warning C6225: Implicit cast between semantically different integer types: assigning 1 or TRUE to HRESULT. Consider using S_FALSE instead

This warning indicates that an HRESULT is being assigned or initialized with a value of an explicit 1. Boolean types indicate success by a non-zero value; success (S_OK) in HRESULT is indicated by a value of 0. This warning is frequently caused by accidental confusion of Boolean and HRESULT types. To indicate success, the symbolic constant S_OK should be used.

Example

In the following code, assignment of HRESULT generates this warning:

#include <windows.h>

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

To correct this warning, use the following code:

VOID f( )
{
  HRESULT hr;
  LPMALLOC pMalloc;
  
  if (SUCCEEDED(CoGetMalloc(1, &pMalloc)))
  {
    hr = S_OK;
    // code ...
  }
  else
  {
    hr = S_FALSE;
    // code ...
  }
}

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

To indicate failure, E_FAIL, or another constant, should be used.

For more information see one of the following topics:

SUCCEEDED

FAILED

To leverage modern C++ memory allocation methodology, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers (Modern C++) and Standard C++ Library Reference.