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
Kirants, now that makes sense! Thanks. I will work this out in my code and let you know how it works. Thanks again!
Your claim that you read the items is not vindicated in your posts where you have instantiated objects of the wrong class in spite of having been given clear instructions.Quote:
Originally Posted by sanclan
You were clearly instructed to ask if something was not understood.Quote:
Originally Posted by Me
You had nothing to ask. Did you? ;)
Excuse me but we here help people who attempt at helping themselves first.Quote:
Originally Posted by sanclan
Anyways, if code is what helps you, implement this -
...Create an instance of this, and pass it to CWinApp::ParseCommandLineCode:class CYourCommandLineInfo : public CCommandLineInfo
{
public:
void ParseParam(
const TCHAR* pszParam,
BOOL bFlag,
BOOL bLast
)
{
// Implement this to handle your parameters
}
};
Your ParseParam will be called for command line parameters, and you can implement it to set flags of essence.
Now, you have code... On a platter. :cool:
EDIT: Ah, I see Kiran has...
Sadly, I am surprised how few projects I have worked on have used this approach when it seems to be the most object oriented way and fitting nicely with existing framework also..Quote:
Originally Posted by Siddhartha
I tried using the Visual C++ 6.0 wizard to add this class to my project. I used the Generic C++ class. When I do this, does the code for the deriving of the CCommandLineInfo go into the CCustomCommandLineInfo.h?
What am I doing wrong? I am trying to create a class that reads the command line argument and based on the argument, runs in either an Operator mode (basic dialog gui asking for information and executing off of that information), Engineering mode (all dialogs and menus are shown and useable), or Server mode (no guis or dialog boxes).
Here is my CCommandLineParse.h code:
I am getting errors such as:Code://--------------
#pragma once
#include "afxwin.h"
class CCommandLineParse : public CCommandLineInfo
{
// Constructor/Destructor
public:
CCommandLineParse(void);
~CCommandLineParse(void);
BOOL IsEngineering(){return m_bEngineering;};
BOOL IsOperator(){return m_bOperator;};
BOOL IsServer() { return m_bServer;};
private:
BOOL m_bEngineering;
BOOL m_bOperator;
BOOL m_bServer;
};
//-------------------
Here is my CCommandLineParse.cpp code:
//-------------------------------------
#include "stdafx.h"
#include ".\commandlineparse.h"
CCommandLineParse::CCommandLineParse()
{
CCommandLineParse()
{
m_bEngineering = m_bOperator = m_bServer = FALSE;
}
//jhs 02/16/06 - variables to indicate the param passed
BOOL m_bEngineering ;//for /e
BOOL m_bOperator; //for /o
BOOL m_bServer;//for /s
//jhs 02/16/06 - public methods for checking these
public:
BOOL IsEngineering(){return m_bEngineering;};
BOOL IsOperator(){return m_bOperator;};
BOOL IsServer() { return m_bServer;};
virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
{
if(0 == strcmp(pszParam,"/e"))
{
m_bEngineering = TRUE;
}
else if(0 == strcmp(pszParam,"/o"))
{
m_bOperator = TRUE;
}
else if(0 == strcmp(pszParam,"/s"))
{
m_bServer = TRUE;
}
}
}
CCommandLineParse::~CCommandLineParse(void)
{
}
//--------------------------------------------
Quote:
c:\Working\MapTextConverter\CommandLineParse.cpp(6): error C2143: syntax error : missing ';' before '{'
c:\Working\MapTextConverter\CommandLineParse.cpp(16): error C2143: syntax error : missing ';' before 'public'
c:\Working\MapTextConverter\CommandLineParse.cpp(17): error C2143: syntax error : missing ';' before '{'
c:\Working\MapTextConverter\CommandLineParse.cpp(17): error C2534: 'CCommandLineParse' : constructor cannot return a value
c:\Working\MapTextConverter\CommandLineParse.cpp(17): error C2562: 'CCommandLineParse::CCommandLineParse' : 'void' function returning a value
c:\Working\MapTextConverter\CommandLineParse.cpp(18): error C2601: 'IsOperator' : local function definitions are illegal
c:\Working\MapTextConverter\CommandLineParse.cpp(19): error C2601: 'IsServer' : local function definitions are illegal
c:\Working\MapTextConverter\CommandLineParse.cpp(22): error C2575: 'ParseParam' : only member functions and bases can be virtual
c:\Working\MapTextConverter\CommandLineParse.cpp(22): error C2601: 'ParseParam' : local function definitions are illegal
Why are you writing functions inside functions ?
Please bring them out..
- in your commandlineparse.h file class declaration, you do not have ParseParam..
- in the .cpp file, write all the functions in declared in .h file seperately qualified by CCommandLineParse:: ( this means they are all class members )
Code:CCommandLineInfo::CCommandLineInfo()
{
...contents here
}
BOOL CCommandLine::ParseParam(...)
{
...contents here
}
- Please make it a habit to look at the style of the appwizard generated code and follow similarly if you have confusion.
- When you hit a compiler error, make it a point to understand it. It doesn't harm in using the best available tools available to you ( MSDN ) before you post in codeguru. Place caret on the compiler errror, for e.g. C2601 and hit F1. This will bring up the help for that error.
Visual Studio IDE, MSDN etc. are your friends. Get to know them and you will have fun.- Please use code tags while posting code snippets. See my signature on how to use them.
Hi ...
Just a follow-up. I have implemented this code and it is working good with a couple small modifications. Below is the full implementatio into a CWinApp derived application. I will instanciate the class in the InitInstance() method and then parse the command line.
Thanks,
Chris
IMPEACH BUSH !
Code://///////////////////////////////////////////////////////////////////////////////////
// CustomCommandLineInfo.h : header file
/////////////////////////////////////////////////////////////////////////////////////
#if !defined(CUSTOM_COMMAND_LINE_INFO_)
#define CUSTOM_COMMAND_LINE_INFO_
class CCustomCommandLineInfo : public CCommandLineInfo
{
// Construction
public:
CCustomCommandLineInfo();
virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast);
//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;};
};
#endif // !defined(CUSTOM_COMMAND_LINE_INFO_)
/////////////////////////////////////////////////////////////////////////////////////
// CustomCommandLineInfo.cpp Implementation
/////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CustomCommandLineInfo.h"
CCustomCommandLineInfo::CCustomCommandLineInfo()
{
m_bExport = m_bOpen = m_bWhatever = FALSE;
}
void CCustomCommandLineInfo::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;
}
}
/////////////////////////////////////////////////////////////////////////////////////
// CTestMmcSnapInRunApp initialization
/////////////////////////////////////////////////////////////////////////////////////
#include "CustomCommandLineInfo.h"
BOOL CTestMmcSnapInRunApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CCustomCommandLineInfo oInfo;
ParseCommandLine(oInfo);
if(oInfo.IsExport())
{
Beep(1000, 100);
}
else if(oInfo.IsWhatever())
{
Beep(1000, 100);
}
else if(oInfo.IsOpen())
{
Beep(1000, 100);
}
else
{
// Call this method to load the mmc and exit this app.
LoadMmcApplication();
}
CTestMmcSnapInRunDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}