CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Feb 2006
    Posts
    31

    Re: Command Line Arguments

    Kirants, now that makes sense! Thanks. I will work this out in my code and let you know how it works. Thanks again!

  2. #17
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Command Line Arguments

    Quote Originally Posted by sanclan
    I had already read all of these items prior to posting my help request.
    Your claim that you read the items is not vindicated in your posts where you have instantiated objects of the wrong class in spite of having been given clear instructions.
    Quote Originally Posted by Me
    Your implementation would be as follows...
    1. You can write your own CommandLine handling class (say CYourCmdLineHandler) that inherits from CCommandLineInfo.
    2. In your class you would override CCommandLineInfo::ParseParam
    3. You would pass an object of this class in a call to CWinApp::ParseCommandLine
    4. Inside CYourCmdLineHandler::ParseParam you would handle individual cases.
    You were clearly instructed to ask if something was not understood.
    You had nothing to ask. Did you?
    Quote Originally Posted by sanclan
    I had already read all of these items prior to posting my help request. Thanks anyway.... I'll just go back to the books. I thought these forums were for help with building the code, provide sample code, etc. My mistake.
    Excuse me but we here help people who attempt at helping themselves first.

    Anyways, if code is what helps you, implement this -
    Code:
    class CYourCommandLineInfo : public CCommandLineInfo
    {
    public: 
      void ParseParam(
    	 const TCHAR* pszParam, 
    	 BOOL bFlag,
    	 BOOL bLast
      )
      {
    	 // Implement this to handle your parameters
      }   
    };
    ...Create an instance of this, and pass it to CWinApp::ParseCommandLine

    Your ParseParam will be called for command line parameters, and you can implement it to set flags of essence.

    Now, you have code... On a platter.

    EDIT: Ah, I see Kiran has...
    Last edited by Siddhartha; February 15th, 2006 at 04:59 PM.

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

    Re: Command Line Arguments

    Quote Originally Posted by Siddhartha
    the MFC class gives a neat solution... Of course, this is my opinon...
    Sadly, I am surprised how few projects I have worked on have used this approach when it seems to be the most object oriented way and fitting nicely with existing framework also..

  4. #19
    Join Date
    Feb 2006
    Posts
    31

    Re: Command Line Arguments

    I tried using the Visual C++ 6.0 wizard to add this class to my project. I used the Generic C++ class. When I do this, does the code for the deriving of the CCommandLineInfo go into the CCustomCommandLineInfo.h?

  5. #20
    Join Date
    Feb 2006
    Posts
    31

    Re: Command Line Arguments

    What am I doing wrong? I am trying to create a class that reads the command line argument and based on the argument, runs in either an Operator mode (basic dialog gui asking for information and executing off of that information), Engineering mode (all dialogs and menus are shown and useable), or Server mode (no guis or dialog boxes).


    Here is my CCommandLineParse.h code:
    Code:
     //--------------
    #pragma once
    #include "afxwin.h"
    
    class CCommandLineParse : public CCommandLineInfo
    {
    	// Constructor/Destructor
    public:
    	CCommandLineParse(void);
    	~CCommandLineParse(void);
       BOOL IsEngineering(){return m_bEngineering;};
       BOOL IsOperator(){return m_bOperator;};
       BOOL IsServer() { return m_bServer;};
    
    private:
       BOOL m_bEngineering;
       BOOL m_bOperator;
       BOOL m_bServer;
    };
    //-------------------
    
    Here is my CCommandLineParse.cpp code:
    
    //-------------------------------------
    
    
    #include "stdafx.h"
    #include ".\commandlineparse.h"
    
    CCommandLineParse::CCommandLineParse()
    {
    	CCommandLineParse()
       {
          m_bEngineering = m_bOperator = m_bServer = FALSE;
       }
       //jhs 02/16/06 - variables to indicate the param passed
       BOOL m_bEngineering ;//for /e
       BOOL m_bOperator; //for /o
       BOOL m_bServer;//for /s
     
       //jhs 02/16/06 - public methods for checking these
    public:
       BOOL IsEngineering(){return m_bEngineering;};
       BOOL IsOperator(){return m_bOperator;};
       BOOL IsServer() { return m_bServer;};
       
       virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
       {
            if(0 == strcmp(pszParam,"/e"))
            {
                m_bEngineering = TRUE;
             } 
              else if(0 == strcmp(pszParam,"/o"))
             {
                m_bOperator = TRUE;
             }
             else if(0 == strcmp(pszParam,"/s"))
            {
                m_bServer = TRUE;
             }
       }
    
    }
    
    CCommandLineParse::~CCommandLineParse(void)
    {
    }
    
    //--------------------------------------------
    I am getting errors such as:
    c:\Working\MapTextConverter\CommandLineParse.cpp(6): error C2143: syntax error : missing ';' before '{'
    c:\Working\MapTextConverter\CommandLineParse.cpp(16): error C2143: syntax error : missing ';' before 'public'
    c:\Working\MapTextConverter\CommandLineParse.cpp(17): error C2143: syntax error : missing ';' before '{'
    c:\Working\MapTextConverter\CommandLineParse.cpp(17): error C2534: 'CCommandLineParse' : constructor cannot return a value
    c:\Working\MapTextConverter\CommandLineParse.cpp(17): error C2562: 'CCommandLineParse::CCommandLineParse' : 'void' function returning a value
    c:\Working\MapTextConverter\CommandLineParse.cpp(18): error C2601: 'IsOperator' : local function definitions are illegal
    c:\Working\MapTextConverter\CommandLineParse.cpp(19): error C2601: 'IsServer' : local function definitions are illegal
    c:\Working\MapTextConverter\CommandLineParse.cpp(22): error C2575: 'ParseParam' : only member functions and bases can be virtual
    c:\Working\MapTextConverter\CommandLineParse.cpp(22): error C2601: 'ParseParam' : local function definitions are illegal
    Last edited by Siddhartha; February 17th, 2006 at 05:47 PM. Reason: Added Code-Tags...

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

    Re: Command Line Arguments

    Why are you writing functions inside functions ?
    Please bring them out..

    1. in your commandlineparse.h file class declaration, you do not have ParseParam..
    2. in the .cpp file, write all the functions in declared in .h file seperately qualified by CCommandLineParse:: ( this means they are all class members )
      Code:
      CCommandLineInfo::CCommandLineInfo()
      {
      ...contents here
      }
      BOOL CCommandLine::ParseParam(...)
      {
      ...contents here
      }
    3. Please make it a habit to look at the style of the appwizard generated code and follow similarly if you have confusion.
    4. When you hit a compiler error, make it a point to understand it. It doesn't harm in using the best available tools available to you ( MSDN ) before you post in codeguru. Place caret on the compiler errror, for e.g. C2601 and hit F1. This will bring up the help for that error.
      Visual Studio IDE, MSDN etc. are your friends. Get to know them and you will have fun.
    5. Please use code tags while posting code snippets. See my signature on how to use them.
    Last edited by kirants; February 17th, 2006 at 12:12 PM. Reason: tags added

  7. #22
    Join Date
    May 1999
    Location
    Saint Paul, Minnesota, US
    Posts
    91

    Re: Command Line Arguments

    Hi ...

    Just a follow-up. I have implemented this code and it is working good with a couple small modifications. Below is the full implementatio into a CWinApp derived application. I will instanciate the class in the InitInstance() method and then parse the command line.

    Thanks,
    Chris

    IMPEACH BUSH !


    Code:
    /////////////////////////////////////////////////////////////////////////////////////
    // CustomCommandLineInfo.h : header file
    /////////////////////////////////////////////////////////////////////////////////////
    
    #if !defined(CUSTOM_COMMAND_LINE_INFO_)
    #define CUSTOM_COMMAND_LINE_INFO_
    
    class CCustomCommandLineInfo : public CCommandLineInfo
    {
    // Construction
    public:
        CCustomCommandLineInfo();
    
        virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast);
    
        //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;};
    
    };
    
    #endif // !defined(CUSTOM_COMMAND_LINE_INFO_)
    
    
    /////////////////////////////////////////////////////////////////////////////////////
    // CustomCommandLineInfo.cpp Implementation
    /////////////////////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "CustomCommandLineInfo.h"
    
    CCustomCommandLineInfo::CCustomCommandLineInfo()
    {
        m_bExport = m_bOpen = m_bWhatever = FALSE;
    }
    
    void CCustomCommandLineInfo::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;
        }
    }
    
    
    /////////////////////////////////////////////////////////////////////////////////////
    // CTestMmcSnapInRunApp initialization
    /////////////////////////////////////////////////////////////////////////////////////
    
    #include "CustomCommandLineInfo.h"
    
    BOOL CTestMmcSnapInRunApp::InitInstance()
    {
    	AfxEnableControlContainer();
    
    	// Standard initialization
    	// If you are not using these features and wish to reduce the size
    	//  of your final executable, you should remove from the following
    	//  the specific initialization routines you do not need.
    
    #ifdef _AFXDLL
    	Enable3dControls();			// Call this when using MFC in a shared DLL
    #else
    	Enable3dControlsStatic();	// Call this when linking to MFC statically
    #endif
    
        CCustomCommandLineInfo oInfo;
        ParseCommandLine(oInfo);
    
        if(oInfo.IsExport())
        {
            Beep(1000, 100); 
        }
        else if(oInfo.IsWhatever())
        {
            Beep(1000, 100); 
        }
        else if(oInfo.IsOpen())
        {
            Beep(1000, 100); 
        }
        else
        { 
            // Call this method to load the mmc and exit this app. 
            LoadMmcApplication(); 
        } 
    
    	CTestMmcSnapInRunDlg dlg;
    	m_pMainWnd = &dlg;
    	int nResponse = dlg.DoModal();
    	if (nResponse == IDOK)
    	{
    		// TODO: Place code here to handle when the dialog is
    		//  dismissed with OK
    	}
    	else if (nResponse == IDCANCEL)
    	{
    		// TODO: Place code here to handle when the dialog is
    		//  dismissed with Cancel
    	}
    
    	// Since the dialog has been closed, return FALSE so that we exit the
    	//  application, rather than start the application's message pump.
    	return FALSE;
    }

Page 2 of 2 FirstFirst 12

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