CWinAPP::ParseCommandLine() and CCommandLineInfo
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: CWinAPP::ParseCommandLine() and CCommandLineInfo

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,221

    CWinAPP::ParseCommandLine() and CCommandLineInfo

    Does anyone know if the above can be used to extract flags from a list of command line arguments? For example, if someone started my app using this command line:-

    MyApp -L -D hello goodbye

    I could extract the parameters hello and goodbye - but how can I discover that 2 flags preceded them?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: CWinAPP::ParseCommandLine() and CCommandLineInfo

    You can override the CCommandLineInfo class and assign it to your CWinApp, then deal with it in the override.

    Code:
    void CMyCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
    {
      if((_wcsicmp(pszParam,L"l")==0)&&(bFlag==1)){
        m_bMyLFlag=1;
      }
      if((_wcsicmp(pszParam,L"d")==0)&&(bFlag==1)){
        m_bMyDFlag=1;
      }
      CCommandLineInfo::ParseParam(pszParam,bFlag,bLast);
    }
    Then in your App InitInstance():

    Code:
      CMyCommandLineInfo cmdInfo;
    
    
      ParseCommandLine(cmdInfo);
      if(!ProcessShellCommand(cmdInfo)){
    		return FALSE;
      }
    
      //check for command line switches
      if(cmdInfo.m_bMyLFlag){
        //do something
      }

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,221

    Re: CWinAPP::ParseCommandLine() and CCommandLineInfo

    Thanks, I'll experiment with that tomorrow. I wonder why CWinApp::ParseCommandLine() isn't virtual? That would have made life a lot easier.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,221

    Re: CWinAPP::ParseCommandLine() and CCommandLineInfo

    Thanks Kirants. That's pretty much the same as Hoxsiew's suggestion so I'll try it out today.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,221

    Re: CWinAPP::ParseCommandLine() and CCommandLineInfo

    Kirants, your code works except for the 'flag' comparisons (/o, /e and /whatever). If the parameter was a flag, Windows strips off the leading '-' or '/'

    So, for example, instead of :-

    Code:
    if (0 == strcmp(pszParam, "/o"))
    hozsiew's suggestion would be better :-

    Code:
    if ((0 == strcmp(pszParam, "o")) && (bFlag))
    "A problem well stated is a problem half solved. - Charles F. Kettering

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: CWinAPP::ParseCommandLine() and CCommandLineInfo

    Thanks for the headsup, John E. I need to update the FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center