views:

67

answers:

3

i know how to Start process with argument but im trying to create a program that uses this arguments. for example IE8 uses Process::Start( "IExplore.exe","google.com"); as a argument to open new window with url google.com. i want my program to use the argument are send it but i don't know how to get the the argument. like Process::Start( "myprogram.exe","TURE"); i want my program to get the ture thanks in advance Rami

A: 

system("IExplore.exe google.com"); // #include <stdlib.h>

Dani
+4  A: 

There are two choices, depending on what kind of program you are building.

  • If your program is a console mode program, use argc and argv parameters passed to your main().
  • If your program is a GUI mode program, use the pCmdLine parameter passed to your WinMain().

In either case, you can always use GetCommandLine().

Greg Hewgill
GetCommandLine() is windows-specific though and not portable, so using argc/argv is usually to be preferred.
Frank
That's correct. I thought it was pretty clear that the OP is using Windows.
Greg Hewgill
i should go with the second choice right?
Ramiz Toma
+1  A: 

Assuming you write your entry point something like this:

int main(int argc, char* argv[])

Then argc is the number of arguments used to invoke your program and argv are the actual arguments.

Try it out:

#include <cstdio>

int main(int argc, char* argv[])
{
    for (int i = 0; i < argc; ++i)
        printf("%s\n", argv[i]);
}
Travis Gockel
would this work even if im using gui mode?
Ramiz Toma
If you're using the Windows entry point: `int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)`, then `lpCmdLine` is the command line and `nCmdShow` is the number of arguments.
Travis Gockel