Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MFC startup code question

358 views
Skip to first unread message

McGee

unread,
Mar 18, 2003, 3:04:36 PM3/18/03
to
I'm not 100% sure how the MFC startup code works. In particular, I don't see
where CWinApp::CWinApp() sets the global application instance, which is then
used by AfxWinMain(). Here's an excerpt from the constructor for CWinApp:

// initialize CWinThread state
AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
ASSERT(AfxGetThread() == NULL);
pThreadState->m_pCurrentWinThread = this;
ASSERT(AfxGetThread() == this);
m_hThread = ::GetCurrentThread();
m_nThreadID = ::GetCurrentThreadId();

// initialize CWinApp state
ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please
pModuleState->m_pCurrentWinApp = this;
ASSERT(AfxGetApp() == this);

It looks as though somehow p_mModuleState is pointing to a globally
accessible object, which is then read by AfxGetApp().

AfxGetApp() returns afxCurrentWinApp, which is just an alias for
AfxGetModuleState()->m_pCurrentWinApp. CWinApp() assigns to
pModuleState->m_pCurrentWinApp. pModuleState is the result of
_AFX_CMDTARGET_GETSTATE(), which is defined like this:

#ifdef _AFXDLL
#define _AFX_CMDTARGET_GETSTATE() (m_pModuleState)
#else
#define _AFX_CMDTARGET_GETSTATE() (AfxGetModuleState())
#endif

Why does it depend on _AFXDLL, which I assume is set if you're building with
MFC as a DLL? Anyway, it looks like AfxGetModuleState() sets a global or
two, but it's a little confusing.

Can anyone make this more clear?

Thanks,
McGee


Nishant Sivakumar

unread,
Mar 19, 2003, 12:23:47 AM3/19/03
to
Hi

See :- http://www.codeproject.com/cpp/mfcprogflow.asp

Regards,
Nish [VC++ MVP]


"McGee" <cla...@NO-SPAM-TILL-BROOKLYN-trefs.com> wrote in message
news:48A4F4A896C377D4.F37495F6...@lp.airnews.net...

McGee

unread,
Mar 20, 2003, 2:53:40 PM3/20/03
to
Thank you Nishant. No offense, but that link actually contained less
information than my post. In particular, in your article you also mention
this line:

pModuleState->m_pCurrentWinApp = this;

but all that's said about it is "Thus when we create our global CNWinApp
object, it's constructor gets called and the AFX_MODULE_STATE structure is
setup properly." What I wonder is "How?" pModuleState =
_AFX_CMDTARGET_GETSTATE(), which as I said is just a macro:

#ifdef _AFXDLL
#define _AFX_CMDTARGET_GETSTATE() (m_pModuleState)
#else
#define _AFX_CMDTARGET_GETSTATE() (AfxGetModuleState())
#endif

So its value depends on whether or not you're using MFC as a DLL. HERE'S
where I get lost -- how does this all work?

Thanks,
McGee


"Nishant Sivakumar" <ni...@nospam.asianetindia.com> wrote in message
news:uQ9#8ed7CH...@TK2MSFTNGP11.phx.gbl...

0 new messages