Using Main to grab command line arguments if it doesn't already exist?
-
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!
-
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!
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! :)
-
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!
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]
-
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!
As others have said, you either have
main()
or you have your WinApp (theApp
) but not both. And definitely not two instantiations oftheApp
. The command line arguments can be accessed during your InitInstance() function by looking at them_lpCmdLine
data member of your class. There are a number of classes / functions that can help you parse it in theargc/argv
style you're accustomed to, links are in earlier answers to this question. -
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!
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].
-
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!
in your CWinApp:InitInstance, you should be able to access the
__argv
and__argc
variables. -
in your CWinApp:InitInstance, you should be able to access the
__argv
and__argc
variables.I did not know that, been tokenizing
m_lpCmdLine
myself. +5 for the info.