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