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

  1. // API2Help.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 "API2Help.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. // CAPI2HelpApp
  27.  
  28. class CAPI2HelpApp : public CWinApp
  29. {
  30. public:
  31.     CAPI2HelpApp();
  32.  
  33. // Overrides
  34.     // ClassWizard generated virtual function overrides
  35.     //{{AFX_VIRTUAL(CAPI2HelpApp)
  36.     public:
  37.     virtual BOOL InitInstance();
  38.     virtual int ExitInstance();
  39.     //}}AFX_VIRTUAL
  40.  
  41.     //{{AFX_MSG(CAPI2HelpApp)
  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. // CAPI2HelpApp
  51.  
  52. BEGIN_MESSAGE_MAP(CAPI2HelpApp, CWinApp)
  53.     //{{AFX_MSG_MAP(CAPI2HelpApp)
  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 CAPI2HelpApp object
  61.  
  62. CAPI2HelpApp theApp;
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CAPI2HelpApp construction
  66.  
  67. CAPI2HelpApp::CAPI2HelpApp()
  68. {
  69.     // TODO: add construction code here,
  70.     // Place all significant initialization in InitInstance
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CAPI2HelpApp initialization
  75.  
  76. BOOL CAPI2HelpApp::InitInstance()
  77. {
  78.     _Module.Init(ObjectMap, m_hInstance);
  79.     return CWinApp::InitInstance();
  80. }
  81.  
  82. int CAPI2HelpApp::ExitInstance()
  83. {
  84.     _Module.Term();
  85.     return CWinApp::ExitInstance();
  86. }
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // Special entry points required for inproc servers
  90.  
  91. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  92. {
  93.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  94.     return _Module.GetClassObject(rclsid, riid, ppv);
  95. }
  96.  
  97. STDAPI DllCanUnloadNow(void)
  98. {
  99.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  100.     return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  101. }
  102.  
  103. // by exporting DllRegisterServer, you can use regsvr32.exe
  104. STDAPI DllRegisterServer(void)
  105. {
  106.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  107.     HRESULT hRes = S_OK;
  108.  
  109.     // Registers object, typelib and all interfaces in typelib
  110.     hRes = _Module.RegisterServer(TRUE);
  111.     if (FAILED(hRes))
  112.         return hRes;
  113.  
  114.     // Register description of this add-in object in its own
  115.     //  "/Description" subkey.
  116.     // TODO:  If you add more add-ins to this module, you need
  117.     //  to register all of their descriptions, each description
  118.     //  in each add-in object's registry CLSID entry:
  119.     // HKEY_CLASSES_ROOT\Clsid\{add-in CLSID}\Description="add-in description"
  120.     _ATL_OBJMAP_ENTRY* pEntry = _Module.m_pObjMap;
  121.     CRegKey key;
  122.     LONG lRes = key.Open(HKEY_CLASSES_ROOT, _T("CLSID"));
  123.     if (lRes == ERROR_SUCCESS)
  124.     {
  125.         USES_CONVERSION;
  126.         LPOLESTR lpOleStr;
  127.         StringFromCLSID(*pEntry->pclsid, &lpOleStr);
  128.         LPTSTR lpsz = OLE2T(lpOleStr);
  129.  
  130.         lRes = key.Open(key, lpsz);
  131.         if (lRes == ERROR_SUCCESS)
  132.         {
  133.             CString strDescription;
  134.             strDescription.LoadString(IDS_API2HELP_DESCRIPTION);
  135.             key.SetKeyValue(_T("Description"), strDescription);
  136.         }
  137.         CoTaskMemFree(lpOleStr);
  138.     }
  139.     if (lRes != ERROR_SUCCESS)
  140.         hRes = HRESULT_FROM_WIN32(lRes);
  141.  
  142.     return hRes;
  143. }
  144.  
  145. /////////////////////////////////////////////////////////////////////////////
  146. // DllUnregisterServer - Removes entries from the system registry
  147.  
  148. STDAPI DllUnregisterServer(void)
  149. {
  150.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  151.  
  152.     HRESULT hRes = S_OK;
  153.     _Module.UnregisterServer();
  154.     return hRes;
  155. }
  156.  
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // Debugging support
  160.  
  161. // GetLastErrorDescription is used in the implementation of the VERIFY_OK
  162. //  macro, defined in stdafx.h.
  163.  
  164. #ifdef _DEBUG
  165.  
  166. void GetLastErrorDescription(CComBSTR& bstr)
  167. {
  168.     CComPtr<IErrorInfo> pErrorInfo;
  169.     if (GetErrorInfo(0, &pErrorInfo) == S_OK)
  170.         pErrorInfo->GetDescription(&bstr);
  171. }
  172.  
  173. #endif //_DEBUG
  174.