我现在遇到一个问题,就是要实现程序从命令行带参数启动,程序需要判断参数的正确与否,如果正确的话,则自动启动某个功能;如果参数不正确的话,则报告参数错误;请问,这样的VC单文档程序如何编写和调试呢?

解决方案 »

  1.   

    same as the ordinary SDI application
    Project-->settings...
    select Debug tab on the right tab view
    See the "Program arguments:" edit box?
    set your init parameters here
    good luck!
      

  2.   

    在VC中设置运行参数在Project菜单的Settings中选择Debug页,在Program argument中填入参数
    CWinApp的m_lpCmdLine中存储了命令行参数
      

  3.   

    谢谢先!能说得详细一点吗?我对Vc不是很熟!我知道VC用ParseCommandLine(cmdInfo)处理输入的参数,怎么才能截取到这个参数呢?参数传递给ProcessShellCommand(cmdInfo)函数进行处理,那如果自己处理传入的参数的话,是不是要改写ProcessShellCommand(cmdInfo)这个函数?怎么改写呢?能不能给一个总体的设计思路和例子?衷心感谢!附:调试的时候,我在那里加入的"-S",跟踪的时候,cmdInfo.m_nShellCommand显示的是"FileNew",这不对吧?
      

  4.   

    Oh, You also need handle the command line info 
    in CXXApp's InitInstance function
    see CCommandLineInfo in MSDN for detail
    note:
    Derive a new class from CCommandLineInfo to handle other flags and parameter values. Override ParseParam to handle the new flags.
      

  5.   

    在CWinApp的InitInstance中m_lpCmdLine变量存储了命令行参数,调试时直接看m_lpCmdLine的值下面是MSDN对m_lpCmdLine的解释
    Points to a null-terminated string that specifies the command line for the application. Use m_lpCmdLine to access any command-line arguments the user entered when the application was started. m_lpCmdLine is a public variable of type LPTSTR.
      

  6.   

    // MyCmdLineInfo.h: interface for the CMyCmdLineInfo class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_MYCMDLINEINFO_H__C9926400_10EF_48B1_B60B_C72D9CA1CEE6__INCLUDED_)
    #define AFX_MYCMDLINEINFO_H__C9926400_10EF_48B1_B60B_C72D9CA1CEE6__INCLUDED_#include <afxwin.h>#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CMyCmdLineInfo : public CCommandLineInfo  
    {
    public:
    CString m_MyParam;
    void ParseParam( LPCTSTR lpszParam, BOOL bFlag, BOOL bLast );
    CMyCmdLineInfo();
    virtual ~CMyCmdLineInfo();};#endif // !defined(AFX_MYCMDLINEINFO_H__C9926400_10EF_48B1_B60B_C72D9CA1CEE6__INCLUDED_)
    // MyCmdLineInfo.cpp: implementation of the CMyCmdLineInfo class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "TestSDI.h"
    #include "MyCmdLineInfo.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////CMyCmdLineInfo::CMyCmdLineInfo()
    {}CMyCmdLineInfo::~CMyCmdLineInfo()
    {}void CMyCmdLineInfo::ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast)
    {
    TRACE("\n%s\n",lpszParam);
    if(strcmp(lpszParam,"sss")==0)
    {
    m_MyParam="myParam";
    }
    else
    CCommandLineInfo::ParseParam(lpszParam,bFlag,bLast);

    }
    //InitInstance******
    // Parse command line for standard shell commands, DDE, file open
    CMyCmdLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);
    TRACE("\n%s\n",cmdInfo.m_MyParam); // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
    return FALSE;
    //******
    Please set "Program arguments:"  /sss or sss
    It may not a complete answer,but you can get the parameter now.
    Good luck!
      

  7.   

    // MyCmdLineInfo.h: interface for the CMyCmdLineInfo class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_MYCMDLINEINFO_H__C9926400_10EF_48B1_B60B_C72D9CA1CEE6__INCLUDED_)
    #define AFX_MYCMDLINEINFO_H__C9926400_10EF_48B1_B60B_C72D9CA1CEE6__INCLUDED_#include <afxwin.h>#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CMyCmdLineInfo : public CCommandLineInfo  
    {
    public:
    CString m_MyParam;
    void ParseParam( LPCTSTR lpszParam, BOOL bFlag, BOOL bLast );
    CMyCmdLineInfo();
    virtual ~CMyCmdLineInfo();};#endif // !defined(AFX_MYCMDLINEINFO_H__C9926400_10EF_48B1_B60B_C72D9CA1CEE6__INCLUDED_)
    // MyCmdLineInfo.cpp: implementation of the CMyCmdLineInfo class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "TestSDI.h"
    #include "MyCmdLineInfo.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////CMyCmdLineInfo::CMyCmdLineInfo()
    {}CMyCmdLineInfo::~CMyCmdLineInfo()
    {}void CMyCmdLineInfo::ParseParam(LPCTSTR lpszParam, BOOL bFlag, BOOL bLast)
    {
    TRACE("\n%s\n",lpszParam);
    if(strcmp(lpszParam,"sss")==0)
    {
    m_MyParam="myParam";
    }
    else
    CCommandLineInfo::ParseParam(lpszParam,bFlag,bLast);

    }
    //InitInstance
    // Parse command line for standard shell commands, DDE, file open
    CMyCmdLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);
    TRACE("\n%s\n",cmdInfo.m_MyParam); // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
    return FALSE;
    //*********
    Please set "Program arguments:" as /sss or sss
    It is not a complete answer,but you can get the parameter now.
    good luck!