View Full Version : calling ...
@$3.1415rin
15-02-2005, 21:41
when I have a function like this
f1(const char *szFmt,...);
from which I want to call another function like
f2(const char *szFmt,...);
from inside f1, writing f2(szFmt) obviously doesnt work ... anybody know how to do it ?
It's a pretty big hack and I'm not sure if it will work on anything but windows...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
// Prototypes
void Function1( char* pszFmt, ... );
void Function2( char* pszFmt, ... );
// Calls Function2
void Function1( char* pszFmt, ... ) {
printf( "Log: %s\n", pszFmt );
Function2( pszFmt );
}
// Prints a message with numbers
__declspec( naked ) void Function2( char* pszFmt, ... ) {
static char szText[ 1024 ];
va_list argList;
va_start( argList, pszFmt );
_vsnprintf( szText, 1024, pszFmt, argList );
va_end( argList );
printf( szText );
// Since the __declspec( naked ) modifier doesn't generate
// any prolog or epilog code, we must return ourselves.
__asm ret;
}
int main( void ) {
Function1( "Its the numbers %d, %d, %d and %d!\n", 5, 3, 7, 9 );
return 0;
}
Uhh why do you use the "__declspec( naked )" stuff?
I don't think you need this?(and what the heck is it anyway)
@$3.1415rin
15-02-2005, 23:42
thx
naked tells the compiler that the function should be compiled without any initializing or exiting code, e.g. stack ops etc.
stefanhendriks
16-02-2005, 01:01
i miss the point of calling a function in a function? :S
@$3.1415rin
16-02-2005, 01:15
sometimes you wanna do something do some data and then pass the original parameters further to another function, don't you ? the problem here is that you cannot just pass the parameters to the next function, this simply doesnt work, somehow the stack seems to be fucked up then. Therefore a solution would be such a naked function. but since this isnt the most beautiful way to go, gotta think about some other solution.
Pierre-Marie Baty
16-02-2005, 01:29
Else you can use varargs and concatenate all your szFmt into one single string, that you'll feed to f2 this way. I had the same problem once and that's how I did it.
*edit*
code always help
int function1 (const char *fmt, ...)
{
va_list argptr;
string temp_string[256];
// concatenate all the arguments in one string
va_start (argptr, fmt);
vsprintf (temp_string, fmt, argptr);
va_end (argptr);
// call f2
function2 (temp_string);
}
If you pass say 5 parameters to a function using variable parameters...
main:
--------
push 9
push 7
push 3
push 5
push "a string"
call Function1
Function1:
-------------
push "a string" ( Same one as was passed to us )
call Function2
Function2:
-------------
Hopes that the variable arguments are still in the correct order on the stack!
Ofcourse that is inaccurate but hopefully it shows that when anything wants to access those variable arguments they may get the address of a string instead since things are popped off the stack in reverse order.
Hopefully I'm not too wrong on that so correct me if thats the case.
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.