Поделиться через


_set_invalid_parameter_handler

Sets a function to be called when the CRT detects an invalid argument.

_invalid_parameter_handler _set_invalid_parameter_handler(
   _invalid_parameter_handler pNew
);

Parameters

  • [in] pNew
    The function pointer to the new invalid parameter handler.

Return Value

A pointer to the invalid parameter handler before the call.

Remarks

Many C run-time functions check the validity of arguments passed to them. If an invalid argument is passed, the function can set the errno error number or return an error code. In such cases, the invalid parameter handler is also called. This function allows that handler to be set to another function. Only one function can be specified as the invalid argument handler at a time.

When the framework calls the invalid parameter function, it usually means that a nonrecoverable error occurred. The invalid parameter function should save any data it can and then abort. It should not return control to the main function unless you are confident that the error is recoverable.

The invalid parameter handler function must have the following prototype:

void _invalid_parameter(
   const wchar_t * expression,
   const wchar_t * function, 
   const wchar_t * file, 
   unsigned int line,
   uintptr_t pReserved
);

The first argument is the argument expression. The second argument is the name of the CRT function that received the invalid argument. The third argument is the file name in the CRT source. The fourth argument is the line in that file. The last argument is reserved. The parameters all have the value NULL unless a debug version of the CRT library is used.

Requirements

Routine

Required header

_set_invalid_parameter_handler

<stdlib.h>

For more compatibility information, see Compatibility in the Introduction.

Example

In the following example, an invalid parameter error handler is used to print the function that received the invalid parameter and the file and line in CRT sources. When the debug CRT library is used, invalid parameter errors also throw an assertion, which is disabled in this example using _CrtSetReportMode.

// crt_set_invalid_parameter_handler.c
// compile with: /Zi /MTd
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>  // For _CrtSetReportMode

void myInvalidParameterHandler(const wchar_t* expression,
   const wchar_t* function, 
   const wchar_t* file, 
   unsigned int line, 
   uintptr_t pReserved)
{
   wprintf(L"Invalid parameter detected in function %s."
            L" File: %s Line: %d\n", function, file, line);
   wprintf(L"Expression: %s\n", expression);
   abort();
}


int main( )
{
   char* formatString;

   _invalid_parameter_handler oldHandler, newHandler;
   newHandler = myInvalidParameterHandler;
   oldHandler = _set_invalid_parameter_handler(newHandler);

   // Disable the message box for assertions.
   _CrtSetReportMode(_CRT_ASSERT, 0);

   // Call printf_s with invalid parameters.

   formatString = NULL;
   printf(formatString);
}
Invalid parameter detected in function printf. File: printf.c Line: 54
Expression: (format != NULL)

See Also

Reference

Security-Enhanced Versions of CRT Functions

errno, _doserrno, _sys_errlist, and _sys_nerr