Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. thread in COM for Office Addin

thread in COM for Office Addin

Scheduled Pinned Locked Moved COM
helpdatabasecomtutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tomaz Rotovnik
    wrote on last edited by
    #1

    Hello I'm working on COM addin for Microsoft Word application. I build COM addin with two buttons. First button event trigers function TypeText which put some text on screen (in empty document). The problem is when I created thread in COM addin which waits for an event. Thread also includes same function TypeText for printing some text into document. Function is part of "document selection" object declared in MSO.dll. When I trigger an event with second button the function TypeText in my thread is called. I get an error "Exception Handling". Why the same function does not work in thread? please help :(( Here is example of my thread: void CMyAddin::StartProcOut() { CComPtr oSel; CComQIPtr spApp(m_pParentApp); ATLASSERT(spApp); _bstr_t Some_Text(OLESTR("HELLO?")); while (end_thread_out == false){ if (WaitForSingleObject(h_end_th_out,INFINITE) != WAIT_FAILED){ HRESULT hr = spApp->get_Selection(&oSel); oSel->TypeText(Some_Text); } } } Tomaz Rotovnik

    R 1 Reply Last reply
    0
    • T Tomaz Rotovnik

      Hello I'm working on COM addin for Microsoft Word application. I build COM addin with two buttons. First button event trigers function TypeText which put some text on screen (in empty document). The problem is when I created thread in COM addin which waits for an event. Thread also includes same function TypeText for printing some text into document. Function is part of "document selection" object declared in MSO.dll. When I trigger an event with second button the function TypeText in my thread is called. I get an error "Exception Handling". Why the same function does not work in thread? please help :(( Here is example of my thread: void CMyAddin::StartProcOut() { CComPtr oSel; CComQIPtr spApp(m_pParentApp); ATLASSERT(spApp); _bstr_t Some_Text(OLESTR("HELLO?")); while (end_thread_out == false){ if (WaitForSingleObject(h_end_th_out,INFINITE) != WAIT_FAILED){ HRESULT hr = spApp->get_Selection(&oSel); oSel->TypeText(Some_Text); } } } Tomaz Rotovnik

      R Offline
      R Offline
      Ryan_Roberts
      wrote on last edited by
      #2

      Are you trying to access an STA from a different thread than the one that you created the object in?. In which case you are going to have to marshal a new proxy for the thread. Have a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_88tp.asp[^] and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_1rjh.asp[^]. Remember to call CoInitialize in your thread before doing anything with COM too. Ryan

      T 1 Reply Last reply
      0
      • R Ryan_Roberts

        Are you trying to access an STA from a different thread than the one that you created the object in?. In which case you are going to have to marshal a new proxy for the thread. Have a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_88tp.asp[^] and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_1rjh.asp[^]. Remember to call CoInitialize in your thread before doing anything with COM too. Ryan

        T Offline
        T Offline
        Tomaz Rotovnik
        wrote on last edited by
        #3

        Dear Ryan I tried but I do not know if I understand what should I do. void CMyAddin::StartProcOut() { IStream* M_stream; LPVOID* izh = NULL; CComPtr oSel; CComQIPtr spApp(m_pParentApp); ATLASSERT(spApp); _bstr_t Some_Text(OLESTR("HELLO?")); hr = CoInitialize(NULL); //return S_OK hr = CoMarshalInterThreadInterfaceInStream(IID_IDTExtensibility2,this,&M_stream); //return S_OK while (end_thread_out == false){ if (WaitForSingleObject(h_end_th_out,INFINITE) != WAIT_FAILED){ HRESULT hr = spApp->get_Selection(&oSel); oSel->TypeText(Some_Text); } } hr = CoGetInterfaceAndReleaseStream(M_stream,IID_IDTExtensibility2,izh); CoUninitialize(); } The CLSID_MyAddin is uniq identifier static const GUID IID_IDTExtensibility2 = {0xB65AD801L,0xABAF,0x11D0,{0xBB,0x8B,0x00,0xA0,0xC9,0x0F,0x27,0x44}}; class CMyAddin : public IDTExtensibility2 Is this correct? I still get: Unhandled exception in WINWORD.EXE 0xC0000005: Access Violation CMyAddin is created by class CMyAddinCF : public IClassFactory Tomaz Rotovnik

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups