C6230

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

warning C6230: implicit cast between semantically different integer types: using HRESULT in a Boolean context

This warning indicates that a bare HRESULT is used in a context where a Boolean result is expected, such as an if statement. This test is likely to yield incorrect results. For example, the typical success value for HRESULT (S_OK) is false when it's tested as a Boolean.

Example

The following code generates this warning:

#include <windows.h>

VOID f( )
{
  LPMALLOC pMalloc;
  HRESULT hr = CoGetMalloc(1, &pMalloc);

  if (hr)
  {

    // code ...
  }
  else
  {
    // code ...
  }
}

In most situations, the SUCCEEDED or FAILED macro should be used to test the value of the HRESULT. To correct this warning, use the following code:

#include <windows.h>

VOID f( )
{
  LPMALLOC pMalloc;
  HRESULT hr = CoGetMalloc(1, &pMalloc);

  if (SUCCEEDED(hr))
  {

    // code ...
  }
  else
  {
    // code ...
  }
}

For this warning, the SCODE type is treated as an HRESULT.

The use of malloc and free (and related dynamic memory APIs) has many pitfalls as a cause of memory leaks and exceptions. To avoid these kinds of leaks and exception problems, use the pointer and container classes provided by the C++ Standard Library. These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers and C++ Standard Library.