Right now, I am trying to call a function in C++ through a Json object. The Json object would provide me with the name of the callee function and all the parameters. I will be able to extract the parameters using a for loop, but I am not sure how I can pass them in. For loop only allows me to pass arguments one by one, and I did not find a way to call a function besides passing in all the arguments at once. I've made a temporary solution of:
if (parameter_count == 1) func(param_1); if (parameter_count == 2) func(param_1, param_2); ... This solution seems would not work for all cases since it can only work for functions with a limited number of arguments (depending on how many ifs I write). Is there a better way for this? Thanks!
EDIT: Sorry if I was being unclear. I do not know anything about func. I will be reading func from DLL based on its string name. Since I can't really change the function itself, I wouldn't be able to pass in a vector or struct directly.
Or perhaps did I have the wrong understanding? Are we allowed to pass in a single vector in place of a lot of parameters?
Sorry for making a mess through so many edits on this question. Brandon's solution with libffi works. Thanks!
printfdoes with a loss of type safety. Depends on how restricted the function you are passing to is and what variation in types you are trying to pass. More details might helpstd::vector<type>? Possibly passing by reference. Since it appears that you don't know until runtime how many parameters there will be.GetProcAddressordlsymfor example (if it is a C function or exported function), then you could also write the x86_64 ABI code for calling the function.. assuming you're running x86_64 anyway.. OR use libffi to call it. This is alternative to the comments above.