Int afxapi afxwinmain (hinstance, hinstance hprevinstance,
Lptstr lpcmdline, int ncmdshow)
{
Assert (hprevinstance = NULL );
Int nreturncode =-1;
Cwinthread * pthread = afxgetthread ();
Cwinapp * PAPP = afxgetapp ();
// Afx internal Initialization
If (! Afxwininit (hinstance, hprevinstance, lpcmdline, ncmdshow ))
Goto initfailure;
// App global initializations (rare)
If (PAPP! = NULL &&! PAPP-> initapplication ())
Goto initfailure;
// Perform specific initializations
If (! Pthread-> initinstance ())
{
If (pthread-> m_pmainwnd! = NULL)
{
Trace0 ("Warning: destroying non-null m_pmainwnd \ n ");
Pthread-> m_pmainwnd-> destroywindow ();
}
Nreturncode = pthread-> exitinstance ();
Goto initfailure;
}
Nreturncode = pthread-> Run ();
Initfailure:
# Ifdef _ debug
// Check for missing afxlocktempmap CILS
If (afxgetmodulethreadstate ()-> m_ntempmaplock! = 0)
{
Trace1 ("Warning: temp map lock count non-zero (% LD). \ n ",
Afxgetmodulethreadstate ()-> m_ntempmaplock );
}
Afxlocktempmaps ();
Afxunlocktempmaps (-1 );
# Endif
Afxwinterm ();
Return nreturncode;
}
//////////////////////////////////////// /////////////////////////////////////
Cwinthread * afxapi afxgetthread ()
{
// Check for current thread in module thread state
Afx_module_thread_state * pstate = afxgetmodulethreadstate ();
Cwinthread * pthread = pstate-> m_pcurrentwinthread;
// If no cwinthread for the module, then use the global app
If (pthread = NULL)
Pthread = afxgetapp ();
Return pthread;
}
Afx_module_thread_state * afxapi afxgetmodulethreadstate ()
{
Return afxgetmodulestate ()-> m_thread.getdata ();
}
Afx_module_state * afxapi afxgetmodulestate ()
{
_ Afx_thread_state * pstate = _ afxthreadstate;
Afx_module_state * presult;
If (pstate-> m_pmodulestate! = NULL)
{
// Thread state's module State serves as override
Presult = pstate-> m_pmodulestate;
}
Else
{
// Otherwise, use global app state
Presult = _ afxbasemodulestate. getdata ();
}
Assert (presult! = NULL );
Return presult;
}
Template <class type>
Class cprocesslocal: Public cprocesslocalobject
{
// Attributes
Public:
Afx_inline type * getdata ()
{
Type * pdata = (type *) cprocesslocalobject: getdata (& Createobject );
Assert (pdata! = NULL );
Return pdata;
}
Afx_inline type * getdatana ()
{Return (type *) m_pobject ;}
Afx_inline operator type *()
{Return getdata ();}
Afx_inline type * operator-> ()
{Return getdata ();}
// Implementation
Public:
Static cnotrackobject * afxapi Createobject ()
{Return new type ;}
};
Cnotrackobject * cprocesslocalobject: getdata (
Cnotrackobject * (afxapi * pfncreateobject )())
{
If (m_pobject = NULL)
{
Afxlockglobals (crit_processlocal );
Try
{
If (m_pobject = NULL)
M_pobject = (* pfncreateobject )();
}
Catch_all (E)
{
Afxunlockglobals (crit_processlocal );
Throw_last ();
}
End_catch_all
Afxunlockglobals (crit_processlocal );
}
Return m_pobject;
}
_ Afxwin_inline cwinapp * afxapi afxgetapp ()
{Return afxcurrentwinapp ;}
Bool afxapi afxwininit (hinstance, hinstance hprevinstance,
Lptstr lpcmdline, int ncmdshow)
{
Assert (hprevinstance = NULL );
// Handle critical errors and avoid windows message boxes
Seterrormode (0) |
Sem_failcriticalerrors | sem_noopenfileerrorbox );
// Set resource handles
Afx_module_state * pmodulestate = afxgetmodulestate ();
Pmodulestate-> m_hcurrentinstancehandle = hinstance;
Pmodulestate-> m_hcurrentresourcehandle = hinstance;
// Fill in the initial state for the Application
Cwinapp * PAPP = afxgetapp ();
If (PAPP! = NULL)
{
// Windows specific initialization (not done if no cwinapp)
PAPP-> m_hinstance = hinstance;
PAPP-> m_hprevinstance = hprevinstance;
PAPP-> m_lpcmdline = lpcmdline;
PAPP-> m_ncmdshow = ncmdshow;
PAPP-> setcurrenthandles ();
}
// Initialize thread specific data (for main thread)
If (! Afxcontextisdll)
Afxinitthread ();
Return true;
}
Lresult callback _ afxmsgfilterhook (INT code, wparam, lparam );
Void afxapi afxinitthread ()
{
If (! Afxcontextisdll)
{
// Set message filter proc
_ Afx_thread_state * pthreadstate = afxgetthreadstate ();
Assert (pthreadstate-> m_hhookoldmsgfilter = NULL );
Pthreadstate-> m_hhookoldmsgfilter =: setwindowshookex (wh_msgfilter,
_ Afxmsgfilterhook, null,: getcurrentthreadid ());
# Ifndef _ afx_no_ctl3d_support
// Intialize ctl3d for this thread
_ Afx_ctl3d_state * pctl3dstate = _ afxctl3dstate;
If (pctl3dstate-> m_pfnautosubclass! = NULL)
(* Pctl3dstate-> m_pfnautosubclass) (AfxGetInstanceHandle ());
// Allocate Thread Local _ afx_ctl3d_thread just for automatic termination
_ Afx_ctl3d_thread * ptemp = _ afxctl3dthread;
Ptemp; // avoid unused warning
# Endif
}
}
Bool cwinapp: initapplication ()
{
If (cdocmanager: pstaticdocmanager! = NULL)
{
If (m_pdocmanager = NULL)
M_pdocmanager = cdocmanager: pstaticdocmanager;
Cdocmanager: pstaticdocmanager = NULL;
}
If (m_pdocmanager! = NULL)
M_pdocmanager-> adddoctemplate (null );
Else
Cdocmanager: bstaticinit = false;
Return true;
}
Void afxapi afxwinterm (void)
{
// Unregister window classes
Afx_module_state * pmodulestate = afxgetmodulestate ();
Afxlockglobals (crit_regclasslist );
Lptstr lpsz = pmodulestate-> m_szunregisterlist;
While (* lpsz! = 0)
{
Lptstr lpszend = _ tcschr (lpsz, '\ n ');
Assert (lpszend! = NULL );
* Lpszend = 0;
Unregisterclass (lpsz, AfxGetInstanceHandle ());
Lpsz = lpszend + 1;
}
Pmodulestate-> m_szunregisterlist [0] = 0;
Afxunlockglobals (crit_regclasslist );
// Cleanup Ole if required
Cwinthread * pthread = afxgetapp ();
If (pthread! = NULL & pthread-> m_lpfnoletermorfreelib! = NULL)
(* Pthread-> m_lpfnoletermorfreelib) (true, false );
// Cleanup Thread Local tooltip window
_ Afx_thread_state * pthreadstate = afxgetthreadstate ();
If (pthreadstate-> m_ptooltip! = NULL)
{
If (pthreadstate-> m_ptooltip-> destroytooltipctrl ())
Pthreadstate-> m_ptooltip = NULL;
}
If (! Afxcontextisdll)
{
// Unhook windows hooks
If (pthreadstate-> m_hhookoldmsgfilter! = NULL)
{
: Unhookwindowshookex (pthreadstate-> m_hhookoldmsgfilter );
Pthreadstate-> m_hhookoldmsgfilter = NULL;
}
If (pthreadstate-> m_hhookoldcbtfilter! = NULL)
{
: Unhookwindowshookex (pthreadstate-> m_hhookoldcbtfilter );
Pthreadstate-> m_hhookoldcbtfilter = NULL;
}
}
}