2

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

Callum
  • 52
  • 1
  • 7
  • `pAutomaticUpdatedResults` isn't uninitialized. It is totally initialized. Though dereferencing a null pointer certainly isn't prudent. You're going to have to get familiar with [COM](https://learn.microsoft.com/en-us/windows/win32/com/component-object-model--com--portal). – IInspectable Jan 15 '21 at 10:43

1 Answers1

5

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;
}
The Om
  • 369
  • 1
  • 2
  • 8