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

  1. // bldrec.cpp : Defines the initialization routines for the DLL.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <initguid.h>
  6. #include "bldrec.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. // CBldrecApp
  24.  
  25. class CBldrecApp : public CWinApp
  26. {
  27. public:
  28.     CBldrecApp();
  29.  
  30. // Overrides
  31.     // ClassWizard generated virtual function overrides
  32.     //{{AFX_VIRTUAL(CBldrecApp)
  33.     public:
  34.     virtual BOOL InitInstance();
  35.     virtual int ExitInstance();
  36.     //}}AFX_VIRTUAL
  37.  
  38.     //{{AFX_MSG(CBldrecApp)
  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. // CBldrecApp
  48.  
  49. BEGIN_MESSAGE_MAP(CBldrecApp, CWinApp)
  50.     //{{AFX_MSG_MAP(CBldrecApp)
  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 CBldrecApp object
  58.  
  59. CBldrecApp theApp;
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CBldrecApp construction
  63.  
  64. CBldrecApp::CBldrecApp()
  65. {
  66.     // TODO: add construction code here,
  67.     // Place all significant initialization in InitInstance
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CBldrecApp initialization
  72.  
  73. BOOL CBldrecApp::InitInstance()
  74. {
  75.     _Module.Init(ObjectMap, m_hInstance);
  76.     return CWinApp::InitInstance();
  77. }
  78.  
  79. int CBldrecApp::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) ? S_OK : S_FALSE;
  98. }
  99.  
  100. // by exporting DllRegisterServer, you can use regsvr32.exe
  101. STDAPI DllRegisterServer(void)
  102. {
  103.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  104.     HRESULT hRes = S_OK;
  105.     
  106.     // Registers object, typelib and all interfaces in typelib
  107.     hRes = _Module.RegisterServer(TRUE);
  108.     if (FAILED(hRes))
  109.         return hRes;
  110.  
  111.     // Register description of this add-in object in its own
  112.     //  "/Description" subkey.
  113.     // TODO:  If you add more add-ins to this module, you need
  114.     //  to register all of their descriptions, each description
  115.     //  in each add-in object's registry CLSID entry:
  116.     // HKEY_CLASSES_ROOT\Clsid\{add-in CLSID}\Description="add-in description"
  117.     _ATL_OBJMAP_ENTRY* pEntry = _Module.m_pObjMap;
  118.     CRegKey key;
  119.     LONG lRes = key.Open(HKEY_CLASSES_ROOT, _T("CLSID"));
  120.     if (lRes == ERROR_SUCCESS)
  121.     {
  122.         USES_CONVERSION;
  123.         LPOLESTR lpOleStr;
  124.         StringFromCLSID(*pEntry->pclsid, &lpOleStr);
  125.         LPTSTR lpsz = OLE2T(lpOleStr);
  126.  
  127.         lRes = key.Open(key, lpsz);
  128.         if (lRes == ERROR_SUCCESS)
  129.         {
  130.             CString strDescription;
  131.             strDescription.LoadString(IDS_BLDREC_DESCRIPTION);
  132.             key.SetKeyValue(_T("Description"), strDescription);
  133.         }
  134.         CoTaskMemFree(lpOleStr);
  135.     }
  136.     if (lRes != ERROR_SUCCESS)
  137.         hRes = HRESULT_FROM_WIN32(lRes);
  138.  
  139.     return hRes;
  140. }
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // DllUnregisterServer - Removes entries from the system registry
  144.  
  145. STDAPI DllUnregisterServer(void)
  146. {
  147.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  148.  
  149.     HRESULT hRes = S_OK;
  150.     _Module.UnregisterServer();
  151.     return hRes;
  152. }
  153.  
  154.  
  155. /////////////////////////////////////////////////////////////////////////////
  156. // Debugging support
  157.  
  158. // GetLastErrorDescription is used in the implementation of the VERIFY_OK
  159. //  macro, defined in stdafx.h.
  160.  
  161. #ifdef _DEBUG
  162.  
  163. void GetLastErrorDescription(CComBSTR& bstr)
  164. {
  165.     CComPtr<IErrorInfo> pErrorInfo;
  166.     if (GetErrorInfo(0, &pErrorInfo) == S_OK)
  167.         pErrorInfo->GetDescription(&bstr);
  168. }
  169.  
  170. #endif //_DEBUG
  171.