Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    command line arguments

    me again

    how can i use command line arguments if i am not using comand line when opening my programs

    and if i cant, how do i use command line to open them (in xp)

    thanx in advance

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    In console, the main's argv contains the arguments and argc the number of arguments. In Win32 subsystem the lpCmdLine argument of WinMain points to the command line arguments or alternatively you can use GetCommandLine() to get the command line string pointer.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    well
    i am not that knowledgeable about command line
    can you explain it with more detail

    and how do i use GetCommandLine() function?

    thanx

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    LPTSTR GetCommandLine(VOID);
    So, it returns a string containing the commandline - as easy as that.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    but i want to use argv and argc in my program

    can i use these as main(argc, *argv[]) with

    Code:
    LPTSTR GetCommandLine(VOID); this and how?
    thanx matsp

    you are the man
    Last edited by benhaldor; 08-20-2007 at 02:43 PM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

  7. #7
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by benhaldor View Post
    me again

    how can i use command line arguments if i am not using comand line when opening my programs

    and if i cant, how do i use command line to open them (in xp)

    thanx in advance
    You could make a shortcut to your program and then put the parameters in the path of the shortcut, like this: "C:\Myprograms\myprogram.exe -parameter1"...

    You can also do the same using "run" from the Start menu...

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Neo1 View Post
    You could make a shortcut to your program and then put the parameters in the path of the shortcut, like this: "C:\Myprograms\myprogram.exe -parameter1"...

    You can also do the same using "run" from the Start menu...
    Or, you could make a .pid file.

    That was a joke. Does anybody have any clue what I'm talking about?

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    well actually these didnt solve my problems

    any other suggestions?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you actually trying to do? What part of the usage of the command line arguments are you having problems with.

    The basic principle is: if your application is a console application, the "main(int argc, char **argv)" method works, whether it was started from a command-line or using any other method to start the application. If your application is a windows application, then the "GetCommandLine" function is the option of choice for most cases.

    Describe more of what you want to know, and someone will be able to help you, I can assure you.

    --
    Mats

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Try using a batch file or create a program which uses ShellExecute() or ShellExecuteEx().

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    i am not using console and i want to learn GetCommandLine thing but nobody explained how it works

    you said it gets the string from the command line but i want to use this main(int argc, char **argv) but not use the command line
    from what i understand there is no way to do that
    right?

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The only purpose of int main(int argc, char *argv[]) is to retrieve command line arguments. If you don't want to use the command line, then this will be of little use.

    GetCommandLine() is a less portable (Windows-only), and it gives you the whole command line, rather than each argument by itself. That is, with a command line of "program.exe some more text", argv[] will contain "program.exe", "some", "more", and "text", while GetCommandLine() would return exactly what you typed, "program.exe some more text". (I think; it may exclude the program's name. I've never actually used it.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by benhaldor View Post
    i am not using console and i want to learn GetCommandLine thing but nobody explained how it works
    I posted how it works - what is missing from this, or what don't you understand about it?
    you said it gets the string from the command line but i want to use this main(int argc, char **argv) but not use the command line
    from what i understand there is no way to do that
    right?
    This is not clear to me what you actually want to do - do you want to take the input commandline and split it in an "argc, argv" style? Or something else?

    I'm 100% sure that if you clearly state what your goal is, and by this I mean, not just "I want to understand how to use X", but somthing like "I want to be able to use file(s) as argument(s) to my application, and load the file(s) given as commandline arguments".

    --
    Mats

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by matsp View Post
    I'm 100% sure that if you clearly state what your goal is, and by this I mean, not just "I want to understand how to use X", but somthing like "I want to be able to use file(s) as argument(s) to my application, and load the file(s) given as commandline arguments".
    In other words:

    http://catb.org/~esr/faqs/smart-questions.html#goal

    I suggest the OP reads the rest of the article, too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM