Edit

Share via


Warning C6230

Implicit cast between semantically different integer types: using HRESULT in a Boolean context

Remarks

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.

Code analysis name: USING_HRESULT_IN_BOOLEAN_CONTEXT

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.