appui2.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // DDE and ShellExecute support
  20. BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
  21. {
  22. BOOL bResult = TRUE;
  23. switch (rCmdInfo.m_nShellCommand)
  24. {
  25. case CCommandLineInfo::FileNew:
  26. if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
  27. OnFileNew();
  28. if (m_pMainWnd == NULL)
  29. bResult = FALSE;
  30. break;
  31. // If we've been asked to open a file, call OpenDocumentFile()
  32. case CCommandLineInfo::FileOpen:
  33. if (!OpenDocumentFile(rCmdInfo.m_strFileName))
  34. bResult = FALSE;
  35. break;
  36. // If the user wanted to print, hide our main window and
  37. // fire a message to ourselves to start the printing
  38. case CCommandLineInfo::FilePrintTo:
  39. case CCommandLineInfo::FilePrint:
  40. m_nCmdShow = SW_HIDE;
  41. ASSERT(m_pCmdInfo == NULL);
  42. OpenDocumentFile(rCmdInfo.m_strFileName);
  43. m_pCmdInfo = &rCmdInfo;
  44. m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);
  45. m_pCmdInfo = NULL;
  46. bResult = FALSE;
  47. break;
  48. // If we're doing DDE, hide ourselves
  49. case CCommandLineInfo::FileDDE:
  50. m_pCmdInfo = (CCommandLineInfo*)m_nCmdShow;
  51. m_nCmdShow = SW_HIDE;
  52. break;
  53. // If we've been asked to unregister, unregister and then terminate
  54. case CCommandLineInfo::AppUnregister:
  55. {
  56. UnregisterShellFileTypes();
  57. BOOL bUnregistered = Unregister();
  58. // if you specify /EMBEDDED, we won't make an success/failure box
  59. // this use of /EMBEDDED is not related to OLE
  60. if (!rCmdInfo.m_bRunEmbedded)
  61. {
  62. if (bUnregistered)
  63. AfxMessageBox(AFX_IDP_UNREG_DONE);
  64. else
  65. AfxMessageBox(AFX_IDP_UNREG_FAILURE);
  66. }
  67. bResult = FALSE; // that's all we do
  68. // If nobody is using it already, we can use it.
  69. // We'll flag that we're unregistering and not save our state
  70. // on the way out. This new object gets deleted by the
  71. // app object destructor.
  72. if (m_pCmdInfo == NULL)
  73. {
  74. m_pCmdInfo = new CCommandLineInfo;
  75. m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
  76. }
  77. }
  78. break;
  79. }
  80. return bResult;
  81. }
  82. BOOL CWinApp::Unregister()
  83. {
  84. HKEY hKey = 0;
  85. TCHAR szBuf[MAX_PATH+1];
  86. LONG cSize;
  87. BOOL bRet = TRUE;
  88. POSITION pos = GetFirstDocTemplatePosition();
  89. while (pos != NULL)
  90. {
  91. CDocTemplate* pTempl = GetNextDocTemplate(pos);
  92. if (pTempl != NULL)
  93. pTempl->OnCmdMsg(0, CN_OLE_UNREGISTER, NULL, NULL);
  94. }
  95. // Remove profile information -- the registry entries exist if
  96. // SetRegistryKey() was used.
  97. if (m_pszRegistryKey)
  98. {
  99. ASSERT(m_pszProfileName);
  100. CString strKey = _T("Software\\");
  101. strKey += m_pszRegistryKey;
  102. CString strSubKey = strKey + _T("\\") + m_pszProfileName;
  103. DelRegTree(HKEY_CURRENT_USER, strSubKey);
  104. // If registry key is empty then remove it
  105. DWORD dwResult;
  106. if ((dwResult = ::RegOpenKey(HKEY_CURRENT_USER, strKey, &hKey)) ==
  107. ERROR_SUCCESS)
  108. {
  109. if (::RegEnumKey(hKey, 0, szBuf, _MAX_PATH) == ERROR_NO_MORE_ITEMS)
  110. DelRegTree(HKEY_CURRENT_USER, strKey);
  111. ::RegCloseKey(hKey);
  112. }
  113. if (RegQueryValue(HKEY_CURRENT_USER, strSubKey, szBuf, &cSize) == ERROR_SUCCESS)
  114. bRet = TRUE;
  115. }
  116. return bRet;
  117. }
  118. // Under Win32, a reg key may not be deleted unless it is empty.
  119. // Thus, to delete a tree, one must recursively enumerate and
  120. // delete all of the sub-keys.
  121. LONG CWinApp::DelRegTree(HKEY hParentKey, const CString& strKeyName)
  122. {
  123. return AfxDelRegTreeHelper(hParentKey, strKeyName);
  124. }
  125. LONG AFXAPI AfxDelRegTreeHelper(HKEY hParentKey, const CString& strKeyName)
  126. {
  127. TCHAR szSubKeyName[256];
  128. HKEY hCurrentKey;
  129. DWORD dwResult;
  130. if ((dwResult = RegOpenKey(hParentKey, strKeyName, &hCurrentKey)) ==
  131. ERROR_SUCCESS)
  132. {
  133. // Remove all subkeys of the key to delete
  134. while ((dwResult = RegEnumKey(hCurrentKey, 0, szSubKeyName, 255)) ==
  135. ERROR_SUCCESS)
  136. {
  137. if ((dwResult = AfxDelRegTreeHelper(hCurrentKey, szSubKeyName)) != ERROR_SUCCESS)
  138. break;
  139. }
  140. // If all went well, we should now be able to delete the requested key
  141. if ((dwResult == ERROR_NO_MORE_ITEMS) || (dwResult == ERROR_BADKEY))
  142. {
  143. dwResult = RegDeleteKey(hParentKey, strKeyName);
  144. }
  145. }
  146. RegCloseKey(hCurrentKey);
  147. return dwResult;
  148. }
  149. void CWinApp::EnableShellOpen()
  150. {
  151. ASSERT(m_atomApp == NULL && m_atomSystemTopic == NULL); // do once
  152. m_atomApp = ::GlobalAddAtom(m_pszExeName);
  153. m_atomSystemTopic = ::GlobalAddAtom(_T("system"));
  154. }
  155. void CWinApp::RegisterShellFileTypes(BOOL bCompat)
  156. {
  157. ASSERT(m_pDocManager != NULL);
  158. m_pDocManager->RegisterShellFileTypes(bCompat);
  159. }
  160. void CWinApp::RegisterShellFileTypesCompat()
  161. {
  162. ASSERT(m_pDocManager != NULL);
  163. m_pDocManager->RegisterShellFileTypes(TRUE);
  164. }
  165. void CWinApp::UnregisterShellFileTypes()
  166. {
  167. ASSERT(m_pDocManager != NULL);
  168. m_pDocManager->UnregisterShellFileTypes();
  169. }
  170. #ifdef AFX_CORE2_SEG
  171. #pragma code_seg(AFX_CORE2_SEG)
  172. #endif
  173. int CWinApp::GetOpenDocumentCount()
  174. {
  175. ASSERT(m_pDocManager != NULL);
  176. return m_pDocManager->GetOpenDocumentCount();
  177. }
  178. /////////////////////////////////////////////////////////////////////////////
  179. // Doc template support
  180. #ifdef AFX_CORE3_SEG
  181. #pragma code_seg(AFX_CORE3_SEG)
  182. #endif
  183. void CWinApp::AddDocTemplate(CDocTemplate* pTemplate)
  184. {
  185. if (m_pDocManager == NULL)
  186. m_pDocManager = new CDocManager;
  187. m_pDocManager->AddDocTemplate(pTemplate);
  188. }
  189. POSITION CWinApp::GetFirstDocTemplatePosition() const
  190. {
  191. if (m_pDocManager == NULL)
  192. return NULL;
  193. return m_pDocManager->GetFirstDocTemplatePosition();
  194. }
  195. CDocTemplate* CWinApp::GetNextDocTemplate(POSITION& rPosition) const
  196. {
  197. ASSERT(m_pDocManager != NULL);
  198. return m_pDocManager->GetNextDocTemplate(rPosition);
  199. }
  200. /////////////////////////////////////////////////////////////////////////////