The following example can be used to determine whether the-c,-s, or-p option is used when the program is running. You can simply change the option in the specific program according to the example.
Step 1: Reload a class CWzdCommandLineInfo from CCommandLineInfo. The implementation method is as follows:
# If! Defined wzdcommandlineinfo_h <br/> # define wzdcommandlineinfo_h <br/> // wzdcommandlineinfo. h: header file <br/> ////////////////////////////////// //////////////////////////////////////// <br/> // cwzdcommandlineinfo window <br/> class cwzdcommandlineinfo: public ccommandlineinfo <br/>{< br/> // construction <br/> Public: <br/> cwzdcommandlineinfo (); <br/> // attributes <br/> Public: <br/> bool m_bcf Lag; <br/> bool m_bsflag; <br/> bool m_bpflag; <br/> cstring m_sarg; <br/> // Operations <br/> public: <br/> void parseparam (const tchar * pszparam, bool bflag, bool blast); <br/> // overrides <br/> // implementation <br/> public: <br/> virtual ~ Cwzdcommandlineinfo (); <br/> }; <br/> //////////////////////////////////// /// // <br /> # endif
The header file ends. The following is the CPP file.
// WzdCommandLineInfo. cpp: implementation file <br/> // <br/> # include "stdafx. h "<br/> # include" WzdCommandLineInfo. h "</p> <p> # ifdef _ DEBUG <br/> # define new DEBUG_NEW <br/> # undef THIS_FILE <br/> static char THIS_FILE [] = __ FILE __; <br/> # endif <br/> ////////////////////////////// //////////////////////////////////////// //// <br/> // CWzdCommandLineInfo <br/> CWzdCommandLineInfo:: CWzdCommandLineInfo () <br/ >{< Br/> m_bCFlag = FALSE; <br/> m_bSFlag = FALSE; <br/> m_bPFlag = FALSE; <br/> m_sArg = _ T (""); <br/>}< br/> CWzdCommandLineInfo ::~ CWzdCommandLineInfo () <br/>{< br/>}< br/> ////////////////////////// //////////////////////////////////////// /////// <br/> void CWzdCommandLineInfo:: ParseParam (const TCHAR * pszParam, BOOL bFlag, <br/> BOOL bLast) <br/>{< br/> CString sArg (pszParam ); <br/> if (bFlag) <br/>{< br/> m_bCFlag =! SArg. CompareNoCase ("c"); <br/> m_bSFlag =! SArg. CompareNoCase ("s"); <br/> m_bPFlag =! SArg. compareNoCase ("p"); <br/>}< br/> // m_strFileName gets the first nonflag name <br/> else if (m_strFileName.IsEmpty ()) <br/>{< br/> m_sArg = sArg; <br/>}< br/> CCommandLineInfo: ParseParam (pszParam, bFlag, bLast); <br/>}
Step 2: Add member variables to the APP class:
Public:
CWzdCommandLineInfo m_cmdInfo;
Step 3: Modify the following code in the InitInstance () function:
// Parse command line for standard shell commands, DDE, file open <br/> parsecommandline (m_cmdinfo ); <br/> // dispatch commands specified on the command line <br/> If (! Processshellcommand (m_cmdinfo) <br/> return false;
In this way, the CWzdCommandLineInfo object can be obtained through AfxGetApp ()-> m_commaninfo elsewhere in the program, and then the option to run can be known by judging the member variable m_bCFlag.
Reprinted from: http://www.host01.com/article/software/VisualC/20060917184425572.htm