誰かがこの質問を手伝ってくれることを願っています。また、この質問に簡単な答えがあることを願っています。非常に明白な何かが欠けているように感じますが、私は 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;
}