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

  1. // AddInMod.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "bldrec.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.     // TODO: Replace the AddCommand call below with a series of calls,
  49.     //  one for each command your add-in will add.
  50.  
  51.     // The command name should not be localized to other languages.  The 
  52.     //  tooltip, command description, and other strings related to this
  53.     //  command are stored in the string table (IDS_CMD_STRING) and should
  54.     //  be localized.
  55.     LPCTSTR szCommand = _T("BldrecCommand");
  56.     VARIANT_BOOL bRet;
  57.     CString strCmdString;
  58.     strCmdString.LoadString(IDS_CMD_STRING);
  59.     strCmdString = szCommand + strCmdString;
  60.     CComBSTR bszCmdString(strCmdString);
  61.     CComBSTR bszMethod(_T("BldrecCommandMethod"));
  62.     CComBSTR bszCmdName(szCommand);
  63.     VERIFY_OK(pApplication->AddCommand(bszCmdString, bszMethod, 0, m_dwCookie, &bRet));
  64.     if (bRet == VARIANT_FALSE)
  65.     {
  66.         // AddCommand failed because a command with this name already
  67.         //  exists.  You may try adding your command under a different name.
  68.         //  Or, you can fail to load as we will do here.
  69.         *OnConnection = VARIANT_FALSE;
  70.         return S_OK;
  71.     }
  72.  
  73.     // Add toolbar buttons only if this is the first time the add-in
  74.     //  is being loaded.  Toolbar buttons are automatically remembered
  75.     //  by Developer Studio from session to session, so we should only
  76.     //  add the toolbar buttons once.
  77.     if (bFirstTime == VARIANT_TRUE)
  78.     {
  79.         VERIFY_OK(pApplication->
  80.             AddCommandBarButton(dsGlyph, bszCmdName, m_dwCookie));
  81.     }
  82.  
  83.     *OnConnection = VARIANT_TRUE;
  84.     return S_OK;
  85. }
  86.  
  87. // This is called on shut-down, and also when the user unloads the add-in
  88. STDMETHODIMP CDSAddIn::OnDisconnection(VARIANT_BOOL bLastTime)
  89. {
  90.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  91.  
  92.     m_pCommands->UnadviseFromEvents();
  93.     m_pCommands->Release();
  94.     m_pCommands = NULL;
  95.  
  96.     // TODO: Perform any cleanup work here
  97.  
  98.     return S_OK;
  99. }
  100.