Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

C6230

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

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

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 equivalent to HRESULT.

Note that the use of malloc and free (and related dynamic memory allocation APIs) have many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of leaks and exception problems altogether, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.