Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all
i am stucked in a problem.i want to pass some variables values from a program to a mfc dialog based exe...the number of arguments that i want to pass is 3.
is there any way by which i can pass these three arguments into the mfc based exe..
really i am in a urgent need...
Posted

simply us the getcommandline() function this fucntion stakes the argumnets from the comandline,and acc u could use them.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 24-Jan-12 9:40am    
Thanks for sharing the answer you found. I wish more people would do this once they have found a solution not provided by a member here already. +5
Derive a class from CCommandLineInfo and add your implementation of the virtual member function ParseParam() to process your application specific parameters. The derived class should contain member variables to store your parameters.

Process the command line in InitInstance() of your application using your command line info class:
C++
BOOL CMyApp::InitInstance()
{
//...
    // Parse command line
    CMyCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);
    // Process your command line options here using the member vars
    //  of cmdInfo
//...
    // Optional default handling:
    // Dispatch commands specified on the command line.  Will return FALSE if
    // app was launched with /RegServer, /Register, /Unregserver or /Unregister.
    if (!ProcessShellCommand(cmdInfo))
        return FALSE;
//...
    return TRUE;
}


UPDATE: A very simple CCommandLineInfo derived class.
C++
class CMyCmdLineInfo : public CCommandLineInfo
{
public:
    CMyCmdLineInfo(void);
    virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast);
    bool m_bBoolParam;
    bool m_bHelp;
    CString m_strFileParam;
};

CMyCmdLineInfo::CMyCmdLineInfo()
{
    m_bBoolParam = m_bHelpParam = false;
}

void CMyCmdLineInfo::ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast)
{
    bool bHandled = false;
    TCHAR lpszParse = pszParam;
    if (bFlag)
    {
        switch (*lpszParse)
        {
        case _T('b') :
        case _T('B') : m_bBoolParam = bHandled = true; break;
        case _T('h') :
        case _T('H') :
        case _T('?') : m_bHelp = bHandled = true; break;
        case _T('f') : 
            ++lpszParse;
            if (*lpszParse == _T('='))
                ++lpszParse;
            m_strFileParam = lpszParse;
            bHandled = true;
            break;
        }
    }
    // If the last parameter has no flag, it is treated as the file name to be
    //  opened and the string is stored in the m_strFileName member.
    if (!bHandled)
        CCommandLineInfo::ParseParam(pszParam, bFlag, bLast);
}
 
Share this answer
 
v3
Comments
vaibhavbvp 20-Jan-12 4:53am    
hello are you having any sample code for that..i have tried to inherit class from the CCommandLineInfo ,bt i could not get the desired result...it is showing error in CCommandLineInfo class..
Jochen Arndt 20-Jan-12 5:17am    
I have updated the solution.
Theo Buys 7-Aug-19 5:37am    
Tanks for your solution. There are still some minor bugs in your example code: change TCHAR lpszParse into const TCHAR* lpszParse and change m_bHelp into m_bHelpParam. Also CMyCommandLineInfo don't match with CMyCmdLineInfo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900