From several answers on this site, I found out what CoInitialize(Ex)should be called by the thread creator . Then, any code running in this thread can use COM. If this code calls the call itself CoInitialize(Ex), it would be harmless because it would have no effect. It should not be called CoUninitialize- this should also be done by the creator of the stream, but it will not be if it checks the (stored) result CoInitialize(Ex)that will be S_FALSE. If the creator did not take responsibility for the initialization, the thread will be the “grace” of this code to select a suitable thread model, which from then on will not change.
What are the implications of all this for writing and using libraries?
When all the code is your own and you have a small command, you can easily organize COM (un) initialization calls. However, with libraries, the user does not need to know how they do what they do, for example. that COM is involved. I would not want to have in the documentation what can be solved in the code. In addition, you should not make any assumptions about what thread will be executed in the library code, unless it concerns VCL code.
In most open source libraries, I checked the call CoInitialize(Ex)in the section initializationand CoUninitializein the section finalization, often without even checking if the initialization was successful. Some calls InitProcinstead, but some check first IsLibrary.
What should they really do? What if I write the unit myself, that I want someone to be able to use it without much consideration? Wrap everything about COM in a stream, and ask this thread to do its own initialization of COM (un)?
, , ? VCL COM (un) , Forms.TApplication.Create. , ? , - .dpr Forms? , VCL, DLL? , ? - , , COM ?
, : () COM (un)?