c++windowswinapiwuapi

Get last windows update check C++ wuapi


I'm trying to get the last windows update check using wuapi. I have the following code:

VARIANT variant;
VariantInit(&variant);

IAutomaticUpdatesResults* pAutomaticUpdatedResults = NULL;
if (pAutomaticUpdatedResults->get_LastSearchSuccessDate(&variant) != S_OK)
  throw;

std::cout << variant.date << std::endl;

Understandably I get an exception that pAutomaticUpdatedResults is uninitialized but I'm not sure on the correct way to use the wuapi


Solution

  • You can obtain an IAutomaticUpdatesResults* from IAutomaticUpdates2::get_Results.

    IAutomaticUpdates2 is an interface implemented by the AutomaticUpdates coclass.

    Full sample:

    #include <Windows.h>
    #include <wuapi.h>
    #include <iostream>
    
    
    int main() {
        HRESULT hr;
    
        hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
        if(FAILED(hr))
            return hr;
    
        IAutomaticUpdates2* iupdates = nullptr;
        hr = CoCreateInstance(
            CLSID_AutomaticUpdates,
            nullptr,
            CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER | CLSCTX_INPROC_SERVER,
            IID_PPV_ARGS(&iupdates)
        );
    
        IAutomaticUpdatesResults* results = nullptr;
        if(SUCCEEDED(hr)) {
            hr = iupdates->get_Results(&results);
        }
    
        DATE lastSearchSuccessDate = 0.0;
        if(SUCCEEDED(hr)) {
            VARIANT var;
            VariantInit(&var);
            hr = results->get_LastSearchSuccessDate(&var);
            lastSearchSuccessDate = var.date;
        }
    
        if(SUCCEEDED(hr)) {
            std::cout << lastSearchSuccessDate << std::endl;
        }
    
        if(results)
            results->Release();
        if(iupdates)
            iupdates->Release();
    
        CoUninitialize();
    
        return hr;
    }