0

I am going to search for updates with BeginSearch. ISearchCompletedCallback has been implemented in with way:

#pragma once

class ISCC : public ISearchCompletedCallback
{
public:
    ISCC()
    { }
    ~ISCC()
    { }

public:
    ULONG STDMETHODCALLTYPE ISCC::AddRef()
    {
        InterlockedIncrement(&refCounter_);
        return refCounter_;
    }
    ULONG STDMETHODCALLTYPE Release()
    {
        ULONG ulRefCount = InterlockedDecrement(&refCounter_);
        if (0 == refCounter_)
        {
            delete this;
        }
        return ulRefCount;
    }
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID *ppvObj)
    {
       if (!ppvObj)
          return E_INVALIDARG;
       *ppvObj = NULL;
       if (riid == IID_IUnknown || riid == IID_ISearchCompletedCallback)
       {
          *ppvObj = (LPVOID)this;
           AddRef();
           return NOERROR;
       }
       return E_NOINTERFACE;
    }
    virtual HRESULT STDMETHODCALLTYPE Invoke(
        ISearchJob *searchJob, ISearchCompletedCallbackArgs *callbackArgs)
    {
       std::cout << "Invoked" << std::endl;
       return S_OK;
    }

private:
    long refCounter_;
};

Also i created another class (Enumerator) where i placed method to start asynchronous search and some private data members which i need to use in my programm:

class Enumerator
{
public:
    Enumerator() { }
    ~Enumerator() { }

public:
    //! Begins execution of an asynchronous search for updates.
    void AsynchSearch(const _tstring& criteria = _T("IsInstalled=0"))
    {
       CComPtr<IUpdateSession> pUpSession;
       if (SUCCEEDED(CoCreateInstance(
           CLSID_UpdateSession,
           NULL,
           CLSCTX_INPROC_SERVER,
           IID_IUpdateSession,
          (LPVOID*)&pUpSession)))
       {
           if (SUCCEEDED(pUpSession->CreateUpdateSearcher(&pUpSearcher_)))
           {
               HRESULT ret;
               ret = pUpSearcher_->BeginSearch(
                   CComVariant(criteria.c_str()).bstrVal,
                   iscc_,
                   CComVariant(_T("Scanning")),
                   &pJob_);

               if (FAILED(ret))
               {
                  // I get next error in this place
                  // E_POINTER Invalid pointer. <<-- Error!!!

                  std::cout << "Error occured" << std::endl;
               }
           }
       }
   }

private:
    //! Asynchronous search callback.
    CComPtr<ISCC> iscc_;
    //! Job pointer.
    CComPtr<ISearchJob> pJob_;
    //! Searcher pointer.
    CComPtr<IUpdateSearcher> pUpSearcher_;
};

My problem i that when i use method BeginSearch i get error "Invalid pointer" (see method Enumerator::AsynchSearch). Looks like pointer CComPtr<ISCC> iscc_ has been initialized incorrectly. In which way should I initialize CComPtr<ISCC> iscc_ pointer? Thanks.

Space Rabbit
  • 141
  • 2
  • 11
  • 1
    That should really be a [CComObject](https://msdn.microsoft.com/en-us/library/aehcbwe0.aspx), initialized using [CComObject::CreateInstance](https://msdn.microsoft.com/en-us/library/9e31say1.aspx). It's not immediately clear, why you use ATL smart pointers to reference COM objects, but do not use ATL to implement your COM interface (by deriving from e.g. [CComObjectRootEx](https://msdn.microsoft.com/en-us/library/8hzca2fs.aspx)). No one should have to implement their own `QueryInferface` method, or reference counting. – IInspectable Sep 02 '16 at 09:42
  • So i need to inherit my callback implementation from `CComObjectRootEx` and `ISearchCompletedCallback` and i should initilize `CComPtr iscc_ pointer` with `CComObject::CreateInstance`. Is that correct? – Space Rabbit Sep 02 '16 at 09:56
  • 1
    That's one possible implementation (if you use `CComObject` instead of `CComPtr`). – IInspectable Sep 02 '16 at 10:03
  • @IInspectable ok, i will try this way and tell you know. – Space Rabbit Sep 02 '16 at 10:41

0 Answers0