home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / comide / comide1 / comide1.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  5.1 KB  |  174 lines

  1. //1
  2. // NOTE: This program will only automate msdev from within itself
  3. //       under _DEBUG build.  Run comide1.exe standalone or from within msdev
  4. //       using BuildExecute (Ctrl-F5), not DebugGo.
  5.  
  6. // Copyright (C) 1992-1998 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. // This source code is only intended as a supplement to the
  10. // Microsoft Visual C++ Language  Reference and related
  11. // electronic documentation provided with Microsoft Visual C++.
  12. // See these sources for detailed information regarding the
  13. // Microsoft Visual C++ product.
  14.  
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #include <tchar.h>
  18.  
  19. #import <devshl.dll> no_namespace
  20. #import <devedit.pkg> rename("ReplaceText", "ReplaceTxt") rename("FindText", "FindTxt")
  21.  
  22. using namespace DSTextEditor;
  23.  
  24. const int nBufLen = 512;
  25. const int nLives = 4; // nLives must be even
  26.  
  27. // TO DO: Edit these paths
  28. #pragma message ("Edit the paths at lines 19-21 in comide1.cpp to point to your sample tree.")
  29. LPCTSTR pszSource = _T("d:/samples/com/comide/comide1/comide1.cpp");
  30. LPCTSTR pszProject = _T("d:/samples/com/comide/comide2/comide2.dsw");
  31. LPCTSTR pszProjectSource = _T("d:/samples/com/comide/comide2/comide2.cpp");
  32.  
  33. void dump_com_error(_com_error &e)
  34. {
  35.     _tprintf(_T("Oops - hit an error!\n"));
  36.     _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  37.     _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  38.     _bstr_t bstrSource(e.Source());
  39.     _bstr_t bstrDescription(e.Description());
  40.     _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  41.     _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
  42. }
  43.  
  44. struct StartUpCom {
  45.     StartUpCom() { CoInitialize(NULL); }
  46.     ~StartUpCom() { CoUninitialize(); }
  47. } _global_com_inst;
  48.  
  49. void main()
  50. {
  51.     IApplicationPtr pApp;
  52.  
  53.     try {
  54.     try {
  55.     // Start up existing msdev, if available
  56.     HRESULT hr = pApp.GetActiveObject(L"MSDEV.APPLICATION");
  57.     if (FAILED(hr))
  58.         _com_issue_error(hr);
  59.  
  60.     } catch(_com_error& e) {
  61.     dump_com_error(e);
  62.     _tprintf(_T("\aNOTE: This is not a TRUE error!!\n"));
  63.     _tprintf(_T("\aStarting up Msdev.exe...........\n"));
  64.  
  65.     try {
  66.     // Start up fresh instance of msdev
  67.     HRESULT hr = pApp.CreateInstance(L"MSDEV.APPLICATION", NULL, CLSCTX_LOCAL_SERVER);
  68.     if (FAILED(hr))
  69.         _com_issue_error(hr);
  70.     } catch(_com_error& e) {
  71.     dump_com_error(e);
  72.     }
  73.  
  74.     }
  75.  
  76.     pApp->Visible = VARIANT_TRUE;
  77.  
  78.     IDocumentsPtr pDocs = pApp->Documents;
  79.  
  80.     pDocs->Open(pszProject, "Auto", true);
  81.     pDocs->Open(pszProjectSource);
  82.  
  83.     ITextDocumentPtr pTextDoc = pApp->ActiveDocument;
  84.  
  85.     ITextWindowPtr pTextWin = pTextDoc->ActiveWindow;
  86.     pTextWin->WindowState = DSTextEditor::dsWindowStateMaximized;
  87.  
  88.     ITextSelectionPtr pTextSel = pTextDoc->Selection;
  89.  
  90.     pTextSel->SelectAll();
  91.     pTextSel->Delete();
  92.  
  93.     FILE* fptr1 = _tfopen(pszSource, _T("rt"));
  94.     assert(fptr1 != NULL);
  95.     TCHAR buf1[nBufLen];
  96.  
  97.     while (1)
  98.     {
  99.         assert(!feof(fptr1) && !ferror(fptr1));
  100.         LPTSTR p = _fgetts(buf1, nBufLen, fptr1);
  101.         if (p != buf1)
  102.         {
  103.             assert(feof(fptr1));
  104.             assert(!ferror(fptr1));
  105.             break;
  106.         }
  107.         pTextSel->Text = p;
  108.     }
  109.     fclose(fptr1);
  110.  
  111.     pTextDoc->Save();
  112.     pTextSel->StartOfDocument();
  113.     pTextSel->SelectLine();
  114.  
  115.     _bstr_t pb = pTextSel->Text;
  116.     LPTSTR p = (LPTSTR) pb;
  117.     assert(p != NULL && _tcslen(p) > 2);
  118.     assert(isdigit(p[2]));
  119.     int nLife = 0;
  120.     _stscanf(&p[2], _T("%d"), &nLife);
  121.     assert(nLife > 0 && nLife <= 0x100000);
  122.  
  123.     if (nLife < nLives)
  124.         _stprintf(&p[2], _T("%d"), nLife+1);
  125.     else // nLives must be even
  126.     {
  127.         _stprintf(&p[3], _T("%s"), _T("       "));
  128.         _stprintf(&p[2], _T("%d"), 1);
  129.     }
  130.     pTextSel->LineUp();
  131.     pTextSel->DestructiveInsert(p);
  132.     pTextDoc->Save();
  133.  
  134.     assert(pTextSel->FindTxt("pszSource", (long) dsMatchFromStart));
  135.     pTextSel->SelectLine();
  136.     if ((nLife % 2) == 1)
  137.         assert(pTextSel->ReplaceTxt("comide1", "comide2", (long) dsMatchForward));
  138.     else
  139.         assert(pTextSel->ReplaceTxt("comide2", "comide1", (long) dsMatchForward));
  140.  
  141.     assert(pTextSel->FindTxt("pszProject", (long) dsMatchForward));
  142.     pTextSel->SelectLine();
  143.     if ((nLife % 2) == 0)
  144.         assert(pTextSel->ReplaceTxt("comide1", "comide2", (long) dsMatchForward));
  145.     else
  146.         assert(pTextSel->ReplaceTxt("comide2", "comide1", (long) dsMatchForward));
  147.  
  148.     assert(pTextSel->FindTxt("pszProjectSource", (long) dsMatchForward));
  149.     pTextSel->SelectLine();
  150.     if ((nLife % 2) == 0)
  151.         assert(pTextSel->ReplaceTxt("comide1", "comide2", (long) dsMatchForward));
  152.     else
  153.         assert(pTextSel->ReplaceTxt("comide2", "comide1", (long) dsMatchForward));
  154.  
  155.     pTextDoc->Save();
  156.     if (nLife < nLives)
  157.     {
  158.         pApp->RebuildAll();
  159.         pTextDoc->Close();
  160.         pApp->ExecuteCommand("BuildExecute");
  161.     }
  162.  
  163.     pDocs->CloseAll();
  164.     pApp->ExecuteCommand("WorkspaceClose");
  165.  
  166.     if (nLife >= nLives)
  167.         pApp->Quit();
  168.  
  169.     } catch(_com_error& e) {
  170.         dump_com_error(e);
  171.     }
  172. }
  173.  
  174.