Click to See Complete Forum and Search --> : How do I get the commandline-arguments?


esesosc
September 6th, 1999, 02:30 AM
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

Oleg Lobach
September 6th, 1999, 03:22 AM
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.

DaBj
October 21st, 1999, 03:27 AM
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

ALM
October 21st, 1999, 08:32 AM
The easiest way is via the __argc and __targv macros.

Cheers!
Alvaro