Sign in to follow this  
Followers 0

default args c++

9 posts in this topic

Posted (edited)

I'm having problems where autoit is crashing when calling a function in a dll file.

I am trying to give the function optional parameters.

From what I have read, it seems you can do it in much

the same way as autoit.

I have tried in a number of ways including

//prototype

//declaration

void add(int i = 0);

void add(int i = 0){

//stuff

}

void add(int i);

void add(int i = 0){

//stuff

}

void add(int);

void add(int i = 0){

//stuff

}

etc...

in all cases I get a crash if I do not pass it something.

Is there a specific way of achieving this for libraries, like overloading?

EDIT:

func is __stdcall if that makes any difference.

Edited by JohnOne

Share this post


Link to post
Share on other sites



Posted

Optional parameters are not knowable at runtime. They are compile time constants ONLY. The reason they work in C++ when you include a header is because the header declares it so the compiler inserts it if you ignore it. If you are using a non-C++ language or not using the header which declares the default value, you must specify it like a normal parameter.

Share this post


Link to post
Share on other sites

Posted (edited)

Yes the prototype is in a header file, and I'mm using VC++ VS2010.

AutoIt exits with -1073741819

Edited by JohnOne

Share this post


Link to post
Share on other sites

Posted

JohnOne, it is my understanding that the prototype is where you declare defaults and the actual function does not have defaults. Eg:

int main(int a = 42, char b = 'a');  /* Prototype */

int main(int a, char <img src='http://www.autoitscript.com/forum/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> {  /* Function */
  /* blah blah */
}

In my (little) experience, this is the correct way to do this.

Share this post


Link to post
Share on other sites

Posted (edited)

Johnone, what you are trying to do is impossible. Optional parameters is only syntactic sugar resolved at compiletime as richard says. The function still expects the last parameter, hence you are corrupting the stack.

Normally, when you have default parameters, you set them to 0 - and the called function recognizes this as a default parameters. Eg:

void foo(int a, char * string) {
    if(!string) { // user called it like foo(5, NULL);
        //handle
    } else {
        //work with it
    }
    ...

}

Else you have to get into variadic functions, or passing an array of parameters and a count.

E: I guess you could overload the functions, afaik _stdcall gets exported as _[name]@[parameters_in_bytes]. However, you would still have to choose the right one.

JohnOne, it is my understanding that the prototype is where you declare defaults and the actual function does not have defaults. Eg:

int main(int a = 42, char b = 'a');  /* Prototype */

int main(int a, char <img src='http://www.autoitscript.com/forum/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> {  /* Function */
  /* blah blah */
}

In my (little) experience, this is the correct way to do this.

You can also do it in the definition. Edited by Shaggi

Share this post


Link to post
Share on other sites

Posted

Yes the prototype is in a header file, and I'mm using VC++ VS2010.

AutoIt exits with -1073741819

............................................________........................

....................................,.-‘”...................``~.,..................

.............................,.-”...................................“-.,............

.........................,/...............................................”:,........

.....................,?......................................................,.....

.................../...........................................................,}....

................./......................................................,:`^`..}....

.............../...................................................,:”........./.....

..............?.....__.........................................:`.........../.....

............./__.(.....“~-,_..............................,:`........../........

.........../(_....”~,_........“~,_....................,:`........_/...........

..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}...........

...........((.....*~_.......”=-._......“;,,./`..../”............../............

...,,,___.`~,......“~.,....................`.....}............../.............

............(....`=-,,.......`........................(......;_,,-”...............

............/.`~,......`-...................................../...................

.............`~.*-,.....................................|,./.....,__...........

,,_..........}.>-._...................................|..............`=~-,....

.....`=~-,__......`,.........................................................

...................`=~-,,.,......................................................

................................`:,,...........................`..............__..

.....................................`=-,...................,%`>--==``.......

........................................_..........._,-%.......`...............

...................................,<`.._|_,-&``................`..............

2 people like this

Share this post


Link to post
Share on other sites

Posted

That was good..

Share this post


Link to post
Share on other sites

Posted

No, Jave (wild guess).

Good anyway!

Share this post


Link to post
Share on other sites

Posted

Cheers for the info gentlebodes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.