Skip to content

How to control process lifetime if implementing a COM EXE? #1493

@forderud

Description

@forderud

Version

2.0.250303.1

Summary

I've just started modernizing some existing ATL projects to WinRT. One challenge then faced has been how to control process lifetime for a EXE-based COM server.

ATL (atlbase.h) seem to control process lifetime by first using CoAddRefServerProcess()/CoReleaseServerProcess() to keep track of the number of living COM objects. SetEvent(m_hEventShutdown) is afterwards called to set a shutdown event when the object count drops to zero. This event triggers posting of a WM_QUIT message that ends the message-loop and terminates the COM server process.

I'm aiming to replicate this behavior with WinRT, but have been unable to find any similar building blocks in the WinRT library. The closest thing I've seen have been winrt::get_module_lock(), but it only solves a part of the problem. I'm still faced with the following remaining subproblems:

  1. There's no elegant way to wait for the module lock count to drop to zero, except for checking it periodically in a for-loop.
  2. The class factory object seem to be included in the count, which causes problems since the class factory seem to outlive the client. I can compensate for this with a while (get_module_lock() >= 1) Sleep(1000); check, but I'm not sure if its's safe to assume that the class factory always outlives the client.
  3. I also need some kind of "have any object been created" check to prevent premature process termination before any COM objects have been created.

It would be great if the C++/WinRT library could be extended with building blocks similar to the ones in ATL to ease lifetime control for EXE-based COM servers.

Reproducible example

Expected behavior

No response

Actual behavior

No response

Additional comments

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions