home *** CD-ROM | disk | FTP | other *** search
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1992 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Microsoft Foundation Classes Reference and Microsoft
- // QuickHelp and/or WinHelp documentation provided with the library.
- // See these sources for detailed information regarding the
- // Microsoft Foundation Classes product.
-
-
- #include "stdafx.h"
- #include "winhand_.h" // for creation of CHandleMap
-
- #include <stdarg.h>
-
- #ifdef AFX_INIT_SEG
- #pragma code_seg(AFX_INIT_SEG)
- #endif
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- #ifndef _AFXDLL
- #error file must be compiled with _AFXDLL
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // _AFXDLL support
-
- static AFX_EXTENSION_MODULE coreDLL = { NULL, NULL };
-
- /////////////////////////////////////////////////////////////////////////////
- // Per-app initialization
-
- BOOL WINAPI _AfxAppDataInit()
- {
- ASSERT(coreDLL.hModule != NULL);
-
- ASSERT(sizeof(AFX_APPDATA) <= 128); // hard coded limit in 'afxdll.asm'
- #ifdef _DEBUG
- ASSERT(sizeof(AFX_APPDEBUG) <= 32); // hard coded limit in 'afxdll.asm'
- #endif
-
- // called for each Application's instance run with the MFC200.DLL
- if (_AfxGetAppData()->cbSize < sizeof(AFX_APPDATA) ||
- (int)_AfxGetAppData()->cbSize < 0)
- {
- #ifdef _DEBUG
- OutputDebugString("FATAL ERROR: Illegal size for AFX_APPDATA\n\r");
- OutputDebugString(" AFX_NULL segment must start at DGROUP:0010\n\r");
- ASSERT(FALSE);
- #endif
- return FALSE;
- }
- if (_AfxGetAppData()->wVersion != 0x200)
- {
- #ifdef _DEBUG
- OutputDebugString("FATAL ERROR: Illegal version for AFX_APPDATA\n\r");
- #endif
- ASSERT(FALSE);
- return FALSE;
- }
-
- // make sure we have a routines for memory allocator, VBX and abort
- ASSERT(_AfxGetAppData()->lpfnAppAlloc != NULL);
- ASSERT(_AfxGetAppData()->lpfnAppFree != NULL);
- ASSERT(_AfxGetAppData()->lpfnVBApiEntry != NULL);
- ASSERT(_AfxGetAppData()->lpfnAppAbort != NULL);
-
- // other sanity asserts : AFX_APPDATA must be zero initialized !
- ASSERT(afxExceptionContext.m_pCurrent == NULL);
- ASSERT(afxExceptionContext.m_pLinkTop == NULL);
-
- #ifdef _DEBUG
- // check debug entries
- ASSERT(_AfxGetAppDebug() != NULL);
- ASSERT(_AfxGetAppDebug()->lpfnTraceV != NULL);
- ASSERT(_AfxGetAppDebug()->lpszAllocFileName == NULL);
- ASSERT(!_AfxGetAppDebug()->bMemoryTracking); // tracking off
- // internal App data memory is not tracked
- #endif //_DEBUG
-
- // allocate handle maps
- ASSERT(_AfxGetAppData()->appMapHGDIOBJ == NULL);
- _AfxGetAppData()->appMapHGDIOBJ = new CHandleMap(RUNTIME_CLASS(CGdiObject));
- ASSERT(_AfxGetAppData()->appMapHDC == NULL);
- _AfxGetAppData()->appMapHDC = new CHandleMap(RUNTIME_CLASS(CDC), 2);
- ASSERT(_AfxGetAppData()->appMapHMENU == NULL);
- _AfxGetAppData()->appMapHMENU = new CHandleMap(RUNTIME_CLASS(CMenu));
- ASSERT(_AfxGetAppData()->appMapHWND == NULL);
- _AfxGetAppData()->appMapHWND = new CHandleMap(RUNTIME_CLASS(CWnd));
-
- // allocate structures for sub-system state
- ASSERT(_AfxGetAppData()->appVBState == NULL);
- _AfxGetAppData()->appVBState = new AFX_VBSTATE;
- ASSERT(_AfxGetAppData()->appLastFRState == NULL);
- _AfxGetAppData()->appLastFRState = new AFX_FRSTATE;
- ASSERT(_AfxGetAppData()->appOCState == NULL);
- _AfxGetAppData()->appOCState = new AFX_OCSTATE;
- _AfxGetAppData()->appOCState->cWaitForRelease = 0; // extra init
-
- // any CRuntimeClasses registered between last DLL loaded and now must
- // be ours
- ASSERT(_AfxGetAppData()->pFirstAppClass == NULL);
- _AfxGetAppData()->pFirstAppClass = CRuntimeClass::pFirstClass;
- CRuntimeClass::pFirstClass = NULL; // clear for next DLL
-
- // there is a CDynLinkLibrary object for each DLL available for each app.
- // hook in MFCDLL first (always last on the resource handler chain)
- ASSERT(_AfxGetAppData()->pFirstDLL == NULL); // must be first added
- new CDynLinkLibrary(coreDLL); // will add to list
- ASSERT(_AfxGetAppData()->pFirstDLL != NULL);
-
- // other initialization
- _AfxGetAppData()->appDlgTextClr = (COLORREF)-1; // use normal text
-
- #ifdef _DEBUG
- // turn on memory tracking from now on
- _AfxGetAppDebug()->bMemoryTracking = TRUE;
- #endif
- return TRUE;
- }
-
- void WINAPI _AfxAppDataTerm()
- {
- // free the DLL info blocks
- CDynLinkLibrary* pDLL;
- CDynLinkLibrary* pDLLNext;
- for (pDLL = _AfxGetAppData()->pFirstDLL; pDLL != NULL; pDLL = pDLLNext)
- {
- pDLLNext = pDLL->m_pNextDLL; // save next pointer before delete
- delete pDLL;
- }
-
- // free any MFC library implementation state
- delete _AfxGetAppData()->appVBState;
- delete _AfxGetAppData()->appLastFRState;
- delete _AfxGetAppData()->appOCState;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CDynLinkLibrary class
-
- IMPLEMENT_DYNAMIC(CDynLinkLibrary, CCmdTarget)
-
- // Constructor - will wire into the current application's list
- CDynLinkLibrary::CDynLinkLibrary(AFX_EXTENSION_MODULE& state)
- {
- ASSERT(state.hModule != NULL);
- m_hResource = m_hModule = state.hModule;
- m_pFirstSharedClass = state.pFirstSharedClass;
-
- // Insert at the head of the list (extensions will go in front of core DLL)
- m_pNextDLL = _AfxGetAppData()->pFirstDLL;
- _AfxGetAppData()->pFirstDLL = this;
- }
-
- CDynLinkLibrary::~CDynLinkLibrary()
- {
- }
-
- #ifdef _DEBUG
- void CDynLinkLibrary::AssertValid() const
- {
- ASSERT(m_hModule != NULL);
- }
-
- void CDynLinkLibrary::Dump(CDumpContext& dc) const
- {
- CCmdTarget::Dump(dc);
-
- AFX_DUMP1(dc, "\nm_hModule = ", (UINT)m_hModule);
- AFX_DUMP1(dc, "\nm_hResource = ", (UINT)m_hResource);
- char szName[64];
- GetModuleFileName(m_hModule, szName, sizeof(szName));
- AFX_DUMP1(dc, "\nmodule name = ", szName);
- }
- #endif //_DEBUG
-
- // This is called in an extension DLL's LibMain
- // It makes a copy of the DLL's HMODULE as well as all the CRuntimeClass
- // objects that have been registered as part of the normal C++ static
- // constructors called before LibMain.
- void AFXAPI AfxInitExtensionModule(AFX_EXTENSION_MODULE& state, HMODULE hModule)
- {
- ASSERT(hModule != NULL);
- state.hModule = hModule;
- state.pFirstSharedClass = CRuntimeClass::pFirstClass;
- CRuntimeClass::pFirstClass = NULL; // clear for next DLL or EXE
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Resource helpers
-
- HINSTANCE AFXAPI AfxFindResourceHandle(LPCSTR lpszName, LPCSTR lpszType)
- {
- ASSERT(lpszName != NULL);
- ASSERT(lpszType != NULL);
-
- // first check the app
- HINSTANCE hInstApp = AfxGetResourceHandle();
- if (::FindResource(hInstApp, lpszName, lpszType) != NULL)
- return hInstApp;
-
- // check for DLLs in proper order
- CDynLinkLibrary* pDLL;
- for (pDLL = _AfxGetAppData()->pFirstDLL; pDLL != NULL;
- pDLL = pDLL->m_pNextDLL)
- {
- if (pDLL->m_hResource != NULL &&
- ::FindResource(pDLL->m_hResource, lpszName, lpszType) != NULL)
- {
- // found it in a DLL
- return pDLL->m_hResource;
- }
- }
-
- // if failed to find resource, return application
- return hInstApp;
- }
-
- // _AfxLoadString must not only check for the appropriate string segment
- // in the resource file, but also that the string is non-zero
- int PASCAL _AfxLoadString(UINT nID, char* pszBuf)
- {
- ASSERT(AfxIsValidAddress(pszBuf, 256)); // must be big enough for 256 bytes
-
- LPCSTR lpszName = MAKEINTRESOURCE((nID>>4)+1);
- HINSTANCE hInst;
- int nLen;
-
- // first check the app
- hInst = AfxGetResourceHandle();
- if (::FindResource(hInst, lpszName, RT_STRING) != NULL &&
- (nLen = ::LoadString(hInst, nID, pszBuf, 255)) != 0)
- {
- // found a non-zero string in app
- return nLen;
- }
-
- // check for DLLs in proper order
- CDynLinkLibrary* pDLL;
- for (pDLL = _AfxGetAppData()->pFirstDLL; pDLL != NULL;
- pDLL = pDLL->m_pNextDLL)
- {
- if ((hInst = pDLL->m_hResource) != NULL &&
- ::FindResource(hInst, lpszName, RT_STRING) != NULL &&
- (nLen = ::LoadString(hInst, nID, pszBuf, 255)) != 0)
- {
- return nLen;
- }
- }
-
- return 0; // did not find it
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- // Library init and WEP
-
- extern "C"
- int CALLBACK LibMain(HINSTANCE hInstance, WORD, WORD, LPSTR)
- {
- #ifdef _DEBUG
- OutputDebugString("MFCDLL Initializing!\n\r");
- #endif //_DEBUG
- // Shared initialization
- AfxInitExtensionModule(coreDLL, hInstance);
-
- return 1; // ok
- }
-
- void CWinApp::_ForceLinkage()
- {
- ASSERT(FALSE); // dummy routine for building DLL
- }
-
- #ifdef AFX_CORE1_SEG
- #pragma code_seg("WEP_TEXT")
- #endif
-
- extern "C" int CALLBACK _WEP(int)
- {
- // subset of AfxWinTerm for when DLL terminates
- ((CWnd&)CWnd::wndTop).m_hWnd = NULL;
- ((CWnd&)CWnd::wndBottom).m_hWnd = NULL;
- ((CWnd&)CWnd::wndTopMost).m_hWnd = NULL;
- ((CWnd&)CWnd::wndNoTopMost).m_hWnd = NULL;
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Helper routines to delegate to app provided implementation
-
- #ifdef AFX_CORE1_SEG
- #pragma code_seg(AFX_CORE1_SEG)
- #endif
-
- #ifdef _DEBUG
-
- extern "C" void CDECL
- AfxTrace(LPCSTR pszFormat, ...)
- {
- va_list args;
- va_start(args, pszFormat);
-
- (_AfxGetAppDebug()->lpfnTraceV)(pszFormat, args);
- }
-
- extern "C" void AFXAPI
- AfxAssertFailedLine(LPCSTR lpszFileName, int nLine)
- {
- (_AfxGetAppDebug()->lpfnAssertFailed)(lpszFileName, nLine);
- }
-
- BOOL AFXAPI AfxEnableMemoryTracking(BOOL bNewTrackingOn)
- {
- BOOL bOldTrackingOn = _AfxGetAppDebug()->bMemoryTracking;
- _AfxGetAppDebug()->bMemoryTracking = bNewTrackingOn;
- return bOldTrackingOn;
- }
-
- #endif //_DEBUG
-
- void AFXAPI AfxAbort()
- {
- #ifdef _DEBUG
- OutputDebugString("DLL's AfxAbort called\n\r");
- OutputDebugString(" - calling app provided AbortProc\n\r");
- #endif //_DEBUG
- (_AfxGetAppData()->lpfnAppAbort)();
- ASSERT(FALSE); // NOT REACHED !!
- }
-
- void AFXAPI AfxTerminate()
- {
- // hookability not provided
- AfxAbort();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Memory allocation is done by app !
-
- void* operator new(size_t nSize)
- {
- #ifdef _DEBUG
- ASSERT(_AfxGetAppData()->lpfnAppAlloc != NULL);
- ASSERT(_AfxGetAppDebug()->lpszAllocFileName == NULL);
- _AfxGetAppDebug()->bAllocObj = FALSE;
- #endif //_DEBUG
- void* p = (_AfxGetAppData()->lpfnAppAlloc)(nSize);
- if (p == NULL)
- AfxThrowMemoryException();
- return p;
- }
-
- void* operator new(size_t nSize, LPCSTR pFileName, int nLine)
- {
- #ifdef _DEBUG
- ASSERT(_AfxGetAppData()->lpfnAppAlloc != NULL);
- _AfxGetAppDebug()->lpszAllocFileName = pFileName;
- _AfxGetAppDebug()->nAllocLine = nLine;
- _AfxGetAppDebug()->bAllocObj = FALSE;
- #endif //_DEBUG
-
- void* p = (_AfxGetAppData()->lpfnAppAlloc)(nSize);
-
- #ifdef _DEBUG
- _AfxGetAppDebug()->lpszAllocFileName = NULL;
- #endif //_DEBUG
-
- if (p == NULL)
- AfxThrowMemoryException();
- return p;
- }
-
- void operator delete(void* pbData)
- {
- if (pbData == NULL)
- return;
- #ifdef _DEBUG
- ASSERT(_AfxGetAppData()->lpfnAppFree != NULL);
- _AfxGetAppDebug()->bAllocObj = FALSE;
- #endif //_DEBUG
- (*_AfxGetAppData()->lpfnAppFree)(pbData);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Additional CObject new/delete operators for memory tracking
-
- #ifdef _DEBUG
- void* CObject::operator new(size_t nSize)
- {
- ASSERT(_AfxGetAppData()->lpfnAppAlloc != NULL);
- ASSERT(_AfxGetAppDebug()->lpszAllocFileName == NULL);
- _AfxGetAppDebug()->bAllocObj = TRUE;
- void* p = (_AfxGetAppData()->lpfnAppAlloc)(nSize);
- if (p == NULL)
- AfxThrowMemoryException();
- return p;
- }
-
- void* CObject::operator new(size_t nSize, LPCSTR pFileName, int nLine)
- {
- ASSERT(_AfxGetAppData()->lpfnAppAlloc != NULL);
- _AfxGetAppDebug()->lpszAllocFileName = pFileName;
- _AfxGetAppDebug()->nAllocLine = nLine;
- _AfxGetAppDebug()->bAllocObj = TRUE;
-
- void* p = (_AfxGetAppData()->lpfnAppAlloc)(nSize);
- _AfxGetAppDebug()->lpszAllocFileName = NULL;
- if (p == NULL)
- AfxThrowMemoryException();
- return p;
- }
-
- void CObject::operator delete(void* pbData)
- {
- if (pbData == NULL)
- return;
- ASSERT(_AfxGetAppData()->lpfnAppFree != NULL);
- _AfxGetAppDebug()->bAllocObj = TRUE;
- (*_AfxGetAppData()->lpfnAppFree)(pbData);
- }
-
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // we must also replace any direct calls to malloc/free
-
- extern "C"
- void __far* __cdecl _fmalloc(size_t nSize)
- {
- #ifdef _DEBUG
- ASSERT(_AfxGetAppData()->lpfnAppAlloc != NULL);
- ASSERT(_AfxGetAppDebug()->lpszAllocFileName == NULL);
- _AfxGetAppDebug()->bAllocObj = FALSE;
- BOOL bOldTracking = _AfxGetAppDebug()->bMemoryTracking;
- _AfxGetAppDebug()->bMemoryTracking = FALSE; // mallocs not tracked
- #endif //_DEBUG
- void* p = (_AfxGetAppData()->lpfnAppAlloc)(nSize);
- #ifdef _DEBUG
- _AfxGetAppDebug()->bMemoryTracking = bOldTracking;
- #endif //_DEBUG
- if (p == NULL)
- AfxThrowMemoryException();
- return p;
- }
-
- extern "C"
- void __cdecl _ffree(void __far* p)
- {
- #ifdef _DEBUG
- ASSERT(_AfxGetAppData()->lpfnAppFree != NULL);
- _AfxGetAppDebug()->bAllocObj = FALSE;
- #endif //_DEBUG
- (*_AfxGetAppData()->lpfnAppFree)(p);
- }
-
- extern "C"
- void __far* __cdecl _frealloc(void __far* pOld, size_t nSize)
- {
- ASSERT(_AfxGetAppData()->lpfnAppReAlloc != NULL);
- return (_AfxGetAppData()->lpfnAppReAlloc)(pOld, nSize);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // and finally replace the _set_new_handler
-
- _PNH AFXAPI _AfxSetNewHandler(_PNH pnh)
- {
- ASSERT(_AfxGetAppData()->lpfnAppSetNewHandler != NULL);
- return (_PNH)(_AfxGetAppData()->lpfnAppSetNewHandler)((FARPROC)pnh);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Also stub out the runtime init 'setenvp' routine to avoid malloc calls
-
- extern "C" void _cdecl _setenvp()
- {
- }
-
- /////////////////////////////////////////////////////////////////////////////
-