ENTERPRISE

Windows 7 : Interacting with the Built-In Security Features - WORKING WITH AUTOMATIC UPDATES (part 2)

2/27/2014 1:45:30 AM

3. Writing the Update Code

Once you know whether the user's system is healthy from an update check and download perspective, you need to know which updates the user has actually installed. For example, you might worry that your application won't work properly without the daylight saving time and time zone updates found in the Knowledge Base article at http://support.microsoft.com/kb/2158563. Using the technique shown in Listing 2, you can obtain the complete list of updates and search it for the appropriate update.

Example 2. Obtaining a list of installed updates
private void btnUpdates_Click(object sender, EventArgs e)
{
// Clear the list box.
lstResults.Items.Clear();

// Create the Microsoft Update Searcher (MUS) type.
Type MUStype = Type.GetTypeFromProgID("Microsoft.Update.Searcher");

// Use the MUS type to create a searcher object.
dynamic Searcher = Activator.CreateInstance(MUStype);

// Access the entire history.
dynamic Updates =
Searcher.QueryHistory(0, Searcher.GetTotalHistoryCount());

// Check each update in the history in turn.
foreach (dynamic Update in Updates)
{
// Display only the successful updates.
if (Update.ResultCode == 2)
{
// Display the update information.

lstResults.Items.Add("Title: " + Update.Title);
lstResults.Items.Add("Description: " + Update.Description);
lstResults.Items.Add("Date (GMT): " + Update.Date);
lstResults.Items.Add("Installation Method: " +
Update.ClientApplicationID);

// Add a blank line.
lstResults.Items.Add("");
}
}
}

The code begins by clearing the previous results from the list box and creating a Type, MUStype, based on Microsoft.Update.Searcher. The code then creates the Search object using Activator.CreateInstance() with MUStype as an argument.

The Searcher object actually has access to a number of methods for querying the update database. The example uses the simplest of these techniques, QueryHistory(). You supply the starting record and number of records to return as inputs. The other methods are documented at http://msdn.microsoft.com/library/aa386530.aspx.

The next step is determining which updates to check. One of the most important properties for developers is the ResultCode. The example looks for updates that succeeded. A result code can have any of the following values:

  • 2: The update succeeded.

  • 4: The update failed for some reason other than that the user or system aborted it.

  • 5: The user or the system aborted the update. An update is aborted either manually or when the system detects an error in the update code or conditions.

  • Other: There could be other update codes that aren't listed. A third-party vendor could provide an update code for a partial update or a pending update. In most cases, you aren't concerned about these alternate conditions and need to know only whether the update succeeded, failed, or aborted.

    Figure 3. It's possible to search the update database for a particular update.
The output information also includes an update title, a description of what the update does, the date the user installed the update, and the technique used to install the update. Figure 3 shows typical output from this part of the example.
Other  
  •  Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 5) - What’s not in Windows Runtime
  •  Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 4) - Language Projections
  •  Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 3) - Metadata in Windows Runtime - Namespaces
  •  Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 2) - Metadata in Windows Runtime - Metadata Format
  •  Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 1) - Windows Runtime Architecture Overview
  •  Windows 8 Architecture from a Developer’s Point of View : Windows 8 Development Architecture
  •  Windows 7 : Programming KMDF Hardware Driver - Mapping Resources - Code to Map Resources
  •  Windows 7 : Programming KMDF Hardware Driver - Handling Interrupts (part 2) - Deferred Processing for Interrupts
  •  Windows 7 : Programming KMDF Hardware Driver - Handling Interrupts (part 1) - Code for EvtInterruptIsr Callback
  •  Windows 7 : Programming KMDF Hardware Driver - Support Device Interrupts (part 2) - Post-Interrupt Enable and Pre-Interrupt Disable Processing
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8