I am developping a C++/CLI library and I have the following class:
public ref class AbstractClassA abstract{
protected:
// signature can be changed by me (method will be overriden in C#)
virtual void write(String ^ str, String ^ format, ...array<Object^> ^args) = 0;
internal:
// signature cannot be changed by me
void internWrite(const char *str, const char *format, va_list args){
write(gcnew String(str), gcnew String(format), ???)}
};
The method 'internWrite' must call the method 'methodA' but I am not able to find how to convert the 'va_list' parameter to a managed array. I found some interesting posts [1], [2] but the mentionned solutions require to have the 'va_list' lenght. I do not have these information and I cannot change the signature of 'internWrite'.
I can change the signature of 'write'.
How can I convert va_list to a managed variable args array ?
[EDIT] The comments of the method 'internWrite' mention:
-format + args: formated print, same syntax as printf function
Maybe I need use the parameter 'format' to get the length of the va_list (as mentionned on length of va_list when using variable list arguments?). But how can I do that? How the printf command does that ?
[EDIT] Making the hypothesis that format+args have the same syntax as printf, I create a method to get the lenght of agrs (by countinh the '%'). Here is my code at this moment:
void write(char *format, va_list args){
String^ mgFormat = gcnew String(format);
int nbArgs = sizeOfArgs(format);
array<Object^>^ mgArgs = gcnew array<Object^>(nbArgs);
va_start(args, format);
for (int _i = 0; _i < nbArgs; ++_i){
mgArgs[_i] = va_arg(args, int); // not sure the type is int ... how to know ?
}
va_end(args);
internWrite(mgFormat, mgArgs);
}
But now I am stucked about adding each arg object to the array because the macro 'va_arg' requires the type of the current argument. I do not have this information. How to iterate va_list and fill my managed array?
[1] How do I pass variable arguments from managed to unmanaged with a C++/CLI Wrapper?
[2] Calling a .NET function with variable number of parameters from unmanaged code