我希望有人可以帮助解决这个问题。我也希望这个问题有一个简单的答案。我觉得我遗漏了一些非常明显的东西,但我是 C++ 新手,无法解决这个问题。
我想将 IUpdateCollection 传递给函数,将 IUpdates 放入集合中,然后能够在函数之外访问集合。在下面的代码中,一切都编译/运行,但是在 Searcher 函数内部时 IUpdateCollection 中的项目计数为 5,但是当我稍后尝试从函数外部计算 IUpdateCollection 中的项目时,计数为 0。
我在这里想念什么?
谢谢!
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;
}