.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   calling ... (http://forums.bots-united.com/showthread.php?t=3590)

@$3.1415rin 15-02-2005 20:41

calling ...
 
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 ?

Lazy 15-02-2005 21:10

Re: calling ...
 
It's a pretty big hack and I'm not sure if it will work on anything but windows...

Code:

#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;
}


Rick 15-02-2005 22:20

Re: calling ...
 
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 22:42

Re: calling ...
 
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 00:01

Re: calling ...
 
i miss the point of calling a function in a function? :S

@$3.1415rin 16-02-2005 00:15

Re: calling ...
 
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 00:29

Re: calling ...
 
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
Code:

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


Lazy 16-02-2005 00:37

Re: calling ...
 
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.


All times are GMT +2. The time now is 14:04.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.