CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 1999
    Posts
    1

    How do I get the commandline-arguments?

    Hi!
    In my Microsoft Visual C++ 6.0 application I would like to get the commandline arguments, argc and argv. I have seen code like the following ex.
    [int main(int argc, char **argv) { }]
    But that doesn't help me much, since I don't have any 'main' (or do I?). How can I get argc and argv and use them in my View?

    Thanks
    //Stefan



  2. #2
    Join Date
    Jul 1999
    Location
    Moscow, Russia
    Posts
    667

    Re: How do I get the commandline-arguments?

    Hi,
    The extraction command-line switches going on in InitInstance->ParseCommandLine(cmdInfo)::

    void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
    {
    for (int i = 1; i < __argc; i++)
    {
    LPCTSTR pszParam = __targv[i];
    BOOL bFlag = FALSE;
    BOOL bLast = ((i + 1) == __argc);
    if (pszParam[0] == '-' || pszParam[0] == '/')
    {
    // remove flag specifier
    bFlag = TRUE;
    ++pszParam;
    }
    rCmdInfo.ParseParam(pszParam, bFlag, bLast);
    }
    }





    To extract your Param try this:
    1. derive your custom CMyCommandLineInfo structure from CCommandLineInfo.
    2.Override it's membeer function ParseParam like this:

    void CMyCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
    {

    if(strcmp(pszParam,"YOUR_SWITCH")==0)
    {
    ; // YOUR_SWITCH found!!! - do what you want
    return;
    }

    CMyCommandLineInfo::ParseParam(pszParam,bFlag,bLast);
    }




    3. Modify Your InitInstance :

    CMyCommandLineInfo cmdInfo;// insead of CCommandLineInfo cmdInfo
    ParseCommandLine(cmdInfo);





    Hope this help,
    Oleg.


  3. #3
    Join Date
    Jun 1999
    Location
    Sweden
    Posts
    18

    Re: How do I get the commandline-arguments?

    WinAPI function GetCommandLine() returns LPSTR that points to the command line string, using CommandLineToArgv(lpCmdLine, *pNumArgs) you can get an argv array out of this. Or CWinApp::m_lpCmdLine instead of GetCommandLine() if you use MFC.

    Another alternative is available on this site at http://www.codeguru.com/mfc/comments/5199.shtml

    Daniel Bj&oumlrkman
    Sweden

  4. #4
    Join Date
    Jun 1999
    Location
    Miami, FL
    Posts
    972

    Re: How do I get the commandline-arguments?

    The easiest way is via the __argc and __targv macros.

    Cheers!
    Alvaro


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured