home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / appinit.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  162 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 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 AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23.  
  24. BOOL AFXAPI AfxWinInit(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  25.     LPTSTR lpCmdLine, int nCmdShow)
  26. {
  27.     ASSERT(hPrevInstance == NULL);
  28.  
  29.     // handle critical errors and avoid Windows message boxes
  30.     SetErrorMode(SetErrorMode(0) |
  31.         SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);
  32.  
  33.     // set resource handles
  34.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  35.     pModuleState->m_hCurrentInstanceHandle = hInstance;
  36.     pModuleState->m_hCurrentResourceHandle = hInstance;
  37.  
  38.     // fill in the initial state for the application
  39.     CWinApp* pApp = AfxGetApp();
  40.     if (pApp != NULL)
  41.     {
  42.         // Windows specific initialization (not done if no CWinApp)
  43.         pApp->m_hInstance = hInstance;
  44.         pApp->m_hPrevInstance = hPrevInstance;
  45.         pApp->m_lpCmdLine = lpCmdLine;
  46.         pApp->m_nCmdShow = nCmdShow;
  47.         pApp->SetCurrentHandles();
  48.     }
  49.  
  50.     // initialize thread specific data (for main thread)
  51.     if (!afxContextIsDLL)
  52.         AfxInitThread();
  53.  
  54.     return TRUE;
  55. }
  56.  
  57. ///////////////////////////////////////////////////////////////////////////
  58. // CWinApp Initialization
  59.  
  60. void CWinApp::SetCurrentHandles()
  61. {
  62.     ASSERT(this == afxCurrentWinApp);
  63.     ASSERT(afxCurrentAppName == NULL);
  64.  
  65.     AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
  66.     pModuleState->m_hCurrentInstanceHandle = m_hInstance;
  67.     pModuleState->m_hCurrentResourceHandle = m_hInstance;
  68.  
  69.     // Note: there are a number of _tcsdup (aka strdup) calls that are
  70.     // made here for the exe path, help file path, etc.  In previous
  71.     // versions of MFC, this memory was never freed.  In this and future
  72.     // versions this memory is automatically freed during CWinApp's
  73.     // destructor.  If you are freeing the memory yourself, you should
  74.     // either remove the code or set the pointers to NULL after freeing
  75.     // the memory.
  76.  
  77.     // get path of executable
  78.     TCHAR szBuff[_MAX_PATH];
  79.     VERIFY(::GetModuleFileName(m_hInstance, szBuff, _MAX_PATH));
  80.  
  81.     LPTSTR lpszExt = _tcsrchr(szBuff, '.');
  82.     ASSERT(lpszExt != NULL);
  83.     ASSERT(*lpszExt == '.');
  84.     *lpszExt = 0;       // no suffix
  85.  
  86.     TCHAR szExeName[_MAX_PATH];
  87.     TCHAR szTitle[256];
  88.     // get the exe title from the full path name [no extension]
  89.     VERIFY(AfxGetFileName(szBuff, szExeName, _MAX_PATH) == 0);
  90.     if (m_pszExeName == NULL)
  91.     {
  92.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  93.         m_pszExeName = _tcsdup(szExeName); // save non-localized name
  94.         AfxEnableMemoryTracking(bEnable);
  95.     }
  96.  
  97.     // m_pszAppName is the name used to present to the user
  98.     if (m_pszAppName == NULL)
  99.     {
  100.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  101.         if (AfxLoadString(AFX_IDS_APP_TITLE, szTitle) != 0)
  102.             m_pszAppName = _tcsdup(szTitle);    // human readable title
  103.         else
  104.             m_pszAppName = _tcsdup(m_pszExeName);   // same as EXE
  105.         AfxEnableMemoryTracking(bEnable);
  106.     }
  107.  
  108.     pModuleState->m_lpszCurrentAppName = m_pszAppName;
  109.     ASSERT(afxCurrentAppName != NULL);
  110.  
  111.     // get path of .HLP file
  112.     if (m_pszHelpFilePath == NULL)
  113.     {
  114.         lstrcpy(lpszExt, _T(".HLP"));
  115.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  116.         m_pszHelpFilePath = _tcsdup(szBuff);
  117.         AfxEnableMemoryTracking(bEnable);
  118.         *lpszExt = '\0';       // back to no suffix
  119.     }
  120.  
  121.     if (m_pszProfileName == NULL)
  122.     {
  123.         lstrcat(szExeName, _T(".INI")); // will be enough room in buffer
  124.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  125.         m_pszProfileName = _tcsdup(szExeName);
  126.         AfxEnableMemoryTracking(bEnable);
  127.     }
  128. }
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CFile implementation helpers
  132.  
  133. #ifdef AfxGetFileName
  134. #undef AfxGetFileName
  135. #endif
  136.  
  137. UINT AFXAPI AfxGetFileName(LPCTSTR lpszPathName, LPTSTR lpszTitle, UINT nMax)
  138. {
  139.     ASSERT(lpszTitle == NULL ||
  140.         AfxIsValidAddress(lpszTitle, _MAX_FNAME));
  141.     ASSERT(AfxIsValidString(lpszPathName));
  142.  
  143.     // always capture the complete file name including extension (if present)
  144.     LPTSTR lpszTemp = (LPTSTR)lpszPathName;
  145.     for (LPCTSTR lpsz = lpszPathName; *lpsz != '\0'; lpsz = _tcsinc(lpsz))
  146.     {
  147.         // remember last directory/drive separator
  148.         if (*lpsz == '\\' || *lpsz == '/' || *lpsz == ':')
  149.             lpszTemp = (LPTSTR)_tcsinc(lpsz);
  150.     }
  151.  
  152.     // lpszTitle can be NULL which just returns the number of bytes
  153.     if (lpszTitle == NULL)
  154.         return lstrlen(lpszTemp)+1;
  155.  
  156.     // otherwise copy it into the buffer provided
  157.     lstrcpyn(lpszTitle, lpszTemp, nMax);
  158.     return 0;
  159. }
  160.  
  161. /////////////////////////////////////////////////////////////////////////////
  162.