Posted on 2011-06-20 17:34
點(diǎn)點(diǎn)滴滴 閱讀(982)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
02 編程語言
獲取命令行的方法:
1、GetCommandLine() 獲取輸入的所有信息,包括程序所在路徑及參數(shù)
2、AfxGetApp()->m_lpCmdLine 只包含參數(shù)
一般情況下,獲取到命令行后就可以針對命令行中的內(nèi)容進(jìn)行相應(yīng)的處理了
CObject
└CCommandLineInfo
類CCommandLineInfo用于分析啟動(dòng)應(yīng)用時(shí)的命令行參數(shù)。
MFC應(yīng)用一般都會(huì)在它的應(yīng)用對象中使用函數(shù)InitInstance創(chuàng)建這個(gè)類的一個(gè)本地實(shí)例。然后把該對象傳給CWinApp::ParseCommandLine,ParseCommandLine又重復(fù)調(diào)用ParseParam填充CCommandLineInfo對象。最后,CCommandLineInfo對象被傳給CWinApp::ProcessShellCommand來處理命令行參數(shù)和選項(xiàng)。
BOOL CExampleApp::InitInstance()
{
...
// 分析標(biāo)準(zhǔn)外殼命令、DDE、打開文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// 調(diào)度在命令行中指定的命令。如果
// 用 /RegServer、/Register、/Unregserver 或 /Unregister 啟動(dòng)應(yīng)用程序,則返回 FALSE。
if (!ProcessShellCommand(cmdInfo))
return FALSE;
...
}
void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
{
for (int i = 1; i < __argc; i++)
{
LPCTSTR pszParam = __targv[i];
BOOL bFlag = FALSE;
BOOL bLast = ((i + 1) == __argc);
if (pszParam[0] == '-' || pszParam[0] == '/')
{
// remove flag specifier
bFlag = TRUE;
++pszParam;
}
rCmdInfo.ParseParam(pszParam, bFlag, bLast);
}
}
這里有個(gè)繼承CCommandLineInfo類的例子
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/geeeeeeee/archive/2008/12/13/3510195.aspx