WinUI3 : Understating COM apartments and objects in WinUI3 application

Harshithraj1871 1,686 Reputation points
2023-02-27T09:03:29.7+00:00

Hi,

I'm working on the WinUI3 desktop application in CPP. I have disabled the XAML generation of main function and wrote my own main function like this.

int CALLBACK
WinMain([[maybe_unused]] HINSTANCE pInstance, [[maybe_unused]] HINSTANCE pPrevInstance, [[maybe_unused]] LPSTR pCmdLine, [[maybe_unused]] int pShowCmd) {

    winrt::init_apartment(winrt::apartment_type::single_threaded);

    // Start Event loop
    Application::Start([&](auto&&) {

        make<App>();
        });

I read about COM apartments and objects from this documentation - https://learn.microsoft.com/en-us/windows/win32/com/single-threaded-apartments, but I'm not able to relate what an object and apartment are in a WinUI application.

Is each UIElement a COM object? We are initializing with the STA threading model, does the default WinUI3 project (the default project which gets created from the visual studio template) have only one apartment which we initialize?

It would be of great help if you could help me understand what the COM objects and apartments are in the WinUI3 application.

Thank you

Windows development | Windows App SDK
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2023-02-28T03:03:07.77+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Is each UIElement a COM object?

    COM is a technology that allows objects to interact across process and computer boundaries as easily as within a single process. COM enables this by specifying that the only way to manipulate the data associated with an object is through an interface on the object.

    In my opinion, not each UIElement is a COM object.

    We are initializing with the STA threading model, does the default WinUI3 project (the default project which gets created from the visual studio template) have only one apartment which we initialize?

    The simplest way to view the COM threading architecture is to think of all the COM objects in the process as divided into groups called apartments. A COM object lives in exactly one apartment.

    An apartment is an environment in which COM objects can live. It’s not a thread, nor a process, COM apartments exist to ensure thread safety. It is so abstract.

    Single-threaded Apartment model (STA): One or more threads in a process use COM and calls to COM objects are synchronized by COM. Interfaces are marshaled between threads. A degenerate case of the single-threaded apartment model, where only one thread in a given process uses COM, is called the single-threading model.

    Single-threaded apartments consist of exactly one thread.

    For more details, I suggest you could refer to the Docs:

    Processes, Threads, and Apartments

    COM Objects and Interfaces

    The Component Object Model

    Thank you.

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.