Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using Main to grab command line arguments if it doesn't already exist?

Using Main to grab command line arguments if it doesn't already exist?

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestoolstutorialquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucidation
    wrote on last edited by
    #1

    First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said... I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:

    #include "stdafx.h"
    #include ...a few other files.....

    class CApp : public CWinAppEx
    {
    public:
    CApp();
    public:
    virtual BOOL InitInstance();
    };

    extern CApp theApp;

    int main(int argc, char *argv[])
    {
    MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
    }

    // CApp construction
    CApp::CApp()
    {
    MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
    }

    // CApp object
    CApp theApp;

    // CApp initialization
    BOOL CApp::InitInstance()
    //int main(array ^args)
    {
    MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
    ...does other stuff here...
    }

    So what do I need to do to use MAIN correctly to grab the command line arguments? Thanks!

    A C C S C 5 Replies Last reply
    0
    • L Lucidation

      First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said... I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:

      #include "stdafx.h"
      #include ...a few other files.....

      class CApp : public CWinAppEx
      {
      public:
      CApp();
      public:
      virtual BOOL InitInstance();
      };

      extern CApp theApp;

      int main(int argc, char *argv[])
      {
      MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
      }

      // CApp construction
      CApp::CApp()
      {
      MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
      }

      // CApp object
      CApp theApp;

      // CApp initialization
      BOOL CApp::InitInstance()
      //int main(array ^args)
      {
      MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
      ...does other stuff here...
      }

      So what do I need to do to use MAIN correctly to grab the command line arguments? Thanks!

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      It looks like you're using MFC... in which case the main() function is hidden away in the framework. You need to do something a little different. Here are some examples/documentation: http://www.codeguru.com/forum/showthread.php?t=386406[^] http://msdn.microsoft.com/en-us/library/zaydx040%28VS.80%29.aspx[^] http://www.visualcpp.org/?p=33[^] Good luck! :)

      1 Reply Last reply
      0
      • L Lucidation

        First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said... I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:

        #include "stdafx.h"
        #include ...a few other files.....

        class CApp : public CWinAppEx
        {
        public:
        CApp();
        public:
        virtual BOOL InitInstance();
        };

        extern CApp theApp;

        int main(int argc, char *argv[])
        {
        MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
        }

        // CApp construction
        CApp::CApp()
        {
        MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
        }

        // CApp object
        CApp theApp;

        // CApp initialization
        BOOL CApp::InitInstance()
        //int main(array ^args)
        {
        MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
        ...does other stuff here...
        }

        So what do I need to do to use MAIN correctly to grab the command line arguments? Thanks!

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        You have code for a Windows application there. And it also looks like it is using MFC. You won't find a main(), nor will you need to. Odd, though is that you have 'theApp' declared twice. That's definitely not a good start. There are methods of the CWinAppEx class that can be called in InitInstance to assist with command line handling. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        1 Reply Last reply
        0
        • L Lucidation

          First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said... I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:

          #include "stdafx.h"
          #include ...a few other files.....

          class CApp : public CWinAppEx
          {
          public:
          CApp();
          public:
          virtual BOOL InitInstance();
          };

          extern CApp theApp;

          int main(int argc, char *argv[])
          {
          MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
          }

          // CApp construction
          CApp::CApp()
          {
          MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
          }

          // CApp object
          CApp theApp;

          // CApp initialization
          BOOL CApp::InitInstance()
          //int main(array ^args)
          {
          MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
          ...does other stuff here...
          }

          So what do I need to do to use MAIN correctly to grab the command line arguments? Thanks!

          C Offline
          C Offline
          Chuck OToole
          wrote on last edited by
          #4

          As others have said, you either have main() or you have your WinApp (theApp) but not both. And definitely not two instantiations of theApp. The command line arguments can be accessed during your InitInstance() function by looking at the m_lpCmdLine data member of your class. There are a number of classes / functions that can help you parse it in the argc/argv style you're accustomed to, links are in earlier answers to this question.

          1 Reply Last reply
          0
          • L Lucidation

            First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said... I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:

            #include "stdafx.h"
            #include ...a few other files.....

            class CApp : public CWinAppEx
            {
            public:
            CApp();
            public:
            virtual BOOL InitInstance();
            };

            extern CApp theApp;

            int main(int argc, char *argv[])
            {
            MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
            }

            // CApp construction
            CApp::CApp()
            {
            MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
            }

            // CApp object
            CApp theApp;

            // CApp initialization
            BOOL CApp::InitInstance()
            //int main(array ^args)
            {
            MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
            ...does other stuff here...
            }

            So what do I need to do to use MAIN correctly to grab the command line arguments? Thanks!

            S Offline
            S Offline
            Software_Developer
            wrote on last edited by
            #5

            Look [here] for parsing C++ Command-Line Arguments in a console app's main. More [here] and [here]. Look [here] and [here] for parsing C++ Command-Line Arguments in MFC. MSDN : [CWinApp::ParseCommandLine] and [CCommandLineInfo::CCommandLineInfo].

            1 Reply Last reply
            0
            • L Lucidation

              First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said... I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:

              #include "stdafx.h"
              #include ...a few other files.....

              class CApp : public CWinAppEx
              {
              public:
              CApp();
              public:
              virtual BOOL InitInstance();
              };

              extern CApp theApp;

              int main(int argc, char *argv[])
              {
              MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
              }

              // CApp construction
              CApp::CApp()
              {
              MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
              }

              // CApp object
              CApp theApp;

              // CApp initialization
              BOOL CApp::InitInstance()
              //int main(array ^args)
              {
              MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
              ...does other stuff here...
              }

              So what do I need to do to use MAIN correctly to grab the command line arguments? Thanks!

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              in your CWinApp:InitInstance, you should be able to access the __argv and __argc variables.

              image processing toolkits | batch image processing

              C 1 Reply Last reply
              0
              • C Chris Losinger

                in your CWinApp:InitInstance, you should be able to access the __argv and __argc variables.

                image processing toolkits | batch image processing

                C Offline
                C Offline
                Chuck OToole
                wrote on last edited by
                #7

                I did not know that, been tokenizing m_lpCmdLine myself. +5 for the info.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups