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

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