如何将参数从程序传递到基于Mfc的exe [英] how to pass arguments froma program to a mfc based exe

查看:122
本文介绍了如何将参数从程序传递到基于Mfc的exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我陷入了问题.我想将一些变量值从程序传递到基于MFC的exe对话框...我想传递的参数数是3.
有什么方法可以将这三个参数传递给基于Mfc的exe..
确实我非常需要...

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...

推荐答案

简单地向我们提供getcommandline()函数,此功能会将COMANDLINE中的argumnets放到中间,因此您可以使用它们.
simply us the getcommandline() function this fucntion stakes the argumnets from the comandline,and acc u could use them.


CCommandLineInfo派生一个类,并添加虚拟成员函数ParseParam()的实现以处理特定于应用程序的参数.派生类应包含用于存储参数的成员变量.

使用命令行信息类在应用程序的InitInstance()中处理命令行:
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:
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.

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);
}


这篇关于如何将参数从程序传递到基于Mfc的exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆