备忘:在VC++单文档程序中,使用自己的命令行参数

来源:互联网 发布:福彩分析软件 编辑:程序博客网 时间:2024/06/05 09:29

在BOOL CxxxApp::InitInstance() 中 , 添加红色字体函数

...

// 分析标准外壳命令、DDE、打开文件操作的命令行
 CCommandLineInfo cmdInfo;
 ParseCommandLine(cmdInfo);
 ProcessCmdLine(cmdInfo);


 // 调度在命令行中指定的命令。如果
 // 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
 if (!ProcessShellCommand(cmdInfo))
  return FALSE;
....

 

函数编写如下:

/**
处理命令行

config001.xml 是xxx.exe的可选配置文件,存放在xxx.exe所在目录下的config子目录中;
命令行可以带上自己的参数,如:xxx.exe -config001.xml 

注意参数,[-]号后面没有空格。[-]号后面的参数与app/config目录下保存的配置文件名一致,不带路径。
*/
void CxxxApp::ProcessCmdLine(CCommandLineInfo &cmdInfo)
{
 CString strConfigFileName;

 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;
  }
  strConfigFileName = pszParam;
  break;
 }

 if ("" == strConfigFileName)
  return ;

 std::string strPath0;
 strPath0 = CFileEx::GetExeDirectory() + CFileEx::Separator() + "Config\\" + strConfigFileName.GetBuffer(0);

 std::string strPath1;
 strPath1 = CFileEx::GetExeDirectory() + CFileEx::Separator() + "Config.xml";

 CMarkup xml;
 if (xml.Load(strPath0.c_str()))
  xml.Save(strPath1.c_str());
}