Udostępnij za pośrednictwem


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.

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