By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

va_list help, please ...

P: n/a
--------------------------------------------------------------------------------

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
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Peter wrote:

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);

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

P: n/a
Peter wrote:
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
Jul 23 '05 #3

P: n/a
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

Jul 23 '05 #4

P: n/a
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>?

Jul 23 '05 #5

P: n/a
ks****@gmail.com wrote:
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?
Jul 23 '05 #6

P: n/a
ks****@gmail.com wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.