views:

62

answers:

3

When you have an out of process COM Server and you call a function from a Client inside this server from Thread X inside the client, then how this function get executed in the COM Server?

In the thread its currently executing on, or on its main thread?

+1  A: 

Threads do not jump from process to process.

Inside the COM Server, COM listens for incoming method calls and has a pool of threads (specific to this process) to serve the request.

Timores
So the code in the comm server has to be made threadsafe by the developer or is that already taken care of by the system?
Tony
This is configurable in your COM server: either you ask COM to serialize all requests, or you tell COM that you will take care of multi-threading (e.g. with locks) and requests will not be serialized.
Timores
+1  A: 

See Inter-Object Communication, Proxy and Stub.

A client always calls interface methods in some in-process object. If the actual object is local or remote, the call is made to a proxy object, which then makes a remote procedure call to the actual object.

So what method is actually executed? The answer is that whenever there is a call to an out-of-process interface, each interface method is implemented by a proxy object. The proxy object is always an in-process object that acts on behalf of the object being called. This proxy object knows that the actual object is running in a local or remote server.

The proxy object packages up the function parameters in some data packets and generates an RPC call to the local or remote object. That packet is picked up by a stub object in the server's process on the local or a remote computer, which unpacks the parameters and makes the call to the real implementation of the method. When that function returns, the stub packages up any out-parameters and the return value and sends it back to the proxy, which unpacks them and returns them to the original client.

Thus, client and server always talk to each other as if everything was in-process.

Bill
+2  A: 

Normal COM apartment threading rules are observed. If the object was created by the client in an STA apartment then your client thread need to use a marshaled interface pointer or it gets RPC_E_WRONG_THREAD. The actual method call will execute on the server in its STA thread, it needs to pump a message loop for that to work. Execution is serialized, no locking should be needed.

If it lives in the MTA apartment then the method call will execute on an arbitrary RPC worker thread. And you'll need to take the usual threading precautions.

Hans Passant
Hmmm, the question is about an out-of-proc server, so marshaling will always occur. I think your answer is more related to an in-proc server, although the threading config of both client and server need to be identical to prevent marshaling.
Timores