CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    Denmark
    Posts
    31

    Command line parameters to a Visual C++ Dialog prog.

    Hi..

    How do i get parameters in a Visual C Dialog based app. (i.e. test.exe /test)

    Regards
    Kasper Kirkeby


  2. #2
    Join Date
    May 1999
    Posts
    24

    Re: Command line parameters to a Visual C++ Dialog prog.

    Use GetCommandLine() function
    and parse it yourself.

    Thanks,
    Ilgar Mashayev

  3. #3
    Join Date
    May 1999
    Posts
    2

    Re: Command line parameters to a Visual C++ Dialog prog.

    I was just working on the same thing and stumbled into this approach. One advantage to it is that you don't have to parse the command line youself, MFC does a lot of the annoying grunt work for you.
    ParseCommandLine goes though the command line sending each piece to CCommandLineInfo::ParseParam. You can add code in your own version of CCommandLineInfo to look for the flags you want. This seems to be fairly well described in the documentation.

    Example:

    In InitInstance()
    ...

    // start addition
    CMyComInfo cmdInfo; // CMyComInfo is derrived from CCommandLineInfo
    ParseCommandLine(cmdInfo);
    // end addition

    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)

    ...

    Simple verion of CMyComInfo

    // MyComInfo.h: interface for the CMyComInfo class.
    //
    //////////////////////////////////////////////////////////////////////

    #if !defined(AFX_MYCOMINFO_H__7C4531D3_02FC_11D3_83E4_00207811095A__INCLUDED_)
    #define AFX_MYCOMINFO_H__7C4531D3_02FC_11D3_83E4_00207811095A__INCLUDED_

    #if _MSC_VER >= 1000
    #pragma once
    #endif // _MSC_VER >= 1000

    class CMyComInfo : public CCommandLineInfo
    {
    public:
    virtual void ParseParam( LPCTSTR lpszParam, BOOL bFlag, BOOL bLast );
    CMyComInfo();
    virtual ~CMyComInfo();

    };

    #endif // !defined(AFX_MYCOMINFO_H__7C4531D3_02FC_11D3_83E4_00207811095A__INCLUDED_)

    and...

    // MyComInfo.cpp: implementation of the CMyComInfo class.
    //
    //////////////////////////////////////////////////////////////////////

    #include "stdafx.h"
    #include "My.h"
    #include "MyComInfo.h"

    #ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    CMyComInfo::CMyComInfo()
    {

    }

    CMyComInfo::~CMyComInfo()
    {

    }

    void CMyComInfo::ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast)
    {
    CMyApp *pApp=(CMyApp*)AfxGetApp();
    if (bFlag) {
    if (CString(lpszParam)==CString("test")) {
    pApp->m_bTest=TRUE;
    // or whatever settings need to be adjusted...
    }
    CCommandLineInfo::ParseParam(lpszParam, bFlag, bLast);
    }

    Thats it... (Yeah Right) I found that this works, but I haven't used it enough to
    really know much about it. Read up on CCommandLineInfo and ParseParam, it may be
    able to do what you want.

    Good Luck,
    -Timothy Eyring


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