A third suggestion from Luca Bacci on creating a fake agile wrapper that is technically agile but is not useful outside its home apartment was to use the COM static store.
The COM static store doesn’t help us, because the COM static store runs down when the last thread shuts down COM, whereas we want something that runs down when a single apartment shuts down COM.
There is a per-apartment version of the COM static store, but it is part of the CoreApplicationView, which is (1) something that only UWP programs have, and (2) exists only for apartments that have a core application view, and those are restricted to single-threaded apartments.
Even if there were a per-apartment static store, putting something into it would require generating a unique name and remembering it for later removal. You could probably use the stringified address of the object, with a fixed GUID prefix as a pseudo-namespace. Since we’re dealing with a dictionary, the insertion and removal operations are going to be O(log n) or O(1), depending on the implementation, but even the constant-time version will have a higher constant than a single index lookup in the Global Interface Table.
Thanks for the deep dive! 🍻
Don't know why, but last I tried the COM static store (runtime class "Windows.ApplicationModel.Core.CoreApplication") was accessible from an unpackaged, Win32 application. It also seems to tie entries to the originating apartment lifetime (namely: the apartment from which the property is added). Can you shed some light?
The whole point about alternative solutions is that MSDN says: "The apartment that calls [RegisterInterfaceInGlobal] must remain alive until the corresponding call to RevokeInterfaceFromGlobal". That sounds like it's an error to keep entries in the GIT after apartments run down; but anyway, happy to hear that it works!
Thank you,
Luca
Well, the static store in CoreApplication is documented as being available for desktop applications. From the CoreApplication Remarks section:
“This API is supported in Universal Windows Platform apps only, except for the Properties property, which is always available.”
So it isn’t surprising that the CoreApplication store is available. But Raymond never stated that this store wasn’t available. He only stated that the store in CoreApplicationView is unavailable.
Thanks, that clarifies things. However the COM static store ties value lifetimes to their apartments just like the GIT. I think I get where the confusion comes from: Raymond talks about lifetime of the store itself and I talk about lifetimes of entries. So:
• CoreApplication is a process-wide singleton, and is kept alive as long as there’s a COM apartment in the process
• CoreApplicationView is a per-apartment singleton, and thus same lifetime as the apartment
• CoreApplication property values have lifetimes tied to the originating apartment
Kind regards,
Luca