CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    MFC General: How to process command line arguments in a MFC application?

    Q: How to process command line arguments in a MFC application?

    A:
    • Derive a class from CCommandLineInfo, say 'CCustomCommandLineInfo'


      Code:
      class CCustomCommandLineInfo : public CCommandLineInfo

    • Add virtual method 'ParseParam()'


      Code:
      {
        virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast);
      }
    • In 'ParseParam()', check the 'pszParam' passed. Note, that 'ParseParam()' will be called as many times as the tokens being used while launching the command line. So, if you call 'Yourapp.exe /e /o /whatever', you will see 3 calls to 'ParseParam()' each with "/e", "/o", "/whatever".


      Code:
      class CCustomCommandLineInfo : public CCommandLineInfo
      {
        CCustomCommandLineInfo()
        {
          m_bExport = m_bOpen = m_bWhatever = FALSE;
        }
      
        //for convenience maintain 3 variables to indicate the param passed. 
        BOOL m_bExport;       //for /e
        BOOL m_bOpen;         //for /o
        BOOL m_bWhatever;     //for /whatever
       
        //public methods for checking these.
      public:
        BOOL IsExport() { return m_bExport; };
        BOOL IsOpen() { return m_bOpen; };
        BOOL IsWhatever() { return m_bWhatever; };
         
        virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
        {
          if(0 == strcmp(pszParam, "/o"))
          {
            m_bOpen = TRUE;
          } 
          else if(0 == strcmp(pszParam, "/e"))
          {
            m_bExport = TRUE;
          }
          else if(0 == strcmp(pszParam, "/whatever"))
          {
            m_bWhatever = TRUE;
          }
        }
      };
    • Instantiate an object of this class in 'InitInstance()' and parse it like below.


      Code:
      BOOL CYourApp::InitInstance()
      {
        // Do all the stuff you want to do, like registering document tempates etc.
      
        CCustomCommandLineInfo oInfo;
        ParseCommandLine(oInfo);
        // ....
      }
    • And then check


      Code:
      BOOL CYourApp::InitInstance()
      {
        // Do all the stuff you want to do, like registering document tempates etc.
      
        CCustomCommandLineInfo oInfo;
        ParseCommandLine(oInfo);
      
        if(oInfo.IsExport())
        {
          // Do something
        }
        else if(oInfo.IsWhatever())
        {
          // Do whatever
        }
      
        // ....
      }



    Last edited by cilu; November 6th, 2007 at 02:07 AM. Reason: fixed parameters

  2. #2
    Join Date
    Nov 2009
    Posts
    1

    Re: MFC General: How to process command line arguments in a MFC application?

    Just as a note, this does not work in Visual Studio if the Configuration Properties->General->Character Set field is set to Unicode. For that, the pszParam must be of type LPCTSTR. Just wanted to point that out in case it wasn't obvious to anybody else.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured