home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atltangram / mfctangram / mfctangramconfigdlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  228 lines

  1. // MFCTangramConfigDlg.cpp : implementation file
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "stdafx.h"
  14. #include "MFCTangram.h"
  15. #include "MFCTangramConfigDlg.h"
  16.  
  17. #include <objbase.h>
  18. #include <comcat.h>
  19. #include <initguid.h>
  20. #include <assert.h>
  21. #include "AtlWorldCat.h" //CATID_AtlTangramWorldCategory
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. // Size of a CLSID as a string
  30. const int CLSID_STRING_SIZE = 39 ;
  31.  
  32. // Helper function
  33. BOOL getFriendlyName(const CLSID& clsid, LPCTSTR szFriendlyName, int iLength);
  34.  
  35. ///////////////////////////////////////////////////////////
  36. //
  37. // Internal helper functions
  38. ///////////////////////////////////////////////////////////
  39. //
  40. // Convert a CLSID to a char string.
  41. //
  42. void CLSIDtochar(   const CLSID& clsid,
  43.                     char* szCLSID,
  44.                     int length)
  45. {
  46.     assert(length >= CLSID_STRING_SIZE) ;
  47.     // Get CLSID
  48.     LPOLESTR wszCLSID = NULL ;
  49.     HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ;
  50.     assert(SUCCEEDED(hr)) ;
  51.  
  52.     // Convert from wide characters to non-wide characters.
  53.     wcstombs(szCLSID, wszCLSID, length);
  54.  
  55.     // Free memory.
  56.     CoTaskMemFree(wszCLSID) ;
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMFCTangramConfigDlg dialog
  61.  
  62.  
  63. CMFCTangramConfigDlg::CMFCTangramConfigDlg(CWnd* pParent /*=NULL*/)
  64.     : CDialog(CMFCTangramConfigDlg::IDD, pParent)
  65. {
  66.     //{{AFX_DATA_INIT(CMFCTangramConfigDlg)
  67.     m_bLocalModel = FALSE;
  68.     //}}AFX_DATA_INIT
  69. }
  70.  
  71.  
  72. void CMFCTangramConfigDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74.     CDialog::DoDataExchange(pDX);
  75.     //{{AFX_DATA_MAP(CMFCTangramConfigDlg)
  76.     DDX_Control(pDX, IDC_WORLD_LIST, m_listWorld);
  77.     DDX_Check(pDX, IDC_MODEL_LOCAL_CHECK, m_bLocalModel);
  78.     //}}AFX_DATA_MAP
  79. }
  80.  
  81.  
  82. BEGIN_MESSAGE_MAP(CMFCTangramConfigDlg, CDialog)
  83.     //{{AFX_MSG_MAP(CMFCTangramConfigDlg)
  84.     ON_WM_DESTROY()
  85.     //}}AFX_MSG_MAP
  86. END_MESSAGE_MAP()
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CMFCTangramConfigDlg message handlers
  90.  
  91. BOOL CMFCTangramConfigDlg::OnInitDialog()
  92. {
  93.     CDialog::OnInitDialog();
  94.  
  95.     // --- Fill the World Components list box with components with the world CATID. ---
  96.  
  97.     // Create the standard COM Category Manager
  98.     ICatInformation* pICatInformation = NULL ;
  99.     HRESULT hr = ::CoCreateInstance(    CLSID_StdComponentCategoriesMgr,
  100.                                         NULL, CLSCTX_ALL, IID_ICatInformation,
  101.                                         (void**)&pICatInformation) ;
  102.     if (FAILED(hr))
  103.     {
  104.         ASSERT(hr) ;
  105.         return FALSE;
  106.     }
  107.  
  108.     // Array of Categories
  109.     int cIDs = 1 ;
  110.     CATID IDs[1] ;
  111.     IDs[0] = CATID_AtlTangramWorldCategory ;
  112.  
  113.         // Get the IEnumCLSID interface.
  114.     IEnumCLSID* pIEnumCLSID = NULL ;
  115.     hr = pICatInformation->EnumClassesOfCategories(cIDs, IDs, 0, NULL, &pIEnumCLSID) ;
  116.     ASSERT(SUCCEEDED(hr)) ;
  117.  
  118.     // Get the next CLSID in the list.
  119.     char szFriendlyName[128] ;
  120.     CLSID clsid ;
  121.  
  122.     while ((hr = pIEnumCLSID->Next(1, &clsid, NULL)) == S_OK)
  123.     {
  124.         // We have the clsid
  125.         // Get the friendly name for the clsid.
  126.         if (getFriendlyName(clsid, szFriendlyName, sizeof(szFriendlyName)))
  127.         {
  128.             int index = m_listWorld.AddString(szFriendlyName) ;
  129.  
  130.             // Add the clisd to the listbox.
  131.             CLSID* pclsid = new CLSID ;
  132.             *pclsid = clsid ;
  133.  
  134.             m_listWorld.SetItemDataPtr(index, pclsid) ;
  135.         }
  136.     }
  137.     ASSERT(SUCCEEDED(hr)) ;
  138.  
  139.     if (m_listWorld.GetCount() > 0)
  140.     {
  141.         m_listWorld.SetCurSel(0) ;
  142.     }
  143.  
  144.  
  145.     // CleanUp
  146.     pICatInformation->Release() ;
  147.  
  148.     return TRUE;    // return TRUE unless you set the focus to a control
  149.                     // EXCEPTION: OCX Property Pages should return FALSE
  150. }
  151.  
  152. void CMFCTangramConfigDlg::OnDestroy()
  153. {
  154.     // Clean up the data in the list box.
  155.     int count = m_listWorld.GetCount() ;
  156.  
  157.     for (int i = 0 ; i < count ; i++)
  158.     {
  159.         CLSID* pclisd = reinterpret_cast<CLSID*>(m_listWorld.GetItemDataPtr(i)) ;
  160.         delete pclisd ;
  161.     }
  162.  
  163.     CDialog::OnDestroy();
  164. }
  165.  
  166. void CMFCTangramConfigDlg::OnCancel()
  167. {
  168.     m_clsid = GUID_NULL ;
  169.  
  170.     CDialog::OnCancel();
  171. }
  172.  
  173. void CMFCTangramConfigDlg::OnOK()
  174. {
  175.     if (m_listWorld.GetCount() != LB_ERR)
  176.     {
  177.         int index = m_listWorld.GetCurSel() ;
  178.         if (index == LB_ERR)
  179.         {
  180.             index = 0 ;
  181.         }
  182.         m_clsid = *(reinterpret_cast<CLSID*>(m_listWorld.GetItemDataPtr(index))) ;
  183.     }
  184.     else
  185.     {
  186.         m_clsid = GUID_NULL ;
  187.     }
  188.  
  189.     CDialog::OnOK();
  190. }
  191.  
  192. ///////////////////////////////////////////////////////////
  193. //
  194. // getFriendlyName Helper function.
  195. //
  196. BOOL getFriendlyName(const CLSID& clsid, LPCTSTR szFriendlyName, int iLength)
  197. {
  198.     HKEY hKey;
  199.     char szKeyBuf[1024] ;
  200.     char szCLSID[CLSID_STRING_SIZE] ;
  201.  
  202.     // Convert the clsid to a string.
  203.     CLSIDtochar(clsid, szCLSID, CLSID_STRING_SIZE) ;
  204.  
  205.     // Make the key.
  206.     sprintf(szKeyBuf, "CLSID\\%s", szCLSID) ;
  207.  
  208.     // Create and open key and subkey.
  209.     long lResult = RegOpenKeyEx(    HKEY_CLASSES_ROOT ,
  210.                                     szKeyBuf,
  211.                                     0,
  212.                                     KEY_ALL_ACCESS,
  213.                                     &hKey) ;
  214.     if (lResult != ERROR_SUCCESS)
  215.     {
  216.         return FALSE ;
  217.     }
  218.  
  219.     // Set the Value.
  220.     ASSERT(szFriendlyName != NULL) ;
  221.     DWORD dwSize = iLength ;
  222.     lResult = RegQueryValueEx( hKey, NULL, NULL, NULL, (BYTE *)szFriendlyName, &dwSize);
  223.  
  224.     RegCloseKey(hKey);
  225.  
  226.     return lResult == ERROR_SUCCESS;
  227. }
  228.