COM Apartment

When a thread calls CoInitialize(NULL) or CoInitializeEx(NULL,COINIT_APARTMENTTHREADED), it creates a new STA apartment. The creating thread is said to be in the apartment. When the thread later instantiates a COM object, the COM object is said to be in the apartment(this is only valid when the COM dll has a value “ThreadingModel=Apartment” in the registry). The first STA is called main STA. The COM object without  the value “ThreadingModel=Apartment” in the registry will be put into the main STA whichever thread created it. A process has only one main STA and may have multiple other STAs. A thread can only  be in one apartment.

A thead calls CoInitializeEx(NULL, COINIT_MULTITHREADED) to create a MTA, and the thread is said to be in the MTA. A process can have at most one MTA. Other threads calling CoInitializeEx(NULL, COINIT_MULTITHREADED) join the MTA instead of creating additional MTAs. The COM objects with ThreadingModel=Free in registry will be put into the MTA no matter how the thread creating it initializes the COM lib. If no MTA is available when creating the COM object with ThreadingModel=Free in registry, the COM lib will create a host MTA to host the COM.

A COM object with ThreadingModel=Both in registry will be put in its creating thread’s apartment. So, if its creating thread is in a STA, the COM object is in that STA. If the creating thread is in the MTA, the COM object is in that MTA.

If a MTA thread instantiates a COM object  with ThreadingModel=Apartment, the COM lib will create a new HOST STA (i.e., create a new thread which calls CoInitializeEx(NULL,COINIT_APARTMENTTHREADED)) and instantiate the COM object in that new thread.

The methods of a COM object in an apartment can be called directly by the thread(s) in the same apartment. The calls to the methods of COM objects in other apartments different than the apartment of the calling thread are marshaled.

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply