April 30, 2024, 04:21:39 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


How to get argv and argc?

Started by Kale, November 03, 2006, 12:43:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Kale

How do i get the argv and argc array using Aurora?

Ionic Wind Support Team

The argv array and argc count are created by the C startup routine and are not a part of the Windows OS.  You can, however, retrieve the entire command line used to start your executable using the API function GetCommandLineA/W

Here is a previous discussion on the subject:

http://www.ionicwind.com/forums/index.php/topic,59.15.html

Which includes code to parse the command line to create your own parameter array.

Paul.
Ionic Wind Support Team

Kale

I see, thanks.

Is there any chance of these being added natively to Aurora? as these are used alot.

sapero

November 03, 2006, 02:49:47 PM #3 Last Edit: November 03, 2006, 03:05:00 PM by sapero
Paul, i use modified consolestartup and winstartup where is a call to crtdll::__GetMainArgs, then argc and argv are passed to the main function.
The stack pointer is saved/restored in .bss section and i never got problems with this modification.
invoke __GetMainArgs, _argc, _argv, _env, 0
invoke main, [$_argc], [$_argv]
mov ÂÃ,  ÂÃ, [_exitcode], eax
mov ÂÃ,  ÂÃ, esp, [_stack]
...
segment .bss
$_argcÂÃ,  ÂÃ, resd 1
$_argvÂÃ,  ÂÃ, resd 1
$_envÂÃ,  ÂÃ,  resd 1

global sub main(int argc, string *argv[])
{
if (argc > 1) MessageBox(0, *(string)(argv[1]));
}


Kale, try this:
import int cdecl __GetMainArgs(int *argc, string *argv, string *environ, opt int unknown=0);

sub main()
{
int argc;
string *argv[1];
string *environ[1];
__GetMainArgs(&argc, &argv, &environ);
main2(argc, argv);
}

sub main2(int argc, string *argv[])
{
for (int a=0; a<argc; a++)
{
print("argument ",a," :", *(string)(argv[a]));
}
*argv[0] = readln();
}

Zen

Interesting hack Sapero. Although I do think it would be cool to have these added to Aurora. Either as an optional argument in main() or perhaps just as globals so you can access them anywhere.

Lewis

Parker

I agree this is something that should be built in. I like Paul's idea of GetArgCount and GetArgString. It's not hard to create a command line parser (especially for someone who created Aurora :D) but it's a really standard feature that should be included I think.

Ionic Wind Support Team

GetArgCount, GetArgString and GetArgStringW are now in Beta1 Rev 3.  Available this evening to a computer near you ;)
Ionic Wind Support Team

Kale

Quote from: Paul Turley on November 06, 2006, 06:01:44 PM
GetArgCount, GetArgString and GetArgStringW are now in Beta1 Rev 3.  Available this evening to a computer near you ;)
Very Very Cool! Thanks Paul! :D

Parker

Thanks, that'll be a nice addition.