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

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

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
{
public:
	/* Constructor Sets default values */
	CCommandLineInfo();

	/* 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;

	~CCommandLineInfo();

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

CWinApp::ParseCommandLine

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 );

ProcessShellCommand

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;
	ParseCommandLine(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
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return TRUE;
}

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#