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

  1. // AddInMod.cpp : implementation file
  2. //
  3.  
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6.  
  7.  
  8. #include "stdafx.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. // This is called when the user first loads the add-in, and on start-up
  20. //  of each subsequent Developer Studio session
  21. STDMETHODIMP CDSAddIn::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime,
  22.         long dwCookie, VARIANT_BOOL* OnConnection)
  23. {
  24.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  25.  
  26.     // Store info passed to us
  27.     IApplication* pApplication = NULL;
  28.     if (FAILED(pApp->QueryInterface(IID_IApplication, (void**) &pApplication))
  29.         || pApplication == NULL)
  30.     {
  31.         *OnConnection = VARIANT_FALSE;
  32.         return S_OK;
  33.     }
  34.  
  35.     m_dwCookie = dwCookie;
  36.  
  37.     // Create command dispatch, send info back to DevStudio
  38.     CCommandsObj::CreateInstance(&m_pCommands);
  39.     m_pCommands->AddRef();
  40.  
  41.     // The QueryInterface above AddRef'd the Application object.  It will
  42.     //  be Release'd in CCommand's destructor.
  43.     m_pCommands->SetApplicationObject(pApplication);
  44.  
  45.     // (see stdafx.h for the definition of VERIFY_OK)
  46.  
  47.     VERIFY_OK(pApplication->SetAddInInfo((long) AfxGetInstanceHandle(),
  48.         (LPDISPATCH) m_pCommands, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE, m_dwCookie));
  49.  
  50.     // Inform DevStudio of the commands we implement
  51.  
  52.     // The command name should not be localized to other languages.  The
  53.     //  tooltip, command description, and other strings related to this
  54.     //  command are stored in the string table (IDS_CMD_STRING) and should
  55.     //  be localized.
  56.     LPCTSTR szCommand = _T("API2HelpCommand");
  57.     VARIANT_BOOL bRet;
  58.     CString strCmdString;
  59.     strCmdString.LoadString(IDS_CMD_STRING);
  60.     strCmdString = szCommand + strCmdString;
  61.     CComBSTR bszCmdString(strCmdString);
  62.     CComBSTR bszMethod(_T("API2HelpCommandMethod"));
  63.     CComBSTR bszCmdName(szCommand);
  64.     VERIFY_OK(pApplication->AddCommand(bszCmdString, bszMethod, 0, m_dwCookie, &bRet));
  65.     if (bRet == VARIANT_FALSE)
  66.     {
  67.         // AddCommand failed because a command with this name already
  68.         //  exists.  You may try adding your command under a different name.
  69.         //  Or, you can fail to load as we will do here.
  70.         *OnConnection = VARIANT_FALSE;
  71.         return S_OK;
  72.     }
  73.  
  74.     // Add toolbar buttons only if this is the first time the add-in
  75.     //  is being loaded.  Toolbar buttons are automatically remembered
  76.     //  by Developer Studio from session to session, so we should only
  77.     //  add the toolbar buttons once.
  78.     if (bFirstTime == VARIANT_TRUE)
  79.     {
  80.         VERIFY_OK(pApplication->
  81.             AddCommandBarButton(dsGlyph, bszCmdName, m_dwCookie));
  82.     }
  83.  
  84.     *OnConnection = VARIANT_TRUE;
  85.     return S_OK;
  86. }
  87.  
  88. // This is called on shut-down, and also when the user unloads the add-in
  89. STDMETHODIMP CDSAddIn::OnDisconnection(VARIANT_BOOL bLastTime)
  90. {
  91.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  92.  
  93.     m_pCommands->Release();
  94.     m_pCommands = NULL;
  95.  
  96.     return S_OK;
  97. }
  98.