Solved

VC++: read cmdline arguments

Posted on 2004-09-20
8
911 Views
Last Modified: 2011-09-20
Hello experts,
I'm using the following code:
---
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow )
{
      bool bOK = false;
      //
      //Read CmdLine's argument(s):
      char *CmdLine;
      CmdLine = (char *) lpCmdLine;
      // Next line can be used for Debugging purposes:
      //MessageBox(0,CmdLine,"A r g u m e n t - Computername",MB_OK);  
      char *computername;
      computername = (char *) CmdLine;
      if (strcmp(computername ,"") == 0)
      {
            MessageBox(0,"No argument has been supplied!","I n f o r m a t i o n",MB_OK);
            return bOK;
      }
                // ...
}
---
Now I want to introduce a switch like "-debug".
This switch I want to use to display some debugging information.

Calling sample:
---
myprog.exe computername                            <== this is working

myprog.exe -debug computername                 <== that is my target
   or
myprog.exe computername -debug                 <== that is my target (which one is better)
---
If anyone knows a solution, please modify the above's code accordingly (I'm using VC++6.0).
Thank you very much for every answer.
  HStrix
 
0
Comment
Question by:HStrix
  • 5
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Use the string manipulation routines or 'strtok()' for that:

bool bDebug = false;

nt APIENTRY WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                    int       nCmdShow )
{
    bool bOK = false;
    //
    //Read CmdLine's argument(s):
    char *CmdLine;
    CmdLine = (char *) lpCmdLine;
    // Next line can be used for Debugging purposes:
    //MessageBox(0,CmdLine,"A r g u m e n t - Computername",MB_OK);  
    char *computername, option;
    computername =  strdup ((char *) CmdLine);
    option = strtok ( computername, " ");

    if ( !strcmp ( option, "-debug")) {

        bDebug = true; // option present

    } else {

         MessageBox(0,"Invalud argument has been supplied!","I n f o r m a t i o n",MB_OK);
         return bOK
    }

    if (!strlen(computername))
    {
         MessageBox(0,"No argument has been supplied!","I n f o r m a t i o n",MB_OK);
         return bOK;
    }
                // ...
}
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW, if you'd like a more "universal" solution, take a look at e.g. http://www.codeproject.com/cpp/simpleoptions.asp ("Simple command line processing") or a "full blown" one at http://www.codeproject.com/cpp/cmdlineparser.asp ("Command line parser")

There's more to find at http://www.codeproject.com/cpp/#Command+line+processing
0
 

Author Comment

by:HStrix
Comment Utility
Thank jkr,
I modified your code to
---
      char *computername;
      char *option;
      computername =  strdup ((char *) CmdLine);
      MessageBox(0,computername,"cmdline",MB_OK);  // for testing only
      option = strtok(computername," ");  
      MessageBox(0,option,"switch",MB_OK);                 // for testing only
                if ( !strcmp ( option, "-debug")) {
        bDebug = true; // option present
            computername = "";  // ???? how to get the computername here ???
    } else {
            bDebug = false;
            computername = (char *) CmdLine;  // here: a switch is not given
    }
---
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> // ???? how to get the computername here ???

You already have it. I assumed the

myprog.exe computername -debug

format, since it is easier to parse.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:HStrix
Comment Utility
Ok, I checked it the way arround.
Now it is as follows: (calling is::= my.exe computername [-debug])
---
      char *computername;
      char *option;
      computername =  strdup ((char *) CmdLine);
      MessageBox(0,computername,"cmdline",MB_OK);  // computername -debug
      option = strtok(computername," ");                      // this is again the computername. not the switch
      MessageBox(0,option,"switch",MB_OK);
                if ( !strcmp ( option, "-debug")) {
                    bDebug = true;    // option present
                } else {
                    bDebug = false;
                }
---
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Sorry, sometimes I am slow :o)
    char* lineToParse = strdup ((char *) CmdLine);
    char *computername;
    char *option;
    computername =  strtok(lineToParse ," ");
    MessageBox(0,computername,"cmdline",MB_OK);  
    option = strtok(NULL ," ");                      
    MessageBox(0,option,"switch",MB_OK);
               if ( !strcmp ( option, "-debug")) {
                   bDebug = true;    // option present
               } else {
                   bDebug = false;
               }
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW, I'd *really* recommend using one of the articles I mentione above.
0
 

Author Comment

by:HStrix
Comment Utility
Thank you jkr,
my working code is now as follows:
---
      char *CmdLine;
      char *string;
      CmdLine = (char *) lpCmdLine;
      //
      // calling is::= my.exe computername [-debug]
      char *testarg;
      testarg = (char *) CmdLine;          
                if (!strlen(testarg))
      {
            MessageBox(0,"No argument has been supplied!","I n f o r m a t i o n",MB_OK);
            return bOK;
            //string = "mhpab5gc"; // for testing
      } else
      {
            string = (char *) lpCmdLine;
      }
      //
      bDebug = false;
      char *computername;
                char *option;
      //
      char *token;
      char seps[]   = " ,\t\n";
      token = strtok( string, seps );
      int itok=0;
      option = "-nodebug";
      while( token != NULL )
      {
            /* While there are tokens in "string" */
            //MessageBox(0,token,"token",MB_OK);
            if (itok==0)
            {
                  computername= token;
            } else
            {
                  option=token;
            }
            itok++;
            /* Get next token: */
            token = strtok( NULL, seps );
      }
      //
      if ( !strcmp ( option, "-debug"))
      {
            bDebug = true;    // option present            
      }
      //bDebug = true;  // for testing
      if (bDebug == true)
      {
            MessageBox(0,"true","bDebug",MB_OK);
            MessageBox(0,CmdLine,"A r g u m e n t - Cmdline",MB_OK);
            MessageBox(0,computername,"Computername",MB_OK);
            MessageBox(0,option,"switch",MB_OK);
      } else
      {
            //MessageBox(0,"false","bDebug",MB_OK);
      }      
---
The problem was, that the program crashed in case of looking for the second token (-debug)
if that token was not supplied.

I will have a look at your suggestions as well.

   Thank you again.

     HStrix
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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

11 Experts available now in Live!

Get 1:1 Help Now