I’m hoping someone can help with this question. I’m also hoping that this question has a simple answer. I feel like I’m missing something very obvious, but I’m new to C++ and have been unable to get past this issue.
I want to pass an IUpdateCollection to a function, put IUpdates into the collection, and then be able to access the collection outside of the function. In the code below, everything compiles/runs, but the count of items in the IUpdateCollection is 5 while inside the Searcher function, but when I later try to count the items in the IUpdateCollection from outside the function, the count is 0.
What am I missing here?
Thanks!
class W
{
public:
// constructor
W()
{
//create the COM object to return the IUpdateSession interface pointer
hr = CoCreateInstance( )
}
int Searcher(IUpdateCollection* pUpdateCollection)
{
//put the updates into our pUpdateCollection
hr = pSearchResult->get_Updates(&pUpdateCollection);
if(FAILED(hr) || pUpdateCollection == NULL)
{ cout << "Failed to put updates in the collection"; return -103; };
long lUpdatesCount = NULL;
hr = pUpdateCollection->get_Count(&lUpdatesCount);
if(FAILED(hr) || pSearchResult == NULL)
{ cout << "Failed to get count of udpates in collection"; return -104; };
cout << lUpdatesCount << endl; //console outputs the actual count here, which at the moment is 5
pUpdateSearcher->Release();
return 0;
}
private:
HRESULT hr;
IUpdateSession* pUpdateSession;
};
int main(int argc, char* argv[])
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
HRESULT hr;
W myW;
//pass the pUpdateCollection to the W.Searcher function
myW.Searcher(pUpdateCollection);
//get count of updates in collection
long lUpdatesCount = NULL;
pUpdateCollection->get_Count(&lUpdatesCount);
cout << lUpdatesCount << endl; //console outputs 0 here instead of the same count that it outputted in W.Searcher(). WHY?
CoUninit();
system("pause");
return 0;
}
Use smart pointers like
_com_ptr_tand_bstr_t. USing the raw pointers and directly operating BSTRs is nothing but pain.#import-ing the COM DLL will create the typed smart pointers for you, including easy to use CreateInstance methods. Smart pointer also eliminate the need to explicitly check for HR after each call, they raise exceptions.As to your question: it depends on the implementation/specification/documentation of your COM object. Perhaps releasing the
IUpdateSearcherclears up the count, but is just speculation on my part. The relevant code would be the COM server, not the client.didn’t notice this is WUA, documented behavior
ISearchResult::Updatesassigns the IUpdateCollection. Therefore you are passing in a value of the pointer, change it in the function scope, then expect the change to apply outside the scope. The very same thing you’re doing, but using int:You would solve the ‘problem’ for
intby passing by ref or a pointer. Same applies to your COM pointer. And using _com_ptr_t would have clearly showed the issue 🙂