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