thread in COM for Office Addin
-
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
-
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
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
-
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
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