CWinApp::m_lpCmdLine problem??
hi...
I created a SDI application.
I need to get CmdLine info inside App::InitInstance()
Then I need to take some action inside CMainFrame depending on the CmdLine info.
Is there any method to Access App info in CMainFrame without using a global variable?
Is the following code ok?
BOOL g_bYes = FALSE;
BOOL CMyApp::InitInstance()
{
if (m_lpCmdLine[0] == _T('\0'))
{
//default settings in CMainFrame
}
else if( (m_lpCmdLine[0] == _T('Y')) &&
(m_lpCmdLine[1] == _T('E')) &&
(m_lpCmdLine[2] == _T('S')) )
{
//passing info to CMainFrame
//g_bYes = TRUE;
}
}
CMainFrame::CMainFrame()
{
/*if(g_bYes == TRUE)
MessageBox("YES");*/
}
Thanks
[892 byte] By [
asinro] at [2007-11-17 18:19:31]

# 3 Re: CWinApp::m_lpCmdLine problem??
There are lots of ways to do it. You can always get at your CWinApp derivative with the global function AfxGetApp(). And since the CWinApp::m_lpCmdLine member is public you can access it with:
CWinApp *pApp = ::AfxGetApp();
ASSERT(pApp);
if (pApp->m_lpCmdLine[0] == _T('\0'))
{
// TODO: ...
}
But maybe it would be better to parse your command line once and for all in your InitInstance method and add small methods to your CWinApp derivative so you could do things like:
CYourWinappDerivative *pApp = (CYourWinappDerivative *)::AfxGetApp();
ASSERT(pApp);
if (pApp->IsCmdLineFlag1())
{
// TODO: ...
}
IMHO it produces code which is easier maintainable. The parsing of the command line is only one place and you don't have to access properties of the CWinApp class which might have been better off as private or protected.
Ofcourse you can probably always use GetCommandLine. Even in an MFC app.
# 7 Re: CWinApp::m_lpCmdLine problem??
This is inside your
BOOL CMyApp::InitInstance()
{
....
...
CString szCommandLine = GetCommandLine();
...
...
// To create the main window, this code // creates a new frame window
// object and then sets it as the //application's main window object.
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
// create and load the frame with its //resources
pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);
// The one and only window has been //initialized, so show and update it.
pFrame->ShowWindow(SW_SHOWMAXIMIZED);
pFrame->UpdateWindow();
if(m_bCommandLineData)
{
pFrame->OnCallConnectFromApp();
}
}
I hope this helps you...
# 8 Re: CWinApp::m_lpCmdLine problem??
Instead of a global variable, you can define it as a static member of you App class:
class CMyApp
{
static int g_bYes=FALSE;
...
};
in CMainFrame you can use it directly:
if( CMyApp::g_bYes )
MessageBox("YES");
Elrond at 2007-11-10 8:30:48 >
