va_list help, please ...

Discussion in 'C++' started by Peter, Feb 15, 2005.

  1. Peter

    Peter Guest

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

    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
     
    Peter, Feb 15, 2005
    #1
    1. Advertising

  2. Peter

    Pete Becker Guest

    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)
     
    Pete Becker, Feb 15, 2005
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Feb 15, 2005
    #3
  4. Peter

    Guest

    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
     
    , Feb 15, 2005
    #4
  5. Peter

    Guest

    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>?
     
    , Feb 15, 2005
    #5
  6. 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?
     
    Victor Bazarov, Feb 15, 2005
    #6
  7. Peter

    Pete Becker Guest

    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)
     
    Pete Becker, Feb 15, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. va_list in JNI

    , Jun 23, 2005, in forum: Java
    Replies:
    4
    Views:
    2,419
  2. KK
    Replies:
    2
    Views:
    664
    Big Brian
    Oct 14, 2003
  3. Rich Herrick

    reference to va_list

    Rich Herrick, Jan 16, 2005, in forum: C++
    Replies:
    0
    Views:
    452
    Rich Herrick
    Jan 16, 2005
  4. John Guo

    va_list help

    John Guo, Mar 31, 2005, in forum: C++
    Replies:
    5
    Views:
    3,335
    Pete Becker
    Mar 31, 2005
  5. Alex

    help with va_list etc

    Alex, Dec 30, 2004, in forum: C Programming
    Replies:
    7
    Views:
    775
    Scott J. McCaughrin
    Jan 2, 2005
Loading...

Share This Page