NO

Author Topic: wuapi test  (Read 4927 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
wuapi test
« on: June 01, 2018, 01:01:09 PM »
This code checks uninstalled updates:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ole2.h>
#include <wuapi.h>

#pragma comment(lib, "ole32")
#pragma comment(lib, "oleaut32")
#pragma comment(lib, "msvcrt")

int printf(const char * format, ...);

const CLSID CLSID_UpdateSession={0x4CB43D7F,0x7EEE,0x4906,0x86,0x98,0x60,0xDA,0x1C,0x38,0xF2,0xFE};
const IID IID_IUpdateSession={0x816858A4,0x260D,0x4260,0x93,0x3A,0x25,0x85,0xF1,0xAB,0xC7,0x6B};

void ProcessList(ISearchResult* pResults);

void __cdecl mainCRTStartup(void)
{
HRESULT hr;
hr = CoInitialize(NULL);
IUpdateSession* pUpdate;
hr = CoCreateInstance(&CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, &IID_IUpdateSession, (LPVOID*)&pUpdate);
if (pUpdate)
{
IUpdateSearcher* pSearcher;
ISearchResult* pResults;
hr = pUpdate->lpVtbl->CreateUpdateSearcher(pUpdate, &pSearcher);
BSTR bstrCriteria = SysAllocString(L"IsInstalled=0");
printf("Searching updates...\n");
hr = pSearcher->lpVtbl->Search(pSearcher, bstrCriteria, &pResults);
SysFreeString(bstrCriteria);
if (pResults) {
ProcessList(pResults);
pResults->lpVtbl->Release(pResults);
}
pUpdate->lpVtbl->Release(pUpdate);
}
CoUninitialize();
ExitProcess(0);
}
void ProcessList(ISearchResult* pResults)
{
IUpdateCollection *pUpdateList;
LONG nCount;
pResults->lpVtbl->get_Updates(pResults, &pUpdateList);
pUpdateList->lpVtbl->get_Count(pUpdateList, &nCount);
if (nCount) {
IUpdate *pItem;
printf("Count: %d\n", nCount);
for (int i = 0; i< nCount; i++) {
BSTR bstrName;
VARIANT_BOOL vb;
pUpdateList->lpVtbl->get_Item(pUpdateList, i, &pItem);
pItem->lpVtbl->get_Title(pItem, &bstrName);
pItem->lpVtbl->get_IsHidden(pItem, &vb);
printf("%ls %s\n", bstrName, vb?"Hidden":"");
SysFreeString(bstrName);
pItem->lpVtbl->Release(pItem);
}
}
pUpdateList->lpVtbl->Release(pUpdateList);
}
« Last Edit: June 03, 2018, 10:22:12 PM by TimoVJL »
May the source be with you

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: wuapi test
« Reply #1 on: June 01, 2018, 08:04:06 PM »
Hi Timo,

Very nice work. I will check it.
Code it... That's all...

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: wuapi test
« Reply #2 on: June 02, 2018, 03:10:27 AM »
Hi Timo,

It builds fine and runs ... and runs ... and runs - it really takes a little while until you see results. But it works fine - thumbs up!

I suggest adding #pragma warn(disable:2216)      // retval never used to suppress some useless warnings.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: wuapi test
« Reply #3 on: June 02, 2018, 08:41:23 AM »
Query from history is fast.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ole2.h>
#include <wuapi.h>

#pragma comment(lib, "ole32")
#pragma comment(lib, "oleaut32")
#pragma comment(lib, "msvcrt")

int printf(const char * format, ...);

const CLSID CLSID_UpdateSession={0x4CB43D7F,0x7EEE,0x4906,0x86,0x98,0x60,0xDA,0x1C,0x38,0xF2,0xFE};
const IID IID_IUpdateSession={0x816858A4,0x260D,0x4260,0x93,0x3A,0x25,0x85,0xF1,0xAB,0xC7,0x6B};

void ProcessList(ISearchResult* pResults);

void __cdecl mainCRTStartup(void)
{
HRESULT hr;
hr = CoInitialize(NULL);
IUpdateSession* pUpdate;
hr = CoCreateInstance(&CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, &IID_IUpdateSession, (LPVOID*)&pUpdate);
if (pUpdate)
{
IUpdateSearcher* pSearcher;
IUpdateHistoryEntryCollection* pHistory;
hr = pUpdate->lpVtbl->CreateUpdateSearcher(pUpdate, &pSearcher);
BSTR bstrCriteria = SysAllocString(L"IsInstalled=0");
printf("Searching updates...\n");
LONG nCount;
hr = pSearcher->lpVtbl->GetTotalHistoryCount(pSearcher, &nCount);
hr = pSearcher->lpVtbl->QueryHistory(pSearcher, 0, nCount, &pHistory);
SysFreeString(bstrCriteria);
if (pHistory) {
for (int i = 0; i< nCount; i++) {
IUpdateHistoryEntry* pEntry;
pHistory->lpVtbl->get_Item(pHistory, i, &pEntry );
if (pEntry) {
BSTR bstr;
pEntry->lpVtbl->get_Title(pEntry, &bstr);
DATE date;
SYSTEMTIME st;
pEntry->lpVtbl->get_Date(pEntry, &date);
VariantTimeToSystemTime(date, &st);
printf("%ls %d-%02d-%02d\n", bstr, st.wYear, st.wMonth, st.wDay);
SysFreeString(bstr);
pEntry->lpVtbl->Release(pEntry);
}
}
//ProcessList(pResults);
pHistory->lpVtbl->Release(pHistory);
}
pUpdate->lpVtbl->Release(pUpdate);
}
CoUninitialize();
ExitProcess(0);
}
« Last Edit: June 03, 2018, 10:10:09 PM by TimoVJL »
May the source be with you