2 Replies Latest reply on Sep 10, 2018 7:50 PM by Sandor Racz

    Multithreading COM c++ addin

    Sandor Racz

      From a button press on my CDialog I create a thread. Then I try calling get_IActiveDoc2 from my ISldWorks ptr, but the ActiveDoc always comes back as junk.

      CoInitialize always returns S_OK. Anyone know why this is happening?   

       

      Thanks for any help!

       

      STDMETHODIMP Exporter::ToolbarCallback0(void)

      {

           AFX_MANAGE_STATE(AfxGetStaticModuleState());

           if(m_dlg == NULL)

           {

                  m_dlg = new MyDialog(this, AfxGetMainWnd());

                  m_dlg->Create(MyDialog::IDD);

            }

           m_dlg->ShowWindow(SW_SHOW);

      }

       

      MyDialog::ButtonEvent()

      {

           ...

           CreateThread(NULL, 0, ExportThread, this,  0,  &dwThreadIdArray);

      }

       

      DWORD WINAPI ExportThread(LPVOID pParam)

      {

           HRESULT result =  ::CoInitialize(0);

           MyDialog* dialog = (MyDialog*) pParam;

           CComPtr<ISldWorks> iSwApp = dialog ->GetSldWorksPtr();

          

           CComPtr<IModelDoc2> swModel;

           iSwApp->get_IActiveDoc2(&swModel);

           //swModel is NULL

      }