locked
How to Add Command Line Parsing to existing Dialog Based MFC C++ Project in Visual Studio 2005?

    Question

  • Have an existing Dialog Based MFC C++ in Visual Studio 2005 program that is already working.

    Would like to add a "Safe Mode" Switch using something like "/Reset" or "/Safe" passed on the Command Line.

    For Example:

       ProgramName.exe /Reset

       ProgramName.exe /Safe

    Have attempted to add Command Line Parsing to the program using the code below:

       #include "afxwin.h"
       .
       .
       .
       CCommandLineInfo cmdInfo;
       ParseCommandLine(cmdInfo); // error C3861: 'ParseCommandLine': identifier not found

       BOOL bFlag = FALSE;
       BOOL bLast = FALSE;
       int xx = 0;

       LPCTSTR pszParam = _T("Reset");

       while (bLast == FALSE)
       {
          cmdInfo.ParseParam(pszParam, bFlag, bLast);
          xx = xx + 1;
       }

    ---

    ParseCommandLine is in the afxwin.h file so: (1) I am not quite sure how to fix this issue; (2) whether I should try to derive a class; -or- (3) where to look next to resolve the command line parsing issue.

    Need code that will compile and work without having to dig into abstracts that have no examples that help decipher the details of how to get the work accomplished.

    Thanks in advance for helpful insights,

    BillNew


    DirectShow Filter Graph Spy: http://alax.info/blog/777 Extremely helpful for finding deep details for DirectShow Graphs.
    • Edited by BillNew Thursday, October 27, 2011 12:48 AM
    Thursday, October 27, 2011 12:41 AM

Answers

  • Still using this line:

       #include "afxwin.h"

    Then added the following to read the Command Line Data:

       // Get Command Line Data...
       LPTSTR CommandLineDetails = AfxGetApp()->m_lpCmdLine;

    Now seeing the Passed Command Line Details... Will parse the CommandLineDetails now that the Command Line Data is known.

    Thanks for the feedback.

    BillNew


    DirectShow Filter Graph Spy: http://alax.info/blog/777 Extremely helpful for finding deep details for DirectShow Graphs.
    • Marked as answer by BillNew Thursday, October 27, 2011 7:21 PM
    Thursday, October 27, 2011 7:21 PM

All replies

  • CCommandLineInfo is usefull to parse standard Windows parameters like /print, /dde or if the command line contains filenames.
    If you don't use standard Windows command line parameters like you do, use 
    LPTSTR CWinApp::m_lpCmdLine;
    
    instead.
    You can remove the CCommandLineInfo code.
    m_lpCmdLine contains the whole command line parameters, not splitted.

    Thursday, October 27, 2011 1:01 AM
  • You could use the Win32 API function GetCommandLineW with the CommandLineToArgvW functionu will than have the good old argc and argv values and can than check the parameter list for your specific values.
    Thursday, October 27, 2011 5:59 AM
  • The idea is to derive from CCommandLineInfo and ceate your own ParseParam override.
    Add member functions to your CCommandLineInfo implementation to store the switch or other data...

    You can always use the undocumented __argc and __argv that are created by the CRT to parse the command line too.


    Martin Richter -- MVP for VC++ [Germany] -- http://blog.m-ri.de
    Thursday, October 27, 2011 8:11 AM
  • Still using this line:

       #include "afxwin.h"

    Then added the following to read the Command Line Data:

       // Get Command Line Data...
       LPTSTR CommandLineDetails = AfxGetApp()->m_lpCmdLine;

    Now seeing the Passed Command Line Details... Will parse the CommandLineDetails now that the Command Line Data is known.

    Thanks for the feedback.

    BillNew


    DirectShow Filter Graph Spy: http://alax.info/blog/777 Extremely helpful for finding deep details for DirectShow Graphs.
    • Marked as answer by BillNew Thursday, October 27, 2011 7:21 PM
    Thursday, October 27, 2011 7:21 PM