va_list help, please ...

P

Peter

--------------------------------------------------------------------------------

Hi,

I was wondering if we can create a va_list by adding objects in the
va_list instead of passing in the parameter preceding first optional
argument? In another words, I want to create a va_list in a similar
manner as creating an array.

The reason is I want to use FormatMessage() in my program where it
takes in a va_list as an argument, but the objects that I would like
to pass in with the va_list is not in the form of a parameter
argument.

I tried doing the following but obviously it won't work, but I have no
idea:

va_list temp;
va_arg(temp, long) = value1;
va_arg(temp, long) = value2;

ANy help would be greatly appreciated,

Thanks,
Peter
 
P

Pete Becker

Peter said:
I tried doing the following but obviously it won't work, but I have no
idea:

va_list temp;
va_arg(temp, long) = value1;
va_arg(temp, long) = value2;

ANy help would be greatly appreciated,

Write a variadic function that calls the function you're interested in.

void wrapper(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
FormatMessage(fmt, ap); // whatever you need here
va_end(ap);
}

Now call wrapper from the spot where you know what your arguments are:

wrapper(fmt, value1, value2);
 
V

Victor Bazarov

Peter said:
I was wondering if we can create a va_list by adding objects in the
va_list instead of passing in the parameter preceding first optional
argument? In another words, I want to create a va_list in a similar
manner as creating an array.

The reason is I want to use FormatMessage() in my program where it
takes in a va_list as an argument, but the objects that I would like
to pass in with the va_list is not in the form of a parameter
argument.

I tried doing the following but obviously it won't work, but I have no
idea:

va_list temp;
va_arg(temp, long) = value1;
va_arg(temp, long) = value2;

ANy help would be greatly appreciated,

I'd create a proxy function with ellipsis in the argument list and then
in it I'd implement the forming of va_list using va_args which I'd after
creating it passed to whatever function I need:

int MyFormatMessage(other_arguments, int lastone, ...)
{
va_list lst;
va_start(lst, lastone);
FormatMessage(blahblahblah, lst);
va_end(lst);
}

That's the way to create your own "printf"-like functions that use vprintf
to actually print.

V
 
K

kspoon

Thank you very much for your suggestions. I think that's a good idea.
The only thing is that I was wondering if I can create something that
is more generic. Because I don't want to just create a single wrapper
function for a single call in the program.

The problem is, the parameters' type can be different types (ex, long,
int ....etc ). Does anybody know how I can write the wrapper Pete
suggested into something that takes in like generic parameters? Is that
even possible?

Thanks a lot,
Peter
 
K

kspoon

Actually, for the two wrappers you guys suggested, would it work if the
parameter is int or long? Doesn't FormatMessage takes only
null-terminated strings? So even if we have a va_list but the arguments
in the va_list are long/int, so it still won't work right? Does that
mean I still need to convert my long variables to char* right? How can
I do that in a "better" way than just using casting>?
 
V

Victor Bazarov

Actually, for the two wrappers you guys suggested, would it work if the
parameter is int or long?

Shouldn't matter.
Doesn't FormatMessage takes only
null-terminated strings?

'FormatMessage' is not a standard function. If you're talking about MS
Windows API 'FormatMessage', then just RTFM. It's OT here anyway.
So even if we have a va_list but the arguments
in the va_list are long/int, so it still won't work right?

Should, AFAIUI.
Does that
mean I still need to convert my long variables to char* right?
What????

How can
I do that in a "better" way than just using casting>?

What are you talking about?
 
P

Pete Becker

Thank you very much for your suggestions. I think that's a good idea.
The only thing is that I was wondering if I can create something that
is more generic. Because I don't want to just create a single wrapper
function for a single call in the program.

The problem is, the parameters' type can be different types (ex, long,
int ....etc ). Does anybody know how I can write the wrapper Pete
suggested into something that takes in like generic parameters? Is that
even possible?

That wrapper function does just what you need. Think about printf --
it's the same sort of thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top