appinit.cpp 4.9 KB

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