Jump to content

How to debug objects in Autoit


 Share

Go to solution Solved by Andreik,

Recommended Posts

Hello! I'm having a problem with windows update installation, I checked topics related to this but I didn't see or noticed the solution. I think the code has no problem with basic windows and microsoft related updates  but when it comes to the drivers it sometimes fails to download or install it, which wouldn't be a problem but when that happens  my compiled code crashes and I get message  "The requested action with this object has failed", my goal is to even when I'm not able to install  these updates my program wont crash but will continue with skipping or researching updates again,  I tried to debugg it with "If @error" but it obviously doesn't catch any error because it's an object, I also tried to debug it with .ResultCode but my program never gets to that line because it crashes exactly at $session_download.Download(). 


 

Func download_and_install()
    Local $reboot = False
    local $result 
    local $hide_stat

    ; Check if update array exists
    If Not IsArray($arr_updates) Then
        log_this("Update array doesn't exist, restarting update phase.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
        Return
    EndIf

    Local $obj_coll
    log_this("Creating update session.")
    Local $obj_sess = ObjCreate("Microsoft.Update.Session", $sys_info[1])
    If @error Then
        log_this("Error creating object, restarting function.")
        Return
    EndIf

    #Region Download updates
    log_this("Creating update downloader.")
    Local $session_download = $obj_sess.CreateUpdateDownloader()
    If @error Then
        MsgBox(0, "Result", "download error.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
        Return
    EndIf

    log_this("Starting to download updates.")
    $arr_updates[0][1] = 1
    For $c = 1 To $arr_updates[0][0]
        
        ; Skip if update is not selected and hide if requested
        If $arr_updates[$c][3] = 0 Then
            If $prog_update[3][1] = 1 And $prog_update[4][1] = 1 Then
                $hide_stat = $arr_updates[$c][5].IsHidden
                If Not $hide_stat Then $arr_updates[$c][5].IsHidden = True
            EndIf
            ContinueLoop
        EndIf
        
        log_this("Downloading update: " & $arr_updates[$c][0])

        Sleep(10000)
        update_install_data()
        $obj_coll = ObjCreate("Microsoft.Update.UpdateColl")
        If @error Then
            log_this("Error creating object, restarting function.")
            Return
        EndIf
        
        $obj_coll.Add($arr_updates[$c][5])
        If @error Then
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf

        $session_download.Updates = $obj_coll
        If @error Then
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf

        $result = $session_download.Download()
        if $result.ResultCode <> 2 then 
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return  
        EndIf
        log_this("Download Result: " & $result.ResultCode)
        If @error Then
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf

        $arr_updates[0][4] += 1
    Next
    log_this("Downloading updates finished.")
    #EndRegion Download updates

    #Region Install updates
    log_this("Creating update installer.")
    Local $session_install = $obj_sess.CreateUpdateInstaller()
    If @error Then
        MsgBox(0, "Result", "install error.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
        Return
    EndIf

    log_this("Starting to install updates.")
    $arr_updates[0][1] = 2
    For $d = 1 To $arr_updates[0][0]
        
        ; Skip if update is not selected
        If $arr_updates[$d][3] <> 1 Then ContinueLoop
            
        log_this( "Installing update: " & $arr_updates[$d][0])
            
        Sleep(10000)
        update_install_data()
        $obj_coll = ObjCreate("Microsoft.Update.UpdateColl")
        If @error Then
            log_this("Error while creating object, restarting function.")
            Return
        EndIf

        $session_install.Updates = $obj_coll
        If @error Then
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf

        $obj_coll.Add($arr_updates[$d][5])
        If @error Then
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf

        $result = $session_install.Install()
        if $result.ResultCode <> 2 then 
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return  
        EndIf
        If @error Then
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf

        If $result.RebootRequired Then $reboot = True
        $arr_updates[0][5] += 1
    Next
    $prog_update[9][1] += $arr_updates[0][5]
    IniWrite($path_progress, "Update", "UpdatesInstalled", $prog_update[9][1])
    log_this("Update installation finished.")
    #EndRegion Install updates

    
    log_this("Will check for updates again after reboot.")
    $prog_update[1][1] = 4
    IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
    

    If $reboot Then log_this("Reboot required.")

    If $prog_overall[3][1] = "Next" Then allow_exe(1, 0)
    log_this("Rebooting system now.")

    If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalization", "NoLockScreen") = 0 Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalization", "NoLockScreen", "REG_DWORD", 1)
    EndIf

    If IniRead($path_progress, "Overall", "Restart", "") = "Yes" Then
        IniWrite($path_progress, "Overall", "Restart", "No")
    EndIf

    ; Read the current value from the INI file
    Local $restarts = IniRead($path_progress, "AditionalConfig", "Restarts", "0")

    ; Convert the value to an integer and add 1
    If Int($restarts) < 10 Then
        $restarts = Int($restarts) + 1
        IniWrite($path_progress, "AditionalConfig", "Restarts", String($restarts))
        Shutdown(6)
        Exit
    Else 
        IniWrite($path_progress, "AditionalConfig", "Restarts", "0")
        $prog_update[1][1] = 7
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
    EndIf
EndFunc   ;==>download_and_install

If anybody knows how to fix it please let me know.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...