Solved

parsing commandline arguments of WinMain

Posted on 2006-11-29
8
2,975 Views
Last Modified: 2008-01-09
Hi all,

I wanted a simple, failsafe way to get command line arguments from:

int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR  lpCmdLine,
    int nCmdShow );

The arguments are 2 absolute file-paths and spaces(blanks) can be part of the file-paths. So, I cann't just search for blank space to find out the next token :)

thanks,
Vikram


0
Comment
Question by:Vikram_B
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18035691
If argument contains blanks, client must use "" for such argument:

yourProgram "C:\Program Files\Dir\File1.txt" "C:\Program Files\Dir\File2.txt"

This is general rule for all command parsers. Without this there is no way to detect number of parameters.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18035699
The norm is to enclose the file paths with "" - for example :

    command "/path/to first/file 1" "/path/to second/file 2"

Those will be passed as :

    argument 1 : /path/to first/file 1
    argument 2 : /path/to second/file 2
0
 

Author Comment

by:Vikram_B
ID: 18035746
thanks Infinity08,

But, how to parse argument1 and argument2? can I use some string manipulation function?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 53

Expert Comment

by:Infinity08
ID: 18036154
You can use whatever you want, but this is the basic idea :

    char *cmdLine = (char*) lpCmdLine;
    char args[128][128] = { 0 };
    int i = 0;
    while (*cmdLine) {
      if (*cmdLine == ' ') {
        cmdLine++;
      }
      else {
        char *end = 0;
        if (*cmdLine == '\"') {
          cmdLine++;
          end = strchr(cmdLine, '\"');
        }
        else {
          end = strchr(cmdLine, ' ');
        }
        if (end) {
          strncpy(args[i], cmdLine, end - cmdLine);
          args[i][end - cmdLine] = '\0';
          cmdLine = end + 1;
          i++;
        }
        else {
          /* ERROR - treat it !! */
        }
      }
    }

Note that this is just for you to get an idea of how you COULD do it. This is ny no means complete code !!!
If more than 128 args are passed or if one arg is more than 127 characters long, this code will create problems !!
The above code is not validated, and is just written off the top of my head, so there might still be other problems with it !
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18036159
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18038022
Maybe you want that (it is tested):

#include <string>
#include <vector>
using namespace std;

int parseCmdLine(const char* szcmdline, vector<string> arguments)
{
    string cmdline = szcmdline;
    cmdline += ' ';   // add a final space for easier parsing
    bool bQuotes = false;
    int lpos = 0;
    int i = 0;
    for (i = 0; i < cmdline.length(); ++i)
    {
          char c = cmdline[i];
          switch(c)
          {
          case '"':   // note it is  '  "  '
                bQuotes = !bQuotes;   break;
          case ' ':
                if (bQuotes)
                     break;   // ignore space if within quotes
                if (i > lpos)
                     arguments.push_back(cmdline.substr(lpos, i - lpos));
                lpos = i+1;
          }
    }
    return arguments.size();
}

You can use it in your WinMain like

    vector<string> arguments;
    int nArgs = parseCmdLine( lpCmdLine, arguments );

    for (int i = 0; i < nArgs; ++i)
          cout << arguments[i] <<  endl;

Regards, Alex



0
 
LVL 14

Accepted Solution

by:
wayside earned 25 total points
ID: 18039182
What might be easier if you are using the Microsoft compiler, is that argc and argv are available as globals. The globals are __argc and __argv (that's two underscore characters in front).

Then you don't have to parse at all:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
  char buffer[500];
  for (int i=0; i<__argc; i++) {
    sprintf(buffer, "arg %ld = %s", i, __argv[i]);
    MessageBox(NULL, buffer, "cmdline", MB_OK);
  }
}

0
 

Author Comment

by:Vikram_B
ID: 18052583
thanks everyone!
 sorry, i cudnt reply in time. i donno why, but i am not receiving notifications from ee for the new comments posted!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now