.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
calling ...
Old
  (#1)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default calling ... - 15-02-2005

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 ?


  
Reply With Quote
Re: calling ...
Old
  (#2)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: calling ... - 15-02-2005

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;
}
  
Reply With Quote
Re: calling ...
Old
  (#3)
Rick
Council Member
 
Rick's Avatar
 
Status: Offline
Posts: 690
Join Date: Dec 2003
Location: Holland
Default Re: calling ... - 15-02-2005

Uhh why do you use the "__declspec( naked )" stuff?
I don't think you need this?(and what the heck is it anyway)
  
Reply With Quote
Re: calling ...
Old
  (#4)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: calling ... - 15-02-2005

thx

naked tells the compiler that the function should be compiled without any initializing or exiting code, e.g. stack ops etc.


  
Reply With Quote
Re: calling ...
Old
  (#5)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Re: calling ... - 16-02-2005

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


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote
Re: calling ...
Old
  (#6)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: calling ... - 16-02-2005

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.


  
Reply With Quote
Re: calling ...
Old
  (#7)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: calling ... - 16-02-2005

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



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."

Last edited by Pierre-Marie Baty; 16-02-2005 at 01:33..
  
Reply With Quote
Re: calling ...
Old
  (#8)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: calling ... - 16-02-2005

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.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com