MFC解析啟動命令列引數——CCommandLineInfo類
阿新 • • 發佈:2018-10-31
MFC中CCommandLineInfo類被用於分析啟動應用時的命令列引數。
MFC應用一般都會在它的應用物件中使用函式InitInstance()建立這個類的一個本地例項。然後把該物件傳給CWinApp::ParseCommandLine(),ParseCommandLine()又重複呼叫ParseParam()填充CCommandLineInfo物件。最後,CCommandLineInfo物件被傳給CWinApp::ProcessShellCommand()來處理命令列引數和選項。所以我們會在App類InitInstance()函式中發現如下幾行程式碼:
這裡要重點注意enum {FileNew, . . . , FileNothing = -1 } m_nShellCommand;
這裡聯合型別定義的m_nShellCommand 就是外殼程式執行的命令型別。如果m_nShellCommand設定為FileNew,那麼程式就會建立新文件;如果想在文件開始時不建立新文件,就必須將m_nShellCommand設定為FilleNothing。
這裡很明白的看出,建構函式中,預設將 m_nShellCommand設定為 FileNew。
可以看出ParseCommandLine()主要是對輸入的命令列引數做一些分析,並呼叫ParseParam()來進行處理。繼續分析ParseParam()函式,檢視如下原始碼:
其它的函式撇開不看,我們重點來分析一下ParseParamFlag()和ParseLast()函式。
ParseParamFlag()判斷傳過來的字串,判斷它的引數型別,並根據引數型別做不同的處理。
此函式如果成功地處理了外殼命令,則返回非零值,否則返回FALSE。程式碼看到這裡,一切都很明白了。ProcessShellCommand()分析m_nShellCommand,並根據m_nShellCommand不同的型別值進行不同的處理。
注意:這裡很多網上給的解決方案都是
這樣的做法執行起來貌似效果也相同,但是當用命令列或資源管理器呼叫應用程式執行時,就無法正常開啟文件。因為不論青紅皁白一律把m_nSellCommand設定為了FileNothing。
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。