???? ID: 183599 - ????? ???????: 22 ??????? 2011 - ??????: 6.0

??? ???? ????????? ????? ?? ????? C++ ?? ????? ?? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?? ???? ????????? ???? ?? ?? Microsoft Word 97 ????????? ????? Microsoft Visual C++ ?? ????? ???? ??? ??? ???? ????? ???????, ????????? ?? ??? ?? ???? ??? Microsoft Word ?? ??? ??????? ???? ???; Microsoft Office ?????????, ??? ?? ???? ???? ????????? ????? ?? ?????? ?? ?? ???? ??? ?? ??? ???? ????

???? ???????

????? ????? ?? ???? illustrate ???? ?? MFC ????? ?? ??? Microsoft Word 97 ????????? ????? Startup(), DocumentChange() ?? Quit() catches ?????????:
  1. ??? ??? ????? ?????-?????? ????????? MFC ?? ????? ?? ????? AppWizard. ???? ????????? WordEvents ???, ?? ???????? ???????? ??????? ?????
  2. ???? ????? ????? ??? ?? ??? ????? ?? ??? ??? "??????? ?? ?????" ?? "??? ?? ???, ????" respectively ???
  3. "??????? ?? ?????" ??? ?? ??? ??? ?????? ??? ????? ??? ??????:
          // Check to see if you've already started the server.
          if(m_app.m_lpDispatch != NULL) {
             AfxMessageBox("Server already started.");
             return;
          }
    
          char buf[256]; // General purpose buffer.
    
          // Start Automation server.
          COleException e;
          if(!m_app.CreateDispatch("Word.Application.8", &e)) {
             sprintf(buf, "Error on CreateDispatch(): %ld (%08lx)",
               e.m_sc, e.m_sc);
             AfxMessageBox(buf, MB_SETFOREGROUND);
             return;
    
          }
    
          // Make server visible through automation.
          // I.e.: Application.Visible = TRUE
          DISPID dispID;
          unsigned short *ucPtr;
          BYTE *parmStr;
          ucPtr = L"visible";
          m_app.m_lpDispatch->GetIDsOfNames(
               IID_NULL, &ucPtr, 1, LOCALE_USER_DEFAULT, &dispID
          );
          parmStr = (BYTE *)( VTS_VARIANT );
          m_app.InvokeHelper(
             dispID, DISPATCH_METHOD | DISPATCH_PROPERTYPUT, VT_EMPTY,
             NULL, parmStr, &COleVariant((short)TRUE)
          );
    
          // Declare the events you want to catch.
    
          // {000209F7-0000-0000-C000-000000000046}
          static const GUID IID_IWord8AppEvents =
          {0x000209f7,0x000,0x0000,{0xc0,0x00,0x0,0x00,0x00,0x00,0x00,0x46 } };
    
          //  Steps for setting up events.
          // 1. Get server's IConnectionPointContainer interface.
          // 2. Call IConnectionPointContainerFindConnectionPoint()
          //    to find the event you want to catch.
          // 3. Call IConnectionPoint::Advise() with the IUnknown
          //    interface of your implementation of the events.
    
          HRESULT hr;
    
          // Get server's IConnectionPointContainer interface.
          IConnectionPointContainer *pConnPtContainer;
          hr = m_app.m_lpDispatch->QueryInterface(
             IID_IConnectionPointContainer,
             (void **)&pConnPtContainer
          );
          ASSERT(!FAILED(hr));
    
          // Find connection point for events you're interested in.
          hr = pConnPtContainer->FindConnectionPoint(
             IID_IWord8AppEvents,
             &m_pConnectionPoint
          );
          ASSERT(!FAILED(hr));
    
          // Get the IUnknown interface of your event implementation.
          LPUNKNOWN pUnk = m_myEventSink.GetInterface(&IID_IUnknown);
          ASSERT(pUnk);
    
          // Setup advisory connection!
          hr = m_pConnectionPoint->Advise(pUnk, &m_adviseCookie);
          ASSERT(!FAILED(hr));
    
          // Release IConnectionPointContainer interface.
          pConnPtContainer->Release();
    					
  4. "??? ?? ????? ??" ??? ?? ??? ??? ?????? ??? ????? ??? ??????:
          // Check if you've started the server.
          if(m_app.m_lpDispatch == NULL) {
             AfxMessageBox("You haven't started the server yet.");
             return;
          }
          m_pConnectionPoint->Unadvise(m_adviseCookie);
    
          // Tell server to quit.
          // Application.Quit()
          DISPID dispID;                   // Temporary DISPID
          unsigned short *ucPtr;           // Temporary name holder
          ucPtr = L"quit";
          m_app.m_lpDispatch->GetIDsOfNames(
    
               IID_NULL, &ucPtr, 1, LOCALE_USER_DEFAULT, &dispID
          );
    
          m_app.InvokeHelper(dispID, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
    
          // Release application object.
          m_app.ReleaseDispatch();
    					
  5. MFC ClassWizard (CTRL + W) ?? ??????? ????, ?? ??????? ?????? ?? ??? ?? CCmdTarget ?? ??? ?? ????? ?????? ("???????" ???? ??????)? ?? ???? MyEventSink ??? ??; ??? Microsoft Word ?? ????????? ????? ?? ????? ??????????? ???? ??????
  6. MFC ClassWizard ??? ??????? ??? ????? ????, ?? ????? ??? ??????? ?? ???? ??? ??????:
          void Startup()
          void Quit()
          void DocumentChange()
    					
  7. ????????? ???? ?? ??? ?? ?? ??????? ?? ??????????? MyEventSink.cpp, ??? ?? ?? ???? ????? ??? ?? ?? ?? ?????? ??? ???? ???? ?? ??? ??? ???? ?? ?? ????? ?????:
          void MyEventSink::Startup()
          {
             AfxMessageBox("MyEventSink::Startup() called.");
          }
    
    
          void MyEventSink::Quit()
          {
             AfxMessageBox("MyEventSink::Quit() called.");
          }
    
          void MyEventSink::DocumentChange()
          {
             AfxMessageBox("MyEventSink::DocumentChange() called.");
          }
    					
  8. ???? MyEventSink.cpp ????? ????? ?? declaration IID_IMyEventSink ?? ??? ??? ????? ClassWizard ???? ??????? ?? ??? ??? ??? ????? GUID ?? ????? ???? ??? ??, ????? ?? GUID ??? ???? ?? ?? ?? ???? ??????? ???????? ?? ??????????? ???, ??????? ???? ???? ??? ???? ?? ??? ????????? ???? ?? ??? ??? IID_IMyEventSink ?? ??? declaration ?? ??????????? ???????: ????? const GUID IID_IMyEventSink = {0x000209f7, 0x000, 0x0000, {0xc0, 0x00, 0x0, 0x00, 0x00 0x00, 0x00, 0x46}};
  9. ????? ????????? ????? ?? WordEventsDlg.h ??? ???? WordEventsDlg ???? ??? ??????:
          COleDispatchDriver m_app;
          IConnectionPoint *m_pConnectionPoint;
          DWORD m_adviseCookie;
          MyEventSink m_myEventSink;
    					
  10. ???? ???? CWordEventsDlg declaration ???? ?? ???? WordEventsDlg.h ???? ?? ??? ????? ?????? ??????:
          #include "MyEventSink.h"
    					
  11. MyEventSink.h ????? ????? ?? destructor ?? declaration ??????; ??? ?? ??? ??? ????? ????:
          // Implementation
          protected:
          virtual ~MyEventSink();
    					
  12. ???? ??? ?? ????????? ?? ??? ??? ????? ?? declaration ???? "??????" ??? ?? ????:
          virtual ~MyEventSink();
          // Implementation
          protected:
          // virtual ~MyEventSink(); // Or this line may be removed.
    					
  13. ???, ????????? ???? OLE/COM ????????? ?? ??????? ???? ?? ??? ?? ???? ??????? ????? ????? ??? ???? ???? ?? ???? ???? "??????? ?? ?????" ??? ?????? ??????? ?? ??????? ???? ?? ????????? ????????? ?? ??????? ???? ??, ?? ???? ?????? ?? destroyed ????? ??? Constructor ?? ?? ???? ?? destructor ??????? ?? ???? ???? ?? ??? handy ?? ????? ?????? ?????:
          // Ole-initialization class.
          class OleInitClass {
          public:
             OleInitClass() {
                OleInitialize(NULL);
             }
             ~OleInitClass() {
                OleUninitialize();
             }
          };
          // This global class calls OleInitialize() at
          // application startup, and calls OleUninitialize()
          // at application exit.
          OleInitClass g_OleInitClass;
    					
  14. ?????? ???? ?? ??????
????????? ?? ??? ??, ?? ??? ????? ???? "??????? ???? ?? ?????"Microsoft Word ??????? ????, ?? ????? ??????? ?? ??? ???? ?? ??? ???? Microsoft Word ??? ?????????? ??,?????? ??? ????????? ?????? ???? DocumentChange() ????? ?? fired ??????? ???? ?????? ???? ???? ???????? ?????, ?? ????? ?? ?? ?? ?? ???? ?? fired ?? ????? ?? ???????? ?? ??????? ?? ????? ????? ?? ???? ??? "???? ?????? ?? ??? ???? ????"??? ?? ????? ??????? ?? ??? ???? ?? Microsoft Word, ?? ?? ??? ???? ?? ??? Microsoft Word ?? ???? ?????? ?? ???? ??? (?????????? ??,exit) ?? Quit ????? ?? ????? ????

?? ????? ?? ???? ?? ?? ????????? ????? ?????? ??? ???? ??? ??????? ?? fired ????? ??? ???? ?? ???? ?? ??? ??? ?????? ???? ?? ??????? ????????? ?????? ?? ??? ??????? ???? ?? ???? ?? ????? ?? ??????? ???? ?? ??? ??? ???? ??? ??? ?? ??????? ??????? ?? ????? ?? ??? ?????

Microsoft Excel ???? interesting ?? ????? ????? ?? ?????? ???? ??, ?? ?????? ??? ???? ?? ??? ?? ???? ????? ?? ???? ?? ???? ???? ???????, ??? ??? ??????? ?? ??? ???? ?? ???:
  • ?? ??? ?????? ??? ????? ?? ?????? ????????? ?? ????? ?? ??? C++ 5.0 Microsoft Visual ?? ??? ??? ?? ?? OLE/COM ???????? ?????? ?? ????? ????? ????? ?? ???, ????? ????? (????? ?? ?? ???? ????) coclass declarations; ???????? coclass ?? ??? ??????? ????? ???????? ????? ?? ?? ?? ????? ??????? ??? ????? ?????? ??? ???? ???, ???? DISPIDs, ?? ????? ???? ?? ?? ??? declared ???? ?? ????? ????
  • ??? Microsoft Word ?? ????????? ?????, Startup(), Quit(), ?? DocumentChange() ?? DISPIDs 1, 2 ?? 3 ?? ???, respectively ??????? ????? MyEventSink ???? ??? ????? ??????? ?? ??? DISPIDs ?? ??????? ???? ?? ??? ?????? ???? ??? ??? ?? ?? ??????? ?? ???? ??? ????? ???, ?? ???? ??? ???? ?? ??????? ClassWizard DISPID 1 ?? ??????? ?????? ????????? ?? ??? ???? ?? ??? ?????? ??????? ???? ?? ???? ???????, ?????? ?????, ????????????? Microsoft Excel ?? ?????, ???? DISPID 1 ?? ??? ??????? ????? ??? ?????? ??? ?? ?????? ??? ??? MyEventSink.cpp DISPIDs ?? ??? ??????? ?? ??? ?????? ?? ??? ?????? ??? ?? ??????? ???? ?????? ????

??????

????? ?? ???? ??? ???? ??????? ?? ??? ??????? ?????? ?? simplifying ??????? ????????? ??, ??? ?? Microsoft ???????? ??? ????? ?? ??? ????? ???? ?????? ?? ????? ????:
181845  (http://support.microsoft.com/kb/181845/ ) HOWTO: COM MFC-?????? ??????? ??? ??? ???? ???????? ?????
?? ??????? ?? ??????, ?? ?? ???? ??? ???? ???????, ?? ??? ??????? ?????, ????? Connpts.exe ????? ??? ????? ???? ?? Microsoft ???????? ??? ?????:
152087  (http://support.microsoft.com/kb/152087/ ) ??? MFC ??? Connpts.exe Implements ??????? ?????

???? ???? ???? ??:
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
  • Microsoft Word 97 Standard Edition
??????: 
kbprogramming kbautomation kbcode kbfaq kbhowto kbinterop kbmt KB183599 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:183599  (http://support.microsoft.com/kb/183599/en-us/ )