C6226

warning C6226: Implicit cast between semantically different integer types: assigning -1 to HRESULT. Consider using E_FAIL instead.

This warning indicates that an HRESULT is being assigned or initialized with a value of an explicit -1. This warning is frequently caused by accidental confusion of integer and HRESULT types. To indicate success, the symbolic constant S_OK should be used. To indicate failure, the symbolic constants starting with E_constant should be used.

For more information see the SUCCEEDED and FAILED macros.

Example

The following code generates this warning:

#include <windows.h>

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

To correct this warning, use the following code:

#include <windows.h>

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

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