What do I put in ParseParam?

I'm trying to convert my MFC to accept command line args so I do this,

CCommandLineInfo cmdInfo;
ParseCommandLine( cmdInfo );
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

now I'm having trouble figuring out how to do the last line
cmdArg = cmdInfo.ParseParam(???????);

I want to read in a string, an integer, and a couple of LONGINTS.

Thanks
LVL 3
zstafaAsked:
Who is Participating?
 
jkrCommented:
You can download the sample code from http://www.codeproject.com/library/PlgInArchSingInst/SingleInstance.zip

Then, you extract the functions neede from the code and add them tou your app:

void CMyWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo) // overriding 'CWinApp::ParseCommandLine'
{
   for (int i = 0; i < m_commandLineParameters.size(); i++)
   {
       BOOL bFlag = FALSE;
       BOOL bLast = ((i + 1) == m_commandLineParameters.size());
       if (m_commandLineParameters[i].GetAt(0) == '-'
             || m_commandLineParameters[i].GetAt(0) == '/')
       {
           m_commandLineParameters[i] =
m_commandLineParameters[i].Right(m_commandLineParameters[i].GetLength() - 1);
           // remove flag specifier
           bFlag = TRUE;
       }
       rCmdInfo.ParseParam(m_commandLineParameters[i], bFlag, bLast);
   }
}


void CMyWinApp::InitCommandLineParameters(CString command)
{
    TCHAR* pArgs = command.GetBuffer(command.GetLength() + 1);
    CString parameter;
    bool bInQuotes = false;
    enum {TERM  = '\0', QUOTE = '\"'};
   
    // clear any previous command line parameters
    m_commandLineParameters.clear();
   
    while (*pArgs)
    {
        while (isspace(*pArgs)) // skip leading whitespace
        {
            pArgs++;
        }
       
        bInQuotes = (*pArgs == QUOTE);  // see if this token is quoted
       
        if (bInQuotes) // skip leading quote
        {
            pArgs++;
        }
       
        parameter = "";
       
        // Find next token.
        // NOTE: Args are normally terminated by whitespace, unless the
        // arg is quoted.  That's why we handle the two cases separately,
        // even though they are very similar.
        if (bInQuotes)
        {
            // find next quote followed by a space or terminator
            while (*pArgs && !(*pArgs == QUOTE && 
                  (isspace(pArgs[1]) || pArgs[1] == TERM)))
            {
                parameter += *pArgs;
                pArgs++;
            }
            if (*pArgs == QUOTE)
            {
                pArgs++;
                if (pArgs[1])   // if quoted token not followed by a terminator
                {
                    pArgs += 2; // advance to next token
                }
            }
        }
        else
        {
            // skip to next non-whitespace character which delimts a file
            while (*pArgs)
            {
                if (isspace(*pArgs))
                {
                    // check to see if it correctly delimits an existing file
                    CFile file;
                    if (file.Open(parameter, CFile::modeRead))
                    {
                        // file exists, break the line here
                        file.Close();
                        break;
                    }
                    else
                    {
                        // file does not exist, continue parsing the line
                    }
                }
                parameter += *pArgs;
                pArgs++;
            }
            if (*pArgs && isspace (*pArgs)) // end of token
            {
                pArgs++;         // advance to next token or terminator
            }
        }
        if (!parameter.IsEmpty())
        {
            // we parsed a parameter, push onto list
            m_commandLineParameters.push_back(parameter);
        }
    }
    command.ReleaseBuffer(-1);
}

All that's left to do is adding

vector<CString> m_commandLineParameters;

to your class' declaration
0
 
jkrCommented:
There's a nice Article on CodeProject that illustrates how to do that: http://www.codeproject.com/library/PlgInArchSingInst.asp ("MFC extension library : Single instance Plug-In"), in particular http://www.codeproject.com/library/PlgInArchSingInst.asp#process ("Processing the new command line")

void CSIPlugIn::ParseNewCommandLine(CCommandLineInfo& rCmdInfo)
{
    for (int i = 0; i < m_commandLineParameters.size(); i++)
    {
        BOOL bFlag = FALSE;
        BOOL bLast = ((i + 1) == m_commandLineParameters.size());
        if (m_commandLineParameters[i].GetAt(0) == '-'
             || m_commandLineParameters[i].GetAt(0) == '/')
        {
            m_commandLineParameters[i] =
m_commandLineParameters[i].Right(m_commandLineParameters[i].GetLength() - 1);
            // remove flag specifier
            bFlag = TRUE;
        }
        rCmdInfo.ParseParam(m_commandLineParameters[i], bFlag, bLast);
    }
}

0
 
zstafaAuthor Commented:
jkr that is for a new command line instance correct? I just need to do this once at the beginning. Thanks
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
>>I just need to do this once at the beginning

Yes, but the principle remains the same, regardless of when you do that.
0
 
zstafaAuthor Commented:
Is there any way to do this with the current direction I am going or do I have to use this plugin?
0
 
jkrCommented:
You should just use the part of the plugin's code that does the parameter parsing - no need to use the whole thing.
0
 
zstafaAuthor Commented:
In your above class where did you get the CSIPlugIn from? Could you give a little more detail please. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.