Thread: Passing Arguments in windows

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Passing Arguments in windows

    Hello all;

    I was wonddering how to pass commands from the command line to my program in c++.


    I have done this successfully in Fedora Core 3 using argv and argc, but I can't seem to get windows to do it.


    The program is executed via command line as follows

    c:>test-app

    I would like to be able to do

    c:>test-app -i 10.1.1.1

    and have my program receive the IP address



    Thanks

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Look up what's in this FAQ page, oh and this
    Last edited by twomers; 07-28-2006 at 08:50 AM.

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Here check out this code, it checks if it got two arguments on the commandline(other than the path to the executable) and if it did it displays them. From this you should be able to utilize the arguments.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        if(argc==3)
        cout<<endl<<argv[1]<<endl<<argv[2]<<endl;
        cin.get();
        return 0;
    }
    The program always has one argument so argc is always one at least and argv[0] is always the path to the executable. After that are the arguments you entered.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Is argv[0] garrenteed to be the path to the executable?

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    As far as I know, yes. My understanding is that the path to the executable is passed as the first argument because it needs to use that path to execute it. Please correct me if I'm wrong.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    No. It was discussed on the C board.
    Quote Originally Posted by Grumpy
    Not quite. It either represents the program name or it is an empty string.

    There are all sorts of possibilities in that, depending on how the word "represents" is interpreted. For example argv[0] can be any of these;

    1) the unadorned name of the executable (eg "program")

    2) the name of the executable file (eg "program.exe" under windows)

    3) the fully qualified path of the executable file (eg "c:/directory/subdirectory/program.exe" or "C:\directory\subdirectory\program.exe" under windows, or drive:[directory.path]program.exe on an old VAX/VMS system).

    4) an alias for the executable (eg several unix shells support aliases, and could be passed as argv[0] by the shell, rather than the actual name of the executable)

    5) the name of something that is linked to the executable (eg symbolic links under unix: the name of the link can be passed as argv[0]).

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    That's a lot of things it can be eh? I've only used it once when I was making an screen-pixel grabber, and displayer, so I dragged and dropped into the program.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    That is what I thought, but I wasn't sure if the rules of C argument passing applied in C++ (since their standards have branched and some things arent the same.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments.
    By omnificient in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 02:41 PM
  2. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. Passing arguments to function...
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 12:50 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM