GetCommandLine 获取当前进程的命令行字符串 对字符串进行分割

时间:2024-02-20 09:46:00
#include "string.h"
#include <vector>

void CAutoZipDlg::OnBnClickedOk()
    {
    using namespace std;
    vector<CString> strVec;

    CString teststr = GetCommandLine(); //retrives the command-line string for the current process.
    CString outputstr; 
    int flag = 0;

    for(int i = 0;i < teststr.GetLength(); i++)
    {
        if(teststr[i]==\'\"\')
        {
            ++flag;

        }
        else if(flag == 2)
        {
            //MessageBox(outputstr, "outputstr");
            strVec.push_back(outputstr);
            outputstr = "";
            flag = 0;
        }
        else if(teststr[i]==\' \' && flag == 0)
        {
            //MessageBox(outputstr, "outputstr");
            strVec.push_back(outputstr);
            outputstr = "";
            
        }
        else
        {
            outputstr += teststr[i];    
        }
    }
 
    if(outputstr.GetLength()>0)
    {
        //MessageBox(outputstr, "outputstr");
        strVec.push_back(outputstr);
    }

    for(vector<CString>::const_iterator it = strVec.begin()+1;it != strVec.end();it++)
    {
        //TRACE(*it);
        //TRACE("\n");
        MessageBox(*it, "文件路径");
    }
}

习惯了用argv来处理命令行的字符串,来到mfc还是有点不习惯的。

对命令行字符串的处理,方法之一。刚入门不久,希望今后可以改进!

http://www.pythonschool.com/python/3.html 转摘