admin管理员组

文章数量:1208153

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.

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.

Share Improve this question edited Jan 20 at 4:03 Remy Lebeau 596k36 gold badges497 silver badges838 bronze badges asked Jan 19 at 17:30 semicolonsemicolon 655 bronze badges 3
  • 1 short answer : if it works, don't add any CoInitialize(Ex) call – Simon Mourier Commented Jan 19 at 18:02
  • 4 That's really bad advice. See Why does CoCreateInstance work even though my thread never called CoInitialize? The curse of the implicit MTA for why. – IInspectable Commented Jan 19 at 18:47
  • 2 There's no CoCreateInstance here as GDI+ doesn't expose any cocreatable COM object. Adding a useless CoInitialize(Ex) poses the risk of a future STA vs MTA mismatch when COM will be really needed. – Simon Mourier Commented Jan 19 at 19:36
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: cDo I need to initialize COM before using IStream with GDI BitmapFromStream()Stack Overflow