MFC解析启动命令行参数——CCommandLineInfo类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianrolin/article/details/46242139
MFC中CCommandLineInfo类被用于分析启动应用时的命令行参数。
MFC应用一般都会在它的应用对象中使用函数InitInstance()创建这个类的一个本地实例。然后把该对象传给CWinApp::ParseCommandLine(),ParseCommandLine()又重复调用ParseParam()填充CCommandLineInfo对象。最后,CCommandLineInfo对象被传给CWinApp::ProcessShellCommand()来处理命令行参数和选项。所以我们会在App类InitInstance()函数中发现如下几行代码:
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
	return FALSE;

这几行代码是程序启动时创建新文档的关键代码。


1:我们首先来看看让CCommandLineInfo类的结构

//in afxwin.h
class CCommandLineInfo : public CObject
{
     public:
     // Sets default values
   CCommandLineInfo();
   BOOL m_bShowSplash;
   BOOL m_bRunEmbedded;
   BOOL m_bRunAutomated;
   enum { FileNew, FileOpen, FilePrint, FilePrintTo, FileDDE, AppRegister,
   AppUnregister, FileNothing = -1 } m_nShellCommand;
// not valid for FileNew
   CString m_strFileName;
   . . .
   ~CCommandLineInfo();
   . . .
};

这里要重点注意enum {FileNew, . . . , FileNothing = -1 } m_nShellCommand;
这里联合类型定义的m_nShellCommand 就是外壳程序执行的命令类型。如果m_nShellCommand设置为FileNew,那么程序就会创建新文档;如果想在文档开始时不创建新文档,就必须将m_nShellCommand设置为FilleNothing。

下面我们再看看CCommandLineInfo的构造函数:

//in appcore.cpp
CCommandLineInfo::CCommandLineInfo()
{
         m_bShowSplash   = TRUE;
         m_bRunEmbedded   = FALSE;
         m_bRunAutomated = FALSE;
         m_nShellCommand = FileNew;
}

这里很明白的看出,构造函数中,缺省将 m_nShellCommand设置为 FileNew。

2:再来看看ParseCommandLine(cmdInfo)函数

void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
{
     for (int i = 1; i < __argc; i++)   // extern int __argc;          
     {
         LPCTSTR pszParam = __targv[i];   //extern char ** __argv;      
                                            extern wchar_t ** __wargv;  
                                            difine __targv   __wargv
         BOOL bFlag = FALSE;
         BOOL bLast = ((i + 1) == __argc);
         if (pszParam[0] == '-' || pszParam[0] == '/')
         {
             // remove flag specifier
             bFlag = TRUE;
             ++pszParam;
         }
         rCmdInfo.ParseParam(pszParam, bFlag, bLast);
     }
}

可以看出ParseCommandLine()主要是对输入的命令行参数做一些分析,并调用ParseParam()来进行处理。继续分析ParseParam()函数,查看如下源代码:

void CCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
{
     if (bFlag)
     {
         USES_CONVERSION;
         ParseParamFlag(T2CA(pszParam));
     }
     else
         ParseParamNotFlag(pszParam);
     ParseLast(bLast);
}

其它的函数撇开不看,我们重点来分析一下ParseParamFlag()和ParseLast()函数。

void CCommandLineInfo::ParseParamFlag(const char* pszParam)
{
     // OLE command switches are case insensitive, while
     // shell command switches are case sensitive
     if (lstrcmpA(pszParam, "pt") == 0)
         m_nShellCommand = FilePrintTo;
     else if (lstrcmpA(pszParam, "p") == 0)
         m_nShellCommand = FilePrint;
     else if (lstrcmpiA(pszParam, "Unregister") == 0 ||
             lstrcmpiA(pszParam, "Unregserver") == 0)
         m_nShellCommand = AppUnregister;
     else if (lstrcmpA(pszParam, "dde") == 0)
     {
         AfxOleSetUserCtrl(FALSE);
         m_nShellCommand = FileDDE;
     }
     else if (lstrcmpiA(pszParam, "Embedding") == 0)
     {
         AfxOleSetUserCtrl(FALSE);
         m_bRunEmbedded = TRUE;
         m_bShowSplash = FALSE;
     }
     else if (lstrcmpiA(pszParam, "Automation") == 0)
     {
         AfxOleSetUserCtrl(FALSE);
         m_bRunAutomated = TRUE;
         m_bShowSplash = FALSE;
     }
}

ParseParamFlag()判断传过来的字符串,判断它的参数类型,并根据参数类型做不同的处理。

void CCommandLineInfo::ParseLast(BOOL bLast)
{
     if (bLast)
     {
         if (m_nShellCommand == FileNew && !m_strFileName.IsEmpty())
             m_nShellCommand = FileOpen;
         m_bShowSplash = !m_bRunEmbedded && !m_bRunAutomated;
     }
}
ParseLast()会判断是否是是FileNew打开新文档,如果是打开新文档,并且打开的文档名不为空的话, 就假定用户想打开这个文档,把命令设置为FileOpen。

因此,我们可以总结一下函数ParseCommandLine()的作用:ParseCommandLine()的作用主要是分析命令行参数,如果没有命令行参数,ParseCommandLine()就假定用户想新建一个文档,于是设置一个FileNew命令;如果命令行参数中有一个文件名,ParseCommandLine()就假定用户想打开该文件,于是设置一个FileOpen命令。


3:接下来,我们来重点看看外壳命令解析的主角:ProcessShellCommand()

BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
{
       BOOL bResult = TRUE;
       switch (rCmdInfo.m_nShellCommand)
     {
           case CCommandLineInfo::FileNew:
                   if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
                         OnFileNew();
                   if (m_pMainWnd == NULL)
                         bResult = FALSE;
                   break;
         case CCommandLineInfo::FileOpen:       . . .
         case CCommandLineInfo::FilePrintTo:     . . .
         case CCommandLineInfo::FilePrint:       . . .
         case CCommandLineInfo::FileDDE:       . . .
         case CCommandLineInfo::AppRegister:   . . .
         case CCommandLineInfo::AppUnregister: . . .
         . . .
       }
}

此函数如果成功地处理了外壳命令,则返回非零值,否则返回FALSE。代码看到这里,一切都很明白了。ProcessShellCommand()分析m_nShellCommand,并根据m_nShellCommand不同的类型值进行不同的处理。


4:最后,我们再来分析文章最初提到的App类InitInstance()函数中的几行代码。

    1) 当CCommandLineInfo cmdInfo进行定义时,首先调用构造函数,构造函数中m_nShellCommand被设置为FileNew;
    2) 然后执行ParseCommandLine(cmdInfo)对命令进行分析;
    3) 最后调用ProcessShellCommand(cmdInfo)处理命令行参数和标志。ProcessShellCommand()判断m_nShellCommand为FileNew,于是调用OnFileNew()创建了一个新的文档。

这也就是创建新文档的来龙去脉。

如果我们希望应用程序启动时不默认打开空白文档,则应该:

CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

if (cmdInfo.m_strFileName.IsEmpty())
{
	cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
}

if (!ProcessShellCommand(cmdInfo))
	return FALSE;

注意:这里很多网上给的解决方案都是

CCommandLineInfo cmdInfo;
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo))
	return FALSE;

这样的做法运行起来貌似效果也相同,但是当用命令行或资源管理器调用应用程序执行时,就无法正常打开文档。因为不论青红皂白一律把m_nSellCommand设置为了FileNothing。


猜你喜欢

转载自blog.csdn.net/tianrolin/article/details/46242139
今日推荐