我需要在另一个单元(STA)线程里激发引出接口的某个方法,所以需要调度引出接口的指针CoMarshalInterThreadInterfaceInStream这个函数就专门干这活的,但是老是不成功!!!

解决方案 »

  1.   

    first look up CoMarshalInterThreadInterfaceInStream in msdn,it use CoGetInterfaceAndReleaseStream to unmarshal Interface in the stream.
    then you now how to do.
    1. create a IStream ,do not write something in it
    eg.
    ::CoInitialize(NULL);
    HRESULT hr;
    CComPtr<IStream> pStream;
    hr=pStream.CoCreateInstance(CLSID_Stream);
    2.then marshal your own interface to the stream
     
     hr=CoMarshalInterThreadInterfaceInStream(IID_YOUWANT_MARSHAL_INTERFACE,pYourObj,pStream);
    if hr=0,then you can pass the pStream to the other thread 
    3.unmarshal you obj from the stream
    eg.
     UINT Myfunc(void p)
    {
      //if the p is the stream pointer
      CComPtr<IID_YOUWANT_MARSHAL_INTERFACE> pYourObj;
      hr=CoGetInterfaceAndReleaseStream((IStream*)p 
    ,IID_YOUWANT_MARSHAL_INTERFACE,(void**)pYourObj);
     //the you have the pointer to you obj in the other thread,do not care about the release of IStream p
    }
    I just use the two function pass interface pointer cross thread and not test cross process ,that's long long ago and the source code is lost.this code maybe contain error because i write it here not copy it to here.the stream implement you can find in many component.eg the ado .to use it just use 
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" named_guids,rename("EOF","adoEOF")
    using namespace ADODB;
      

  2.   

    fantong(饭桶)兄:
    请问如何把接口指针作为方法的参数传递,要用CoMarshalInterThreadInterfaceInStream,CoGetInterfaceAndReleaseStream吗?
      

  3.   

    fantong(饭桶)兄:
    你的代码我试过了,还是不行,
    另外
    hr=CoMarshalInterThreadInterfaceInStream(IID_YOUWANT_MARSHAL_INTERFACE,pYourObj,pStream);
    应该为
    hr=CoMarshalInterThreadInterfaceInStream(IID_YOUWANT_MARSHAL_INTERFACE,pYourObj,&pStream);
    我想问你一个问题,msdn里面说CoMarshalInterThreadInterfaceInStream
    这个函数已经包括创建Stream实例的工作,但是在你的代码中还是自己创建了实例,
    拜托请回忆一下你的源代码,帮帮忙啦
      

  4.   

    to:yangfanfan
    嗬嗬,&号纯属笔误
    IStream 上次确实是我自己创建的,
    偶,再看了看自己创建IStream是脱裤子放屁,这是[out]参数嘛
    代码不要赵抄阿,我只是说了这个流程,这些代码多半编译都通不过,
    但是这个方法确实是可以的,我用它在一个窗口线程中创建的com中向另外一个工作线程传递一个接口参数,唉,说不清楚,画画图
    (windowA (MycomB(functionC(),thread D())))
    我在functionC中创建了MycomE,再将MycomE打包传递到thread D 中使用
    这个办法是可以的
    后来我舍弃了这段代码改用别的方法,所以代码没了