What is the equivalent of Thread.SetApartmentState in C++?

In C# there is a method SetApartmentState in the class Thread. How do I do the same thing in C++?

Upvotes: 6

Views: 2420

Answers (1)

Shog9
Shog9

Reputation: 159688

For unmanaged processes, you control the apartment model used for a thread by passing appropriate parameters to CoInitializeEx(). Larry Osterman wrote up a great little guide to these:

...
When a thread calls CoInitializeEx (or CoInitialize), the thread tells COM which of the two apartment types it’s prepared to host. To indicate that the thread should live in the MTA, you pass the COINIT_MULTITHREADED flag to CoInitializeEx. To indicate that the thread should host an STA, either call CoInitialize or pass the COINIT_APARTMENTTHREADED flag to CoInitializeEx.
...

-- https://learn.microsoft.com/en-us/archive/blogs/larryosterman/what-are-these-threading-models-and-why-do-i-care

Upvotes: 10

Related Questions