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

  1. // Commands.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "bldrec.h"
  6. #include "Commands.h"
  7. #include "dlgfile.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. LPCTSTR szMainKey = _T("Software\\Microsoft\\devstudio\\AddIns\\BldRec\\");
  16. LPCTSTR szThisKey = _T("LogFile");
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CCommands
  20.  
  21. CCommands::CCommands()
  22. {
  23.     m_pApplication = NULL;
  24.     m_pApplicationEventsObj = NULL;
  25.     m_pDebuggerEventsObj = NULL;
  26.     m_theLastTime = CTime::GetCurrentTime();
  27. }
  28.  
  29. CCommands::~CCommands()
  30. {
  31.     ASSERT (m_pApplication != NULL);
  32.     m_pApplication->Release();
  33.  
  34.     SaveFileName();
  35. }
  36.  
  37. void CCommands::SetApplicationObject(IApplication* pApplication)
  38. {
  39.     // This function assumes pApplication has already been AddRef'd
  40.     //  for us, which CDSAddIn did in its QueryInterface call
  41.     //  just before it called us.
  42.     m_pApplication = pApplication;
  43.  
  44.     // Create Application event handlers
  45.     XApplicationEventsObj::CreateInstance(&m_pApplicationEventsObj);
  46.     m_pApplicationEventsObj->AddRef();
  47.     m_pApplicationEventsObj->Connect(m_pApplication);
  48.     m_pApplicationEventsObj->m_pCommands = this;
  49.  
  50.     // Create Debugger event handler
  51.     CComPtr<IDispatch> pDebugger;
  52.     if (SUCCEEDED(m_pApplication->get_Debugger(&pDebugger)) 
  53.         && pDebugger != NULL)
  54.     {
  55.         XDebuggerEventsObj::CreateInstance(&m_pDebuggerEventsObj);
  56.         m_pDebuggerEventsObj->AddRef();
  57.         m_pDebuggerEventsObj->Connect(pDebugger);
  58.         m_pDebuggerEventsObj->m_pCommands = this;
  59.     }
  60. }
  61.  
  62. void CCommands::UnadviseFromEvents()
  63. {
  64.     ASSERT (m_pApplicationEventsObj != NULL);
  65.     m_pApplicationEventsObj->Disconnect(m_pApplication);
  66.     m_pApplicationEventsObj->Release();
  67.     m_pApplicationEventsObj = NULL;
  68.  
  69.     if (m_pDebuggerEventsObj != NULL)
  70.     {
  71.         // Since we were able to connect to the Debugger events, we
  72.         //  should be able to access the Debugger object again to
  73.         //  unadvise from its events (thus the VERIFY_OK below--see stdafx.h).
  74.         CComPtr<IDispatch> pDebugger;
  75.         VERIFY_OK(m_pApplication->get_Debugger(&pDebugger));
  76.         ASSERT (pDebugger != NULL);
  77.         m_pDebuggerEventsObj->Disconnect(pDebugger);
  78.         m_pDebuggerEventsObj->Release();
  79.         m_pDebuggerEventsObj = NULL;
  80.     }
  81. }
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Event handlers
  86.  
  87. // TODO: Fill out the implementation for those events you wish handle
  88. //  Use m_pCommands->GetApplicationObject() to access the Developer
  89. //  Studio Application object
  90.  
  91. // Application events
  92.  
  93. HRESULT CCommands::XApplicationEvents::BeforeBuildStart()
  94. {
  95.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  96.     CComBSTR bstrProj;
  97.     CComPtr<IGenericProject> pProj = NULL;
  98.     CString strStart;
  99.  
  100.     m_pCommands->GetApplicationObject()->get_ActiveProject((IDispatch **)&pProj.p);
  101.     if (pProj)
  102.     {
  103.         pProj->get_Name(&bstrProj);
  104.     }
  105.     strStart = "Build Started " + strStart;
  106.     m_pCommands->MarkTime(strStart, TRUE);
  107.     return S_OK;
  108. }
  109.  
  110. HRESULT CCommands::XApplicationEvents::BuildFinish(long nNumErrors, long nNumWarnings)
  111. {
  112.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  113.     CString strBuildInfo;
  114.     CStdioFile filePlg;
  115.  
  116.     strBuildInfo.Format("Build Finished! Errors=%d, Warnings=%d", nNumErrors, nNumWarnings);
  117.     m_pCommands->MarkTime(strBuildInfo, FALSE);
  118.     return S_OK;
  119. }
  120.  
  121. HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown()
  122. {
  123.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  124.     return S_OK;
  125. }
  126.  
  127. HRESULT CCommands::XApplicationEvents::DocumentOpen(IDispatch* theDocument)
  128. {
  129.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  130.     return S_OK;
  131. }
  132.  
  133. HRESULT CCommands::XApplicationEvents::BeforeDocumentClose(IDispatch* theDocument)
  134. {
  135.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  136.     return S_OK;
  137. }
  138.  
  139. HRESULT CCommands::XApplicationEvents::DocumentSave(IDispatch* theDocument)
  140. {
  141.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  142.     return S_OK;
  143. }
  144.  
  145. HRESULT CCommands::XApplicationEvents::NewDocument(IDispatch* theDocument)
  146. {
  147.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  148.     return S_OK;
  149. }
  150.  
  151. HRESULT CCommands::XApplicationEvents::WindowActivate(IDispatch* theWindow)
  152. {
  153.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  154.     return S_OK;
  155. }
  156.  
  157. HRESULT CCommands::XApplicationEvents::WindowDeactivate(IDispatch* theWindow)
  158. {
  159.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  160.     return S_OK;
  161. }
  162.  
  163. HRESULT CCommands::XApplicationEvents::WorkspaceOpen()
  164. {
  165.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  166.     return S_OK;
  167. }
  168.  
  169. HRESULT CCommands::XApplicationEvents::WorkspaceClose()
  170. {
  171.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  172.     return S_OK;
  173. }
  174.  
  175. HRESULT CCommands::XApplicationEvents::NewWorkspace()
  176. {
  177.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  178.     return S_OK;
  179. }
  180.  
  181. // Debugger event
  182.  
  183. HRESULT CCommands::XDebuggerEvents::BreakpointHit(IDispatch* pBreakpoint)
  184. {
  185.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  186.     return S_OK;
  187. }
  188.  
  189.  
  190. /////////////////////////////////////////////////////////////////////////////
  191. // CCommands methods
  192.  
  193. STDMETHODIMP CCommands::BldrecCommandMethod() 
  194. {
  195.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  196.  
  197.     VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
  198.     
  199.     CDlgFile dlgFile;
  200.     CRegKey regKey;
  201.  
  202.     long lRes;
  203.     unsigned long lSize = _MAX_PATH;
  204.  
  205.     lRes = regKey.Create(HKEY_CURRENT_USER, szMainKey);
  206.     if (SUCCEEDED(lRes))
  207.     {
  208.         regKey.QueryValue(m_strFile.GetBuffer(lSize), szThisKey, &lSize);
  209.         m_strFile.ReleaseBuffer();
  210.     }
  211.     if (m_strFile.IsEmpty())
  212.     {
  213.         m_strFile.LoadString(IDS_DEFAULTFILE);
  214.     }
  215.     
  216.     dlgFile.SetFile(m_strFile);
  217.     if (dlgFile.DoModal() == IDOK)
  218.     {
  219.         m_strFile = dlgFile.GetFile();
  220.     }
  221.     SaveFileName();
  222.     VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
  223.     return S_OK;
  224. }
  225.  
  226. BOOL CCommands::SaveFileName()
  227. {
  228.     CRegKey regKey;
  229.     long lRes = 0;
  230.  
  231.     if (!m_strFile.IsEmpty())
  232.     {
  233.         lRes = regKey.Create(HKEY_CURRENT_USER, szMainKey);
  234.         regKey.SetValue(m_strFile, szThisKey);
  235.     }
  236.     return(lRes == ERROR_SUCCESS);
  237. }
  238.  
  239. BOOL CCommands::MarkTime(LPCSTR szItem, BOOL fStart)
  240. {
  241.     if (m_strFile.IsEmpty())
  242.         return(FALSE);
  243.  
  244.  
  245.     CString strLogItem;
  246.     CTime theTime = CTime::GetCurrentTime();
  247.     CTimeSpan theDiff;
  248.     CComPtr<IDispatch> pDispConfig;
  249.     CComQIPtr<IConfiguration, &IID_IConfiguration> pConfig;
  250.     CString strConfig;
  251.     CComBSTR bstrConfig;
  252.  
  253.     try
  254.     {
  255.         if (SUCCEEDED(m_pApplication->get_ActiveConfiguration(&pDispConfig)) && pDispConfig)
  256.         {
  257.             pConfig = pDispConfig;
  258.             pDispConfig = NULL;
  259.             if (SUCCEEDED(pConfig->get_Name(&bstrConfig)))
  260.             {
  261.                 strConfig = bstrConfig;
  262.             }
  263.         }
  264.         CStdioFile logFile(m_strFile, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite);
  265.         logFile.SeekToEnd();
  266.         CTimeSpan theDiff = theTime - m_theLastTime;
  267.         if (fStart)
  268.         {
  269.             strLogItem.Format(_T("%s :             %s -- %s\n"), theTime.Format(_T("%I:%M:%S %p %m/%d/%y")), szItem, strConfig);
  270.         }
  271.         else
  272.         {
  273.             strLogItem.Format(_T("%s : %s   %s -- %s\n"), theTime.Format(_T("%I:%M:%S %p %m/%d/%y")),
  274.                 theDiff.Format(_T("%H:%M:%S ")),
  275.                 szItem, strConfig);
  276.         }
  277.         m_theLastTime = theTime;
  278.         logFile.WriteString(strLogItem);
  279.         logFile.Close();
  280.     }
  281.     catch(...)
  282.     {
  283.     }
  284.     return(TRUE);
  285. }
  286.  
  287. BOOL AnalyzePlgFile(LPCSTR szProjName)
  288. {
  289.     /*
  290.     To Find all files compiled
  291.     Look for "Compiling..." and go until "Generating Code...", then repeat.
  292.     Files inbetween have type *.obj.
  293.     Catch "Stopped by user" (or whatever it is)
  294.     */
  295.     BOOL fOK = TRUE;
  296.  
  297.     return(fOK);
  298. }