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

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