Exception from HRESULT: 0x80240007 when querying Windows Updates

1

I am getting the error "Exception from HRESULT: 0x80240007" when I am trying to fetch the installed Windows updates. My code worked well in Windows 7, but it's not working in Windows XP. I am getting the error in the line var history = updateSearcher.QueryHistory(0, count);

This is my code snippet:

        var updateSession = new UpdateSession();
        var updateSearcher = updateSession.CreateUpdateSearcher();
        var count = updateSearcher.GetTotalHistoryCount();
        var history = updateSearcher.QueryHistory(0, count);

What changes do I need to make in the code?

c#
windows
windows-update
asked on Stack Overflow Nov 8, 2012 by swati • edited Mar 10, 2021 by riQQ

1 Answer

6

0x80240007 is the error code WU_E_INVALIDINDEX as defined in wuerror.h:

// MessageId: WU_E_INVALIDINDEX
//
// MessageText:
//
// The index to a collection was invalid.
//
#define WU_E_INVALIDINDEX                _HRESULT_TYPEDEF_(0x80240007L)

And the call to UpdateSession.CreateUpdateSearcher.QueryHistory boils down to IUpdateSearcher::QueryHistory and its documentation says:

Remarks
This method returns WU_E_INVALIDINDEX if the startIndex parameter is less than 0 (zero) or if the Count parameter is less than or equal to 0 (zero).

count is most likely not less than 0 but maybe ==0

You need something like

var count = updateSearcher.GetTotalHistoryCount();
var history = count > 0 ? updateSearcher.QueryHistory(0, count) : null;

(or a more complex case handling....)

answered on Stack Overflow Nov 8, 2012 by VolkerK • edited Nov 9, 2012 by VolkerK

User contributions licensed under CC BY-SA 3.0