C++MFC implement a parameter launch test



  • Ask as simple a way as possible to carry out the verification that the annex is launched with a parameter. I mean, if there's no parameter to make a mistake, if it matches to start.

    C++MFC

    Any examples?



  • Use it. ParseCommandLine

    Example:

    NewComandLineInfo.h
    

    class CNewCommandLineInfo : public CCommandLineInfo
    {
    public:
    CString m_sHostAddr;
    UINT m_nPort;

    void ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast);
    

    };
    NewComandLineInfo.cpp

    #include "stdafx.h"
    #include "CommandLineInfoEx.h"

    void CNewCommandLineInfo::ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast)
    {
    if(bFlag) {
    CString sParam(lpszParam);
    if (sParam.Left(2) == "h:") {
    m_sHostAddr = sParam.Right(sParam.GetLength() - 2);
    return;
    }

        if (sParam.Left(2) == "p:") {
            CString sTemp;
    
            sTemp = sParam.Right(sParam.GetLength() - 2);
            m_nPort = atoi(sTemp);
            return;
        }
    }
    
    // Call the base class to ensure proper command line processing
    CCommandLineInfo::ParseParam(lpszParam, bFlag, bLast);
    

    }
    We also need to modify the CWinApp derived class to include our special command line processing:

    // Example.cpp : Defines the class behaviors for the application.
    //

    #include "NewCommandLineInfo.h"

    ...

    BOOL CExampleApp::InitInstance()
    {
    ...
    // Parse command line for standard shell commands, DDE, file open
    // and user-defined flags. The CCommandLine class has been replaced
    CNewCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);

    // Access the variables if needed
    CString sHostAddr = cmdInfo.m_sHostAddr;
    UINT nPort = cmdInfo.m_nPort;
    ...
    

    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2