How do I add the functionality to startup my application based off of a command line argument in Visual C++? Thanks.
Printable View
How do I add the functionality to startup my application based off of a command line argument in Visual C++? Thanks.
If your's is a MFC based application, use CCommandLineInfo.Quote:
Originally Posted by sanclan
Steps explained here.
Thanks. Yes it is an MFC application. I assume I need to add the argument processing in the InitInstance() part of the application. Not sure yet how to do that. If I put app.exe /o then I want it to fire up with a certain dialog box, if it is app.exe /e then put it in an engineering/debug mode, and lastly if it is app.exe /r , run it in a no gui type mode.
Thanks.
Yes...Quote:
Originally Posted by sanclan
Check link in my previous post...Quote:
Originally Posted by sanclan
Okay, so in my InitInstance(), I put CCommandLineInfo cmdLineArg;
Then, use cmdLineArg to read the command line argument somehow, pass it to ParseParam somehow, and then fire off the application in whichever manner the command line argument tells it to?
..This isn't what my post linked above tells.Quote:
Originally Posted by sanclan
Please put some effort into reading and understanding those 4 points, and come back to us if you face problems or dont understand something. It is perhaps simpler for me to post the code, but thats not how things ought to happen.
Okay, thanks. I am new to this and am searching for sample code, that will make it 'click' for me. I have read these 4 things and it still does not make sense. But, thanks. I'll keep trying.
What is it that you don't understand?
I hope you realize that simply copy-pasting code that "clicks" for you will not help you figure out how stuff works, and will not help you maintain your own code.
Not what I said. What I said was, I need to see sample code to understand how it all ties together. I never said anything about copy-pasting, but wouldn't mind that either if it works and I can follow-through it to understand it for the next time around.
One of the links you provided didn't work, but I found my way to it. First, CCommandLineInfo needs to be instantiated in the InitInstance() function of my application object. From that, the object is passed to ParseCommandLine (how?) and then ParseCommandLine repeatedly calls ParseParam to fill the CommandLineInfo object that I created.
Then, ProcessShellCommand gets the object and parses it.
I still do not see how I can use this to pull the /o, /e, and /r arguments, start the application in different modes based on them, etc.
Thanks.
If those MFC classes are hard for you to deal with, another way you can try is using the ::GetCommandLine( ) API.Quote:
Originally Posted by sanclan
Cheers
I have provided only one link... And it works for me. ;)Quote:
Originally Posted by sanclan
The link asks you to inherit from CCommandLineInfo in no uncertain terms... And create an object of the inherited class.Quote:
Originally Posted by sanclan
Why not simply follow instructions?
You are supposed to implement your own ParseParam that gets called once per input paramter. Set a (say boolen) flag when the parameter is recieved by it.Quote:
Originally Posted by sanclan
Do you see correctly now?Quote:
Originally Posted by sanclan
Sorry, but I have provided exactly this information in the link, and you dont seem to be keen on helping yourself to it!
Well... Actually, if one puts effort into understanding, the MFC class gives a neat solution... Of course, this is my opinon... ;)Quote:
Originally Posted by golanshahar
This is the link you provided that doesn't go to where it needs to:
CWinApp::ParseCommandLine
I had already read all of these items prior to posting my help request. Thanks anyway.... I'll just go back to the books. I thought these forums were for help with building the code, provide sample code, etc. My mistake.
- Derive a class from CCommandLineInfo, say CCustomCommandLineInfo
Code:class CCustomCommandLineInfo : public CCommandLineInfo
- Add virtual method, ParseParam
Code:{
virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)..
}
- In ParseParam, check the pszParam passed. Note that this ParseParam will be called as many times as the tokens being used while launching the command line. So, if you call Yourapp.exe /e /o /whatever, you will see 3 calls to ParseParam each with "/e", "/o", "/whatever".
Code:class CCustomCommandLineInfo : public CCommandLineInfo
{
CCustomCommandLineInfo()
{
m_bExport = m_bOpen = m_bWhatever = FALSE;
}
//for convenience maintain 3 variables to indicate the param passed.
BOOL m_bExport ;//for /e
BOOL m_bOpen; //for /o
BOOL m_bWhatever;//for /whatever
//public methods for checking these.
public:
BOOL IsExport(){return m_bExport;};
BOOL IsOpen(){return m_bOpen;};
BOOL IsWhatever() { return m_bWhatever;};
virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
{
if(0 == strcmp(pszParam,"/o"))
{
m_bOpen = TRUE;
}
else if(0 == strcmp(pszParam,"/e"))
{
m_bExport = TRUE;
}
else if(0 == strcmp(pszParam,"/whatever"))
{
m_bWhatever = TRUE;
}
}
}
- Instantiate ab object of this class in InitInstance and parse it.
Code:CCustomCommandLineInfo oInfo;
ParseCommandLine(&oInfo);
- And then check
Code:if(oInfo.IsExport())
{
//do something
}
else if(oInfo.IsWhatever())
{
//do whatever
}
Of course, I agree but I saw OP got confused (for some reason) so I offered another way of action :D.Quote:
Originally Posted by Siddhartha
Cheers