2

I have a code in C# that uses _set_invalid_parameter_handler function. It's Windows specific and i am trying to rewrite this code in standard C++ so it runs on Linux.

I am not sure how to translate this functionality. I was advised to use maybe http://en.cppreference.com/w/c/error/set_constraint_handler_s but i have followed this example and the types of constraint handlers aren't recognized in visual studio 2015 nor when i try to compile it on Linux. Will appreciate some help either using constraint handlers or finding some other way to replicate _set_invalid_parameter_handler functionality in standard C++.

darkThoughts
  • 403
  • 6
  • 18

1 Answers1

0

It's not so easy set_constraint_handler is a C11-functionality, and set_invalid_parameter is Microsoft-specific (also in C++).

In standard C++ the solution is to use exceptions, and try/catch instead of setting a handler - but it requires that the low-level functions you call actually detect and signal errors with exceptions - so we would need a more complete example to see if this helps.

Hans Olsson
  • 11,123
  • 15
  • 38
  • the _set_invalid_parameter_handler in the program is used to detect a bad format string for vsnprintf_s, causing an error normally, will be handled. Is there a signal that will be triggered in this scenario? – darkThoughts Jul 11 '17 at 15:08
  • No, but you are not supposed to have that problem in C++ - you are supposed to use overloaded operator<< to format output (with special things for setting widths etc) - detecting mismatches at compile time. Obviously that can take some effort. – Hans Olsson Jul 11 '17 at 15:11
  • I get the format string and params (that vsnprintf_s uses) from the server and i can't change that code. So i can't exactly use format string and params with <<. It's meant to be used with vsnprintf_s – darkThoughts Jul 11 '17 at 15:13
  • Is there an option to use this data mean for vsnprintf_s, in stringstream or another C++ facility instead? – darkThoughts Jul 11 '17 at 18:09