3

当 CRT 函数获取无效参数时,我正在使用 _set_invalid_parameter_handler 覆盖程序的默认行为,该参数会因 0xc0000417 (STATUS_INVALID_CRUNTIME_PARAMETER) 而崩溃。

这是我的处理程序:

void my_invalid_parameter_handler(
    const wchar_t * expression,
    const wchar_t * function, 
    const wchar_t * file, 
    unsigned int line,
    uintptr_t pReserved
    )
{
    Log(L"Invalid parameter detected");
    Log(L"expression= %s", expression);
    Log(L"function= %s", function);
    Log(L"file= %s", file);
    Log(L"line= %d", line);
    Log(L"pReserved= %p", pReserved);
}

我想记录信息并发送错误报告。在 Debug 构建中,我得到了有用的参数信息,但在 Release 构建中,所有参数都是 NULL,这不是很有用。有没有办法在发布版本中添加有用的信息?

4

1 回答 1

5

MSDN 库文章的备注部分明确提到了这一点:

除非使用 CRT 库的调试版本,否则参数的值都为 NULL

从 crtdefs.h 头文件中可以看出原因,为了便于阅读而进行了编辑:

#ifdef _DEBUG
#  ifndef _CRT_SECURE_INVALID_PARAMETER
#    define _CRT_SECURE_INVALID_PARAMETER(expr) \
       ::_invalid_parameter(__STR2WSTR(#expr), _FUNCTIONW__, __FILEW__, __LINE__, 0)
#  endif

#else

 /* By default, _CRT_SECURE_INVALID_PARAMETER in retail invokes_invalid_parameter_noinfo_noreturn(),
  * which is marked __declspec(noreturn) and does not return control to the application. Even if
  * _set_invalid_parameter_handler() is used to set a new invalid parameter handler which does return
  * control to the application, _invalid_parameter_noinfo_noreturn() will terminate the application and
  * invoke Watson. You can overwrite the definition of _CRT_SECURE_INVALID_PARAMETER if you need.
  *
  * _CRT_SECURE_INVALID_PARAMETER is used in the Standard C++ Libraries and the SafeInt library.
  */
#  ifndef _CRT_SECURE_INVALID_PARAMETER
#    define _CRT_SECURE_INVALID_PARAMETER(expr) ::_invalid_parameter_noinfo_noreturn()
#  endif  /* _CRT_SECURE_INVALID_PARAMETER */
#endif  /* _DEBUG */

我会说,一项优化太多了。能够自己#define _CRT_SECURE_INVALID_PARAMETER 看起来很吸引人,但除非您自己重建 CRT,否则它不起作用。这并不完全实用。

于 2012-03-10T17:11:09.160 回答