warning C6031: Return value ignored: 'GetLastError'

来源:互联网 发布:java qq登录 编辑:程序博客网 时间:2024/05/01 21:45

#ifdef _M_CEE_PURE
#define GetLastError System::Runtime::InteropServices::Marshal::GetLastWin32Error
#else
WINBASEAPI
__checkReturn
DWORD
WINAPI
GetLastError(
    VOID
    );
#endif

 

返回参数有个 __checkReturn 

查msdn发现:

warning C6031: return value ignored: <function> could return unexpected value

This warning indicates that the calling function is not checking the return value of a function call that signals failure via its return value. Depending on which function is being called, this defect can lead to seemingly random program misbehavior, including crashes and data corruptions in error conditions or low-resource situations.

In general, it is not safe to assume that a call to function requiring disk, network, memory, or other resources will always succeed. The caller should always check the return value and handle error cases appropriately.

Example

The following code generates this warning:

#include <stdio.h>  void f( )  {    fopen( "test.c", "r" ); // return value ignored    // code ...  }  
#include <stdio.h>void f( ){  fopen( "test.c", "r" ); // return value ignored  // code ...}

To correct this warning, check the return value of the function as shown in the following code:

#include <stdio.h>  void f( )  {    FILE *stream;    if((stream = fopen( "test.c", "r" )) == NULL )       return;    // code ...  }  
#include <stdio.h>void f( ){  FILE *stream;  if((stream = fopen( "test.c", "r" )) == NULL )     return;  // code ...}

The following code uses safe function fopen_s to correct this warning:

#include <stdio.h>  void f( )  {    FILE *stream;    errno_t err;      if( (err  = fopen_s( &stream, "test.c", "r" )) !=0 )    {      // code ...    }  }  
#include <stdio.h>void f( ){  FILE *stream;  errno_t err;  if( (err  = fopen_s( &stream, "test.c", "r" )) !=0 )  {    // code ...  }}

This warning is also generated if the caller ignores the return value of a function annotated with theMustCheck property as shown in the following code:

  #include <codeanalysis\sourceannotations.h>  [returnvalue:SA_Post(MustCheck=SA_Yes)] bool func( );    
#include <codeanalysis\sourceannotations.h>[returnvalue:SA_Post(MustCheck=SA_Yes)] bool func( );

void test_f()

{

func( ); //return value ignored

}

To correct the previous warning, check the return value as shown in the following code:

void test_f()

{

if( func( ) )

{

//code...

}

}

 

继续查MustCheck:

The MustCheck property specifies whether the caller must inspect the return value of a function. This property is used as a post condition on the return value of a function.

The MustCheck property must be set by using one of the following values:

  • SA_Yes - the return value must be checked; otherwise warning 6031 is issued.

  • SA_No - the return value should not be checked.

只有当MustCheck是SA_Yes时才会要求检查返回值(Checking a return value of a function marked with SA_No does not generate a warning.)

而windows SDK的__checkReturn就相当于(MustCheck=SA_Yes)

总结:检查返回值

原创粉丝点击