EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Winmain Commandline argument

Any Windows 32bit application starts with WinMain(). Command line arguments is passed to WinMain as third argument (lpCmdLine) in the form of string.

int CALLBACK WinMain(
  HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR     lpCmdLine,
  int       nCmdShow

MFC application does not implement WinMain(). WinMain() is however under MFC framework and MFC application programmers can never access lpCmdLine.


GetCommandLine() is the WIN32 API to get the command line string passed to the application. It does not take any input and the return value is a pointer to the command-line string for the current process.

LPTSTR WINAPI GetCommandLine(void);

This works well in Win32 application as well as applications written in MFC or ATL. Simple MFC Applications like a Frame or dialog based applications also can use this same API to access this parameters. MFC DOC VIEW application however has different requirement for command lines. These applications parse command line paramerets and performs verious activities like Activities includes new, open file, print file, installing registry resources to Windows registry or uninstalling the same etc.


CCommandLineInfo class structure is a generic class to hold the state of command line parameters in managed member variables. Please see the inline comments for each member variables.

class CCommandLineInfo : public CObject
	/* Constructor Sets default values */

	/* Utility function to parse one parameter at a time */
	virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast);

	/* Show flash screen */
	BOOL m_bShowSplash;
	/*Running OLE embedded used for COM app*/
	BOOL m_bRunEmbedded;
	/*Running OLE automation in COM DCOM*/
	BOOL m_bRunAutomated;
	/*Shell command flag */
	enum { FileNew, FileOpen, FilePrint, FilePrintTo, FileDDE,
		AppUnregister, FileNothing = -1 } m_nShellCommand;

	/* file path string other than FileNew */
	CString m_strFileName;

	/*Parameters when FilePrintTo command*/
	CString m_strPrinterName;
	CString m_strDriverName;
	CString m_strPortName;


	void ParseParamFlag(const char* pszParam);
	void ParseParamNotFlag(const TCHAR* pszParam);
	void ParseLast(BOOL bLast);


CWinApp member function ParseCommandLine is used to to parse application command line and to populate appripiate members of CCommandLineInfo object which is passed to it. This fuction internally calls ParseParam member function of CCommandLineInfo to parse one parameter at a time.

void ParseCommandLine( CCommandLineInfo& rCmdInfo );


This member function is called by InitInstance to accept the parameters passed from the CCommandLineInfo object identified by rCmdInfo, and perform the indicated action.

BOOL ProcessShellCommand( CCommandLineInfo& rCmdInfo );

Command line MFC code

BOOL CMFCMDIApp::InitInstance()

	m_pMainWnd = pMainFrame;

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;

	// Dispatch commands specified on the command line.  Will return FALSE if
	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;
	// The main window has been initialized, so show and update it

	return TRUE;

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 19:34:56(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.