home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / APPINIT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  7.2 KB  |  234 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef _MAC
  14. #include <macname1.h>
  15. #include <GestaltEqu.h>
  16. #include <MixedMode.h>
  17. #include <macname2.h>
  18. #endif
  19.  
  20. #ifdef AFX_INIT_SEG
  21. #pragma code_seg(AFX_INIT_SEG)
  22. #endif
  23.  
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30.  
  31. BOOL AFXAPI AfxWinInit(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  32.     LPTSTR lpCmdLine, int nCmdShow)
  33. {
  34.     ASSERT(hPrevInstance == NULL);
  35.  
  36.     // handle critical errors and avoid Windows message boxes
  37.     SetErrorMode(SetErrorMode(0) |
  38.         SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
  39.  
  40.     // set resource handles
  41.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  42.     pModuleState->m_hCurrentInstanceHandle = hInstance;
  43.     pModuleState->m_hCurrentResourceHandle = hInstance;
  44.  
  45.     // fill in the initial state for the application
  46.     CWinApp* pApp = AfxGetApp();
  47.     if (pApp != NULL)
  48.     {
  49.         // Windows specific initialization (not done if no CWinApp)
  50.         pApp->m_hInstance = hInstance;
  51.         pApp->m_hPrevInstance = hPrevInstance;
  52.         pApp->m_lpCmdLine = lpCmdLine;
  53.         pApp->m_nCmdShow = nCmdShow;
  54.         pApp->SetCurrentHandles();
  55.     }
  56.  
  57.     // initialize thread specific data (for main thread)
  58.     if (!afxContextIsDLL)
  59.         AfxInitThread();
  60.  
  61.     // Macintosh-specific initialization
  62. #ifdef _MAC
  63.     long lResult;
  64.     if (Gestalt(gestaltAppleEventsAttr, &lResult) == noErr &&
  65.             (lResult & (1 << gestaltAppleEventsPresent)) != 0)
  66.     {
  67.         _afxPfnOpenApp = NewAEEventHandlerProc(_AfxOpenAppHandler);
  68.         if (_afxPfnOpenApp != NULL)
  69.         {
  70.             AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, _afxPfnOpenApp,
  71.                 (long) pApp, false);
  72.         }
  73.         _afxPfnOpenDoc = NewAEEventHandlerProc(_AfxOpenDocHandler);
  74.         if (_afxPfnOpenDoc != NULL)
  75.         {
  76.             AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, _afxPfnOpenDoc,
  77.                 (long) pApp, false);
  78.         }
  79.         _afxPfnPrintDoc = NewAEEventHandlerProc(_AfxPrintDocHandler);
  80.         if (_afxPfnPrintDoc != NULL)
  81.         {
  82.             AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, _afxPfnPrintDoc,
  83.                 (long) pApp, false);
  84.         }
  85.         _afxPfnQuit = NewAEEventHandlerProc(_AfxQuitHandler);
  86.         if (_afxPfnQuit != NULL)
  87.         {
  88.             AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, _afxPfnQuit,
  89.                 (long) pApp, false);
  90.         }
  91.     }
  92. #endif
  93.  
  94.     return TRUE;
  95. }
  96.  
  97. ///////////////////////////////////////////////////////////////////////////
  98. // CWinApp Initialization
  99.  
  100. void CWinApp::SetCurrentHandles()
  101. {
  102.     ASSERT(this == afxCurrentWinApp);
  103.     ASSERT(afxCurrentAppName == NULL);
  104.  
  105.     AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
  106.     pModuleState->m_hCurrentInstanceHandle = m_hInstance;
  107.     pModuleState->m_hCurrentResourceHandle = m_hInstance;
  108.  
  109.     // Note: there are a number of _tcsdup (aka strdup) calls that are
  110.     // made here for the exe path, help file path, etc.  In previous
  111.     // versions of MFC, this memory was never freed.  In this and future
  112.     // versions this memory is automatically freed during CWinApp's
  113.     // destructor.  If you are freeing the memory yourself, you should
  114.     // either remove the code or set the pointers to NULL after freeing
  115.     // the memory.
  116.  
  117.     // get path of executable
  118.     TCHAR szBuff[_MAX_PATH];
  119.     VERIFY(::GetModuleFileName(m_hInstance, szBuff, _MAX_PATH));
  120.  
  121. #ifndef _MAC
  122.     LPTSTR lpszExt = _tcsrchr(szBuff, '.');
  123.     ASSERT(lpszExt != NULL);
  124.     ASSERT(*lpszExt == '.');
  125.     *lpszExt = 0;       // no suffix
  126. #endif
  127.  
  128.     TCHAR szExeName[_MAX_PATH];
  129.     TCHAR szTitle[256];
  130.     // get the exe title from the full path name [no extension]
  131.     VERIFY(AfxGetFileName(szBuff, szExeName, _MAX_PATH) == 0);
  132.     if (m_pszExeName == NULL)
  133.     {
  134.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  135.         m_pszExeName = _tcsdup(szExeName); // save non-localized name
  136.         AfxEnableMemoryTracking(bEnable);
  137.     }
  138.  
  139.     // m_pszAppName is the name used to present to the user
  140.     if (m_pszAppName == NULL)
  141.     {
  142.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  143.         if (AfxLoadString(AFX_IDS_APP_TITLE, szTitle) != 0)
  144.             m_pszAppName = _tcsdup(szTitle);    // human readable title
  145.         else
  146.             m_pszAppName = _tcsdup(m_pszExeName);   // same as EXE
  147.         AfxEnableMemoryTracking(bEnable);
  148.     }
  149.  
  150.     pModuleState->m_lpszCurrentAppName = m_pszAppName;
  151.     ASSERT(afxCurrentAppName != NULL);
  152.  
  153.     // For the Mac, use m_pszAppName instead of the application name when
  154.     // creating the Help and Preferences file names because it's very likely
  155.     // that Mac users will change the application name in the Finder, and that
  156.     // would cause them to lose their preferences and help files. m_pszAppName
  157.     // is somewhat more permanent.
  158.  
  159.     // get path of .HLP file
  160.     if (m_pszHelpFilePath == NULL)
  161.     {
  162. #ifndef _MAC
  163.         lstrcpy(lpszExt, _T(".HLP"));
  164.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  165.         m_pszHelpFilePath = _tcsdup(szBuff);
  166.         AfxEnableMemoryTracking(bEnable);
  167.         *lpszExt = '\0';       // back to no suffix
  168. #else
  169.         OFSTRUCT ofs;
  170.  
  171.         // If this verify fails, probably what's wrong is that the combined
  172.         // lengths of m_pszAppName and " Help" are greater than 31, the max
  173.         // length of a Mac filename. To fix this you should reduce the
  174.         // length of your AFX_IDS_APP_TITLE string.
  175.  
  176.         lstrcpy(szBuff, m_pszAppName);
  177.         lstrcat(szBuff, _T(" Help"));
  178.         VERIFY(OpenFile(szBuff, &ofs, OF_PARSE) != HFILE_ERROR);
  179.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  180.         m_pszHelpFilePath = _tcsdup(ofs.szPathName);
  181.         AfxEnableMemoryTracking(bEnable);
  182. #endif
  183.     }
  184.  
  185.     if (m_pszProfileName == NULL)
  186.     {
  187. #ifndef _MAC
  188.         lstrcat(szExeName, _T(".INI")); // will be enough room in buffer
  189. #else
  190.         // Just a file name, no path - Profile APIs will automatically place
  191.         // the prefs file in the Preferences folder in the System Folder.
  192.         lstrcpy(szExeName, m_pszAppName);
  193.         lstrcat(szExeName, _T(" Preferences"));
  194. #endif
  195.  
  196.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  197.         m_pszProfileName = _tcsdup(szExeName);
  198.         AfxEnableMemoryTracking(bEnable);
  199.     }
  200. }
  201.  
  202. /////////////////////////////////////////////////////////////////////////////
  203. // CFile implementation helpers
  204.  
  205. #ifdef AfxGetFileName
  206. #undef AfxGetFileName
  207. #endif
  208.  
  209. UINT AFXAPI AfxGetFileName(LPCTSTR lpszPathName, LPTSTR lpszTitle, UINT nMax)
  210. {
  211.     ASSERT(lpszTitle == NULL ||
  212.         AfxIsValidAddress(lpszTitle, _MAX_FNAME));
  213.     ASSERT(AfxIsValidString(lpszPathName, FALSE));
  214.  
  215.     // always capture the complete file name including extension (if present)
  216.     LPTSTR lpszTemp = (LPTSTR)lpszPathName;
  217.     for (LPCTSTR lpsz = lpszPathName; *lpsz != '\0'; lpsz = _tcsinc(lpsz))
  218.     {
  219.         // remember last directory/drive separator
  220.         if (*lpsz == '\\' || *lpsz == '/' || *lpsz == ':')
  221.             lpszTemp = (LPTSTR)_tcsinc(lpsz);
  222.     }
  223.  
  224.     // lpszTitle can be NULL which just returns the number of bytes
  225.     if (lpszTitle == NULL)
  226.         return lstrlen(lpszTemp)+1;
  227.  
  228.     // otherwise copy it into the buffer provided
  229.     lstrcpyn(lpszTitle, lpszTemp, nMax);
  230.     return 0;
  231. }
  232.  
  233. /////////////////////////////////////////////////////////////////////////////
  234.