How to make a COM object method run not in the main thread

I have a windows service that implements a COM COM server.

When launched as an application, the methods of a COM object are called in a separate (not in the main) thread, which is just fine. Things change when the service starts - then the methods of the COM object are called in the context of the service flow, which is not cool for me.

I see that this is due to apartments, MTA, STA, etc., but I can’t figure out how to get COM to call object methods in a separate thread, and not in a service.

Maybe this is due to the registration of the com object when the service starts?

My environment is Windows 7 + delphi, but C ++ solutions are welcome.

Update 2011-04-26 :
Kudos to @sharptooth and @Chris Dickson, which made me look for a solution in the direction of message loops.

Since this is an STA, the application uses a message pump to deliver COM messages to the thread that registered the class. I have moved the registration of soklafs to a separate thread that have a message loop, and all com calls are now made in this thread. I tried this approach before, but forgot about the message pipeline, so this was the missing piece of the puzzle. Thanks guys!

+4
source share
1 answer

Objects will be called depending on the thread with which you registered them. If you want objects to be called in a separate apartment (STA), you must register class objects from that apartment. This can be achieved by creating a separate stream and registering from there.

+1
source

Source: https://habr.com/ru/post/1342854/


All Articles