Is there a way to convert a string to a function? I want to basically pass different function names as string as part of a communication protocol and then use one of the string element as a function name.
Is there a way to convert a string to a function? I want to basically pass different function names as string as part of a communication protocol and then use one of the string element as a function name.
It would be a lot safer for both sides to agree what the valid list of functions to be called is, then associate those functions via an enum.
It's possible to do it in a couple of ways, but as EVOEx notes, it is not something you would want unchecked access to.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Assuming that the program runs under the same privileges as usual for that user, the possible "Read Mail Really Fast"-command wouldn't be a new security problem. And depending on what you want to do, it might be actually an easy (although not portable) solution to use the system(3) (or whatever MS-Windows equivalent) call, and make separate executable files out of your functions.
I had a similar problem once, when I wrote a Matlab-alike interface for an image processing framework, and I ended up with a std::map<std::string, function_object_type>, where the function_object_type was a base for all functors I allowed to be called that way. For passing a variable amount and different types of parameters I parsed the input using Yacc&Lex, pushing them on a std::stack<functor_variable_type>, which would then be used by the functor called. In had an alternative idea, but it included overloading "operator,", which I decided, in the interest of my advisor's sanity, not to do. Might have been funny, though. Watching him go mad, I mean.
It doesn't look up a function from that; it looks up a file from that and executes the file.
But it might be possible, just not portably. If the linker leaves in symbol names, it's possible to look up the addresses of them. But anyone using that should be banned from coding.
(Except for libraries of course, in that case it is even common in Windows)
What were we just saying again?Code:struct foo { functionpointer f; char * functionname; };
Quzah.
Hope is the first step on the road to disappointment.