Reputation: 65
I am using the GDI+ function Bitmap::FromStream()
to load a PNG resource, following the example provided in this article.
The Bitmap::FromStream()
function requires an IStream
as its first argument. Since IStream
is a COM interface, and my knowledge of COM programming is limited, I want to clarify:
Do I need to explicitly initialize COM (e.g., using CoInitialize
) before creating or using an object that implements IStream
for this function?
The sample code seems to work fine without explicitly initializing COM, but I’m not sure if this is the correct or reliable approach. Could this lead to issues under certain conditions?
Any insight into whether COM initialization is necessary in this context would be greatly appreciated.
Edit:
This question is not a duplicate.
In the other question, the application fails outright without calling CoInitialize()
, and the focus is on understanding why it is required.
However, in my case, the application works without explicitly initializing COM, and my concern is whether this is safe or if it might lead to issues under certain conditions. My question seeks clarification on whether explicit COM initialization is necessary when using COM interfaces and the potential implications of skipping the initialization.
Upvotes: 3
Views: 129
Reputation: 597906
You do not need to initialize COM if you define your own class that implements a COM interface for use in your own code.
You do need to initialize COM if you create a COM object via a system factory, or pass around your object outside of your exe.
For example, if you use CreateStreamOnHGlobal()
or SHCreateMemStream()
to create the IStream
object then you should initialize COM first. If you implement the IStream
in your own class then you don't need to.
Upvotes: 2