home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / replall / replall.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  4KB  |  169 lines

  1. // ReplAll.cpp : Defines the initialization routines for the DLL.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <initguid.h>
  6. #include "ReplAll.h"
  7. #include "DSAddIn.h"
  8. #include "Commands.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. CComModule _Module;
  17.  
  18. BEGIN_OBJECT_MAP(ObjectMap)
  19.     OBJECT_ENTRY(CLSID_DSAddIn, CDSAddIn)
  20. END_OBJECT_MAP()
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CReplAllApp
  24.  
  25. class CReplAllApp : public CWinApp
  26. {
  27. public:
  28.     CReplAllApp();
  29.  
  30. // Overrides
  31.     // ClassWizard generated virtual function overrides
  32.     //{{AFX_VIRTUAL(CReplAllApp)
  33.     public:
  34.     virtual BOOL InitInstance();
  35.     virtual int ExitInstance();
  36.     //}}AFX_VIRTUAL
  37.  
  38.     //{{AFX_MSG(CReplAllApp)
  39.         // NOTE - the ClassWizard will add and remove member functions here.
  40.         //    DO NOT EDIT what you see in these blocks of generated code !
  41.     //}}AFX_MSG
  42.     DECLARE_MESSAGE_MAP()
  43. };
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CReplAllApp
  48.  
  49. BEGIN_MESSAGE_MAP(CReplAllApp, CWinApp)
  50.     //{{AFX_MSG_MAP(CReplAllApp)
  51.         // NOTE - the ClassWizard will add and remove mapping macros here.
  52.         //    DO NOT EDIT what you see in these blocks of generated code!
  53.     //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // The one and only CReplAllApp object
  58.  
  59. CReplAllApp theApp;
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CReplAllApp construction
  63.  
  64. CReplAllApp::CReplAllApp()
  65. {
  66.     // TODO: add construction code here,
  67.     // Place all significant initialization in InitInstance
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CReplAllApp initialization
  72.  
  73. BOOL CReplAllApp::InitInstance()
  74. {
  75.     _Module.Init(ObjectMap, m_hInstance);
  76.     return CWinApp::InitInstance();
  77. }
  78.  
  79. int CReplAllApp::ExitInstance()
  80. {
  81.     _Module.Term();
  82.     return CWinApp::ExitInstance();
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // Special entry points required for inproc servers
  87.  
  88. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  89. {
  90.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  91.     return _Module.GetClassObject(rclsid, riid, ppv);
  92. }
  93.  
  94. STDAPI DllCanUnloadNow(void)
  95. {
  96.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  97.     return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? 
  98.             S_OK : S_FALSE;
  99. }
  100.  
  101. // by exporting DllRegisterServer, you can use regsvr32.exe
  102. STDAPI DllRegisterServer(void)
  103. {
  104.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  105.     HRESULT hRes = S_OK;
  106.     
  107.     // Registers object, typelib and all interfaces in typelib
  108.     hRes = _Module.RegisterServer(TRUE);
  109.     if (FAILED(hRes))
  110.         return hRes;
  111.  
  112.     // Register description of this add-in object in its own
  113.     //  "/Description" subkey.
  114.     
  115.     _ATL_OBJMAP_ENTRY* pEntry = _Module.m_pObjMap;
  116.     CRegKey key;
  117.     LONG lRes = key.Open(HKEY_CLASSES_ROOT, _T("CLSID"));
  118.     if (lRes == ERROR_SUCCESS)
  119.     {
  120.         USES_CONVERSION;
  121.         LPOLESTR lpOleStr;
  122.         StringFromCLSID(*pEntry->pclsid, &lpOleStr);
  123.         LPTSTR lpsz = OLE2T(lpOleStr);
  124.  
  125.         lRes = key.Open(key, lpsz);
  126.         if (lRes == ERROR_SUCCESS)
  127.         {
  128.             CString strDescription;
  129.             strDescription.LoadString(IDS_REPLALL_DESCRIPTION);
  130.             key.SetKeyValue(_T("Description"), strDescription);
  131.         }
  132.         CoTaskMemFree(lpOleStr);
  133.     }
  134.     if (lRes != ERROR_SUCCESS)
  135.         hRes = HRESULT_FROM_WIN32(lRes);
  136.  
  137.     return hRes;
  138. }
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. // DllUnregisterServer - Removes entries from the system registry
  142.  
  143. STDAPI DllUnregisterServer(void)
  144. {
  145.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  146.  
  147.     HRESULT hRes = S_OK;
  148.     _Module.UnregisterServer();
  149.     return hRes;
  150. }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // Debugging support
  155.  
  156. // GetLastErrorDescription is used in the implementation of the VERIFY_OK
  157. //  macro, defined in stdafx.h.
  158.  
  159. #ifdef _DEBUG
  160.  
  161. void GetLastErrorDescription(CComBSTR& bstr)
  162. {
  163.     CComPtr<IErrorInfo> pErrorInfo;
  164.     if (GetErrorInfo(0, &pErrorInfo) == S_OK)
  165.         pErrorInfo->GetDescription(&bstr);
  166. }
  167.  
  168. #endif //_DEBUG
  169.