home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / oleview / iviewers / iviewers.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  6.5 KB  |  254 lines

  1. // iviewers.cpp : Defines the class behaviors for the application.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "iviewers.h"
  15.  
  16. #include "typelib.h"
  17. #include "iview.h"
  18. #include "util.h"
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CIViewersApp:
  27. // See iviewers.cpp for the implementation of this class
  28. //
  29.  
  30. class CIViewersApp : public CWinApp
  31. {
  32. public:
  33.     CIViewersApp();
  34.  
  35. // Overrides
  36.     // ClassWizard generated virtual function overrides
  37.     //{{AFX_VIRTUAL(CIViewersApp)
  38.     public:
  39.     virtual BOOL InitInstance();
  40.     virtual int ExitInstance();
  41.     //}}AFX_VIRTUAL
  42.  
  43.     //{{AFX_MSG(CIViewersApp)
  44.     afx_msg void OnAppAbout();
  45.         // NOTE - the ClassWizard will add and remove member functions here.
  46.         //    DO NOT EDIT what you see in these blocks of generated code !
  47.     //}}AFX_MSG
  48.     DECLARE_MESSAGE_MAP()
  49. };
  50.  
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CIViewersApp
  54.  
  55. BEGIN_MESSAGE_MAP(CIViewersApp, CWinApp)
  56.     //{{AFX_MSG_MAP(CIViewersApp)
  57.         // NOTE - the ClassWizard will add and remove mapping macros here.
  58.         //    DO NOT EDIT what you see in these blocks of generated code!
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CIViewersApp construction
  64.  
  65. CIViewersApp::CIViewersApp()
  66. {
  67.     // TODO: add construction code here,
  68.     // Place all significant initialization in InitInstance
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // The one and only CIViewersApp object
  73.  
  74. CIViewersApp NEAR theApp;
  75.  
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CIViewersApp initialization
  78.  
  79. BOOL CIViewersApp::InitInstance()
  80. {
  81.     SetRegistryKey( IDS_REGISTRYKEY );
  82. #if (!defined(_USRDLL) && !defined(_AFXDLL))
  83.     Enable3dControls();
  84. //  COleObjectFactory::UpdateRegistryAll();
  85.  
  86.     // Initialize OLE libraries
  87.     if (!AfxOleInit())
  88.     {
  89.         AfxMessageBox(IDP_OLE_INIT_FAILED);
  90.         return FALSE;
  91.     }
  92.  
  93.     m_pMainWnd = (CFrameWnd*)new CTypeLibWnd ;
  94.     ASSERT(m_pMainWnd) ;
  95.  
  96.     if (m_lpCmdLine && *m_lpCmdLine)
  97.     {
  98.         HRESULT hr = LoadTypeLib( m_lpCmdLine, &((CTypeLibWnd*)m_pMainWnd)->m_ptlb ) ;
  99.         if (FAILED(hr))
  100.         {
  101.             CString str ;
  102.             str.Format( _T("LoadTypeLib(%s) failed"), m_lpCmdLine ) ;
  103.             ErrorMessage( str, hr ) ;
  104.             return FALSE ;
  105.         }
  106.     }
  107.     else
  108.     {
  109.         static TCHAR szFilter[] = _T("TypeLib Files (*.tlb;*.olb;*.dll;*.ocx;*.exe)|*.tlb;*.olb;*.dll;*.ocx;*.exe|AllFiles(*.*)|*.*|") ;
  110.  
  111.         CFileDialog dlg( TRUE, _T("*.tlb"), NULL,
  112.                         OFN_FILEMUSTEXIST | OFN_HIDEREADONLY |
  113.                         OFN_PATHMUSTEXIST
  114.                         , szFilter, NULL )  ;
  115.  
  116.         if (IDOK != dlg.DoModal())
  117.             return FALSE ;
  118.  
  119.         HRESULT hr = LoadTypeLib( dlg.GetPathName(), &((CTypeLibWnd*)m_pMainWnd)->m_ptlb ) ;
  120.         if (FAILED(hr))
  121.         {
  122.             CString str ;
  123.             str.Format( _T("LoadTypeLib(%s) failed"), dlg.GetPathName() ) ;
  124.             ErrorMessage( str, hr ) ;
  125.             return FALSE ;
  126.         }
  127.  
  128.     }
  129.  
  130.     if (((CTypeLibWnd*)m_pMainWnd)->LoadFrame(IDR_TYPELIBWND, WS_OVERLAPPEDWINDOW, NULL, NULL) == FALSE)
  131.     {
  132.         TRACE(_T("LoadFrame failed\n") );
  133.         if (((CTypeLibWnd*)m_pMainWnd)->m_ptlb)
  134.             ((CTypeLibWnd*)m_pMainWnd)->m_ptlb->Release() ;
  135.         return FALSE ;
  136.     }
  137.     ASSERT(m_pMainWnd) ;
  138.  
  139.     ((CTypeLibWnd*)m_pMainWnd)->RestorePosition() ;
  140.     ((CTypeLibWnd*)m_pMainWnd)->UpdateWindow() ;
  141.  
  142. #endif
  143.  
  144.     // Register all OLE server (factories) as running.  This enables the
  145.     //  OLE libraries to create objects from other applications.
  146.     COleObjectFactory::RegisterAll();
  147.  
  148.     return TRUE;
  149. }
  150.  
  151. int CIViewersApp::ExitInstance()
  152. {
  153.     return CWinApp::ExitInstance();
  154. }
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157. // Special entry points required for iviewers servers
  158.  
  159. #if (defined(_USRDLL) || defined(_AFXDLL))
  160. extern "C"{
  161. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  162. {
  163.     return AfxDllGetClassObject(rclsid, riid, ppv);
  164. }
  165.  
  166. STDAPI DllCanUnloadNow(void)
  167. {
  168.     return AfxDllCanUnloadNow();
  169. }
  170.  
  171. // by exporting DllRegisterServer, you can use regsvr.exe
  172. STDAPI DllRegisterServer(void)
  173. {
  174.     HRESULT hr ;
  175.     COleObjectFactory::UpdateRegistryAll();
  176.     hr = DllRegisterInterfaceViewer( CLSID_ITypeLibViewer, IID_ITypeLib );
  177.     if (FAILED(hr))
  178.         return hr ;
  179.     hr = DllRegisterInterfaceViewer( CLSID_IDispatchViewer, IID_IDispatch );
  180.     if (FAILED(hr))
  181.         return hr ;
  182.     return DllRegisterInterfaceViewer( CLSID_IDataObjectViewer, IID_IDataObject );
  183. }
  184. #endif //_USRDLL
  185.  
  186. STDAPI DllRegisterInterfaceViewer( REFCLSID rclsid, REFIID riid )
  187. {
  188.     HRESULT hr = S_OK ;
  189.     HKEY hk = NULL ;
  190.  
  191.     LPOLESTR lpszOleIID = NULL;
  192.     LPOLESTR lpszOleCLSID = NULL;
  193.     IMalloc* pmal = NULL ;
  194.  
  195.     if (FAILED(hr = ::CoGetMalloc(MEMCTX_TASK, &pmal)))
  196.         return hr;
  197.     ASSERT(pmal != NULL);
  198.  
  199.     TRY
  200.     {
  201.         USES_CONVERSION;
  202.         LONG cb ;
  203.         TCHAR szKey[128];
  204.         TCHAR szValue[128];
  205.  
  206.         // \Interface\{iid} exists?
  207.         if (FAILED(hr = StringFromCLSID(riid, &lpszOleIID)))
  208.             AfxThrowMemoryException() ;
  209.  
  210.         LPTSTR lpszIID = OLE2T(lpszOleIID);
  211.         wsprintf( szKey, _T("Interface\\%s"), lpszIID );
  212.         cb = sizeof(szValue);
  213.         if ((hr = ::RegQueryValue(HKEY_CLASSES_ROOT, szKey, szValue, &cb)) != ERROR_SUCCESS)
  214.             AfxThrowMemoryException() ;
  215.  
  216.         // Create \Interface\{iid}\Ole2ViewIViewerCLSID
  217.         wsprintf( szKey, _T("Interface\\%s\\Ole2ViewIViewerCLSID"), lpszIID);
  218.         if ((hr = ::RegCreateKey(HKEY_CLASSES_ROOT, szKey, &hk)) != ERROR_SUCCESS)
  219.             AfxThrowMemoryException() ;
  220.  
  221.         // Set = {clsid}
  222.         if (FAILED(hr = ::StringFromCLSID(rclsid, &lpszOleCLSID)))
  223.             AfxThrowMemoryException() ;
  224.  
  225.         LPTSTR lpszCLSID = OLE2T(lpszOleCLSID);
  226.         cb = lstrlen(lpszCLSID);
  227.         if ((hr = ::RegSetValue(hk, NULL, REG_SZ, lpszCLSID, cb)) != ERROR_SUCCESS)
  228.             AfxThrowMemoryException() ;
  229.     }
  230.     CATCH(CException, pException)
  231.     {
  232.     }
  233.     END_CATCH
  234.  
  235.     if (hk)
  236.         RegCloseKey( hk ) ;
  237.  
  238.     if (lpszOleIID && pmal)
  239.         pmal->Free(lpszOleIID) ;
  240.  
  241.     if (lpszOleCLSID && pmal)
  242.         pmal->Free(lpszOleCLSID) ;
  243.  
  244.     if (pmal)
  245.         pmal->Release() ;
  246.  
  247.     return hr ;
  248. }
  249.  
  250. }; // extern "C"
  251.  
  252. /////////////////////////////////////////////////////////////////////////////
  253. // CIViewersApp commands
  254.