Click here to Skip to main content
12,447,778 members (70,729 online)
Click here to Skip to main content
Add your own
alternative version

Stats

91.2K views
15 bookmarked
Posted

va_list, va_start, va_pass!!! or how to pass variable-argument list to next va-function

, 29 Mar 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
trick to pass variable-argument list to next va-function

Introduction

I've been looking for solution of passing variable-argument list from my va-function to another one, like TRACE for example. All solutions I saw were about using special functions that take va_list as argument. But this is a un-straight way. Why couldn't I just pass "..." to next function? C++ syntax doesn't allow this. But C++ allows to extend itself. Let me introduce you new macros from va_ set:

template<byte count>
struct SVaPassNext{
    SVaPassNext<count-1> big;
    DWORD dw;
};
template<> struct SVaPassNext<0>{};
//SVaPassNext - is generator of structure of any size at compile time.

class CVaPassNext{
public:
    SVaPassNext<50> svapassnext;
    CVaPassNext(va_list & args){
		try{//to avoid access violation
			memcpy(&svapassnext, args, sizeof(svapassnext));
		} catch (...) {}
    }
};
#define va_pass(valist) CVaPassNext(valist).svapassnext

#if 0 //using:
void MyVA_Function(szFormat, ...){
    va_list args;
    va_start(args, szFormat);
    SomeOtherVA_Function(szFormat, va_pass(args));
    va_end(args);
}
#endif
how this works:
I just copy 50 * sizeof(DWORD) bytes of stack to my struct of this size and simply pass this struct as ... argument to next function. Compiler just copies my copy of local stack to next function stack. And that's all we need.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

araud
Software Developer (Senior)
Russian Federation Russian Federation
As programmer I understand that every program takes brain to be created. The more complex is the software, the more senior developers it takes. And they say that DNA doesn't have an author? I don't buy it.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionGood! Pin
WuRunZhe5-Dec-13 20:58
memberWuRunZhe5-Dec-13 20:58 
SuggestionPortable or not, variadic functions themselves are evil Pin
Pa3PyX14-Dec-11 12:25
memberPa3PyX14-Dec-11 12:25 
GeneralMy vote of 2 Pin
Michael Imamura6-Apr-09 18:41
memberMichael Imamura6-Apr-09 18:41 
General[Message Deleted] Pin
Ilya Parniuk19-Feb-08 2:04
memberIlya Parniuk19-Feb-08 2:04 
GeneralRe: Crash on memcpy Pin
araud19-Feb-08 2:14
memberaraud19-Feb-08 2:14 
Generalnot portable Pin
peterchen29-Mar-05 6:20
memberpeterchen29-Mar-05 6:20 
GeneralRe: not portable Pin
araud29-Mar-05 18:55
memberaraud29-Mar-05 18:55 
GeneralRe: not portable Pin
peterchen29-Mar-05 19:18
memberpeterchen29-Mar-05 19:18 
Generalportable Pin
araud29-Mar-05 21:41
memberaraud29-Mar-05 21:41 
GeneralRe: portable Pin
Anonymous22-Jun-05 2:19
sussAnonymous22-Jun-05 2:19 
GeneralRe: portable Pin
Jan Richardson7-Aug-05 1:48
memberJan Richardson7-Aug-05 1:48 
GeneralRe: portable Pin
__PPS__18-Apr-07 14:37
member__PPS__18-Apr-07 14:37 
GeneralRe: portable Pin
araud18-Apr-07 18:52
memberaraud18-Apr-07 18:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 29 Mar 2005
Article Copyright 2005 by araud
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid