1 Replies - 1601 Views - Last Post: 16 April 2011 - 06:30 AM Rate Topic: -----

#1 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

your own variable arguments handler

Posted 12 March 2011 - 10:05 PM

Description: this ONLY works with cdecl functions, it will not work with stdcall because it dose not clean up the stack afterwards. if you wanted to make a handler for stdcall you would need to add another pointer to the struct that would store the first arguments address then add the difference in pointers to ESP using inline assembly. a little a hack i made for handling variable arguments in C for the cdecl calling convention
#include <stdio.h>
#include <stdlib.h>

#define VarArgStart(List,Arg) List.FirstArg = (void*)&Arg;
#define VarArgArg(List,Type) (*(Type*)((List.FirstArg+=sizeof(Type))))

typedef struct {
	void* FirstArg;
} VarArgList;

void __cdecl TestFunc(int x,...) {
	VarArgList L;
	VarArgStart(L,x);
	printf("%i,%i",x,VarArgArg(L,int));
}

int main() {
    TestFunc(20,30);
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: your own variable arguments handler

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: your own variable arguments handler

Posted 16 April 2011 - 06:30 AM

Note: Some versions of gcc do not play nicely with this snippet. You'll have to remove the __cdecl in front of the function name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1