C# wuapilib - Windows Updates (Console App) failure - invalid cast exception -


environment:

microsoft visual studio 2010 sp1

c# -- .net 4.0

problem:
works fine, except installation.

after thought , research, i suspect problem attempted cast -- “com object of type 'system.__comobject' interface type 'wuapilib.updateinstaller'.” can seen in code, iid , type have been set values of parameters suspect. following runtime error report:

system.invalidcastexception caught. message=specified cast not valid. source=program_name stacktrace: @ wuapilib.iupdateinstaller.install() @ program_name.windowsupdates.installupdates(updatecollection updatestoinstall) in %path%\visual studio 2010\projects\ … innerexception:

request:

after many hours of research , study, resolve problem of installing updates appreciated. please see following code snippets.

program attempts following:

-- windows update services enabled (no problems)

-- host machine needs updated: (no problems)

-- downloads updates: (no problems)

public static updatecollection downloadupdates() {     // returns updatecollection object.  } 

-- attempts install updates. (exception: specified cast not valid.)

public static void installupdates(updatecollection updatestoinstall) {       iupdateinstaller updateinstaller;      console.writeline("creating updateinstasller...");      //type itype = type.gettypefromprogid("microsoft.update.session", true);     //type t = type.gettypefromprogid("microsoft.update.session", "127.0.0.1")     type itype = type.gettypefromprogid("microsoft.update.installer", true);      updateinstaller = (updateinstaller)activator.createinstance(itype);     updateinstaller.updates = updatestoinstall;     updateinstaller.clientapplicationid = "{3442d4fe-224d-4cee-98cf-30e0c4d229e6}"; //    unsure of iid     iinstallationresult installationresult;     console.writeline("creating updateinstallationresult...");     updateinstaller.isforced = true;      if (updateinstaller.isbusy == false)     {        try        {           installationresult = updateinstaller.install(); // ***bad cast exception here.***           if (updatestoinstall.count > 0)           {               (int = 0; < updatestoinstall.count; i++)               {                if (installationresult.getupdateresult(i).hresult == 0)                          console.writeline("installed : " + updatestoinstall[i].title);                     else                          console.writeline("failed : " + updatestoinstall[i].title);                      // reboot required?                     if (installationresult.rebootrequired == true)                         console.writeline("reboot required 1 of more updates.");                } // end           } // end if          } // end try         catch (exception e)         {            console.writeline("error:: " + e.message + "\n");         }      } // end if } // end method 


Comments