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.
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.