Re: ATL threading problem using client COM interface

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Igor Tandetnik wrote:
"Edward Diener" <eddielee_no_spam_here@xxxxxxxxxxxxxx> wrote in message
news:O90YIBZBJHA.5468@xxxxxxxxxxxxxxxxxxxx
Igor Tandetnik wrote:
"Edward Diener" <eddielee_no_spam_here@xxxxxxxxxxxxxx> wrote in
message news:OJBs59UBJHA.4108@xxxxxxxxxxxxxxxxxxxx
The thread needs to use a client COM interface in order to do its
background processing and that client COM interface is passed to the
thread as its data when the thread is created.
You can't just pass COM interface pointers across threads. You have
to marshal them. See CoMarshalInterThreadInterfaceInStream,
CoGetInterfaceAndReleaseStream. Or IGlobalInterfaceTable, CComGITPtr.
So what you are saying is that in my COM server, when I want to pass a
client interface pointer from the main thread in my STA to my worker
thread, I need to use one of the techniques above ?

Yes. You would also need to initialize COM on your worker thread - it looks like CWorkerThread doesn't itself do that.

My COM Server is a DLL. It does not appear that initializing COM in a DLL is correct. But perhaps I am wrong about this.

Realize this. The client object you want to call from the worker thread presumably lives in STA. Which means that it's not thread-safe. You can marshal its interface pointer to a worker thread, but any call this thread makes on this pointer is marshalled right back to the object's native STA thread, executed there, and any results marshalled back. This is the whole point of STA - to guarantee that COM objects living in it are only ever called on one thread (which allows them to be thread-unsafe).

Yes, I understand this.


Thus, if you hope to gain any performance improvements by spinning a worker thread, you will likely be disappointed. All you achive is adding overhead.

I am not spinning off a worker thread for performance improvement.

What I achieve is the ability to let the client execute while my worker thread is doing what is necessary in the background. This is necessary for me in order for the worker thread to update the client's interface while it is processing. In particular my worker thread is creating a separate window for information to the end user while processing and unless I am able to do my processing in my COM server in the background I can never update that separate window while my processing occurs unless I am using a background thread.


Do any of the COM
smart pointers ( CComPtr, CComQIPtr, or _com_ptr_t ) provide an easy
way to marshall the underlying interface pointer from one thread on
the server to another ?

Which part of "CComGITPtr" did you find unclear?

That class deals with the global interface table. If the interface is created by the client and passed to me on the server is it valid to add that interface to the GIT so that my different server threads can use it in place of the marshalling functions you also mentioned ?

I also realized, if I am not mistaken, that I could use free threading rather than apartment threading, if I was willing to do all my own synchronization, and I should then not have to worry about marshalling of an interface between my threads. Is that correct ?
.



Relevant Pages

  • Re: In-process cross-thread (cross-apartment) marshaling
    ... you register the interface marshaling code.) ... object (ask for IUnknown with the IID of your interfrace as the ... Marshalling an IUnknown and unmarshalling an IUnknown is successful, ...
    (microsoft.public.win32.programmer.ole)
  • Re: Notify multiple instances of my COM dll to take an action
    ... at the dll level and type library marshalling at the exe level right? ... passing the pointer into the exe but the method call on that pointer ... Again - you don't have marshaling support for the callback interface ...
    (microsoft.public.vc.atl)
  • Re: Marshalling of VB6 variant containing an array
    ... It was not a marshalling issue ... I have a problem using variant parameters in C#. ... I have interface defined in VB6. ... Optional vParam As Variant) ...
    (microsoft.public.dotnet.framework.interop)
  • Re: A puzzling question
    ... If you have a remotable interface, ... the proxy implement AddRef/Release for the interface ... there's no real need to call AddRef/Release across the ... When marshalling the marshaller needs to ...
    (microsoft.public.vc.atl)
  • Re: IHTMLDocument2 between threads
    ... You are passing an interface pointer from STA to MTA ... document interface in the Sink object. ... I tried to use marshalling for this purpose by using ...
    (microsoft.public.vc.atl)