Iterating through a COM collection using the IObjectArray interface
Every so often I need to write some code that uses COM interfaces, but since I do so rather infrequently I often forget the COM reference counting rules. The rules are documented on MSDN but the documentation could really do with some basic examples, and finding such examples elsewhere can be rather difficult in my experience (perhaps they’re all in paper books). So this post is going to present one such example, primarily to help my future forgetful self.
Here’s a function that iterates through the objects an IObjectArray and invokes a callback
function for each object:
|
|
The same function could be written using CComPtr, though there really isn’t much point in doing
so in this particular case:
|
|
And here’s an example of using the function implemented above:
|
|