Jump to content

ListView not working correctly


 Share

Recommended Posts

I am writing a program for our school system that will allow students to install software on their PCs without having admin rights. Through what we're calling an "App Store", we can choose what programs they can install/remove.

I started the program with 2 apps, and everything worked great (both were installed on my PC). All the apps are presented in the GUI with a ListView. Now I added a 3rd app that isn't installed, and what happens is that it scans the first app fine, then the 2nd app fine, and then it reaches the 3rd app and it cannot find the program because I don't have it installed. Instead of just finishing the checks, it actually removes the information that I populated in the previous row.

If I don't perform a check on the last program/function (_SpectogramChk()), then everything works fine. I suspect that if I install the program, everything will be ok too. I need it to wrok though without specific programs initially being installed.

If anyone has some time to spare looking over my code to see what I have missed I'd appreciate it.

Hopefully my comments provide a decent enough amount of info for you to see what's happening.

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=D:\Icons\icon.ico
#AutoIt3Wrapper_outfile=App Store.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /om /cs=1 /cn=1
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

;Listview variables
;Adobe Flash
Global $uAdobeFlash, $uAdobeFlashStr, $regAdobeFlash, $aAdobeFlash, $uAdobeFlashVer, $uAdobeFlashDisp
;Adobe Reader
Global $uAdobeReader, $uAdobeReaderStr, $regAdobeReader, $aAdobeReader, $uAdobeReaderVer, $uAdobeReaderDisp
;Spectogram
Global $uSpectogram, $uSpectogramStr, $regSpectogram, $aSpectogram, $uSpectogramVer, $uSpectogramDisp

;Start Interface
$formMain = GUICreate("App Store", 500, 192, 201, 124)
$listSoftware = GUICtrlCreateListView("", 8, 8, 400, 175)
; Add columns
_GUICtrlListView_AddColumn($listSoftware, "Software", 120)
_GUICtrlListView_AddColumn($listSoftware, "Installed", 80)
_GUICtrlListView_AddColumn($listSoftware, "Number of Installs", 115)
_GUICtrlListView_AddColumn($listSoftware, "Availability", 80)
; Add items
_GUICtrlListView_AddItem($listSoftware, "Adobe Flash")
_GUICtrlListView_AddSubItem($listSoftware, 0, "Scanning...", 1)
_GUICtrlListView_AddSubItem($listSoftware, 0, "Scanning...", 2)
_GUICtrlListView_AddSubItem($listSoftware, 0, "Scanning...", 3)
_GUICtrlListView_AddItem($listSoftware, "Adobe Reader")
_GUICtrlListView_AddSubItem($listSoftware, 1, "Scanning...", 1)
_GUICtrlListView_AddSubItem($listSoftware, 1, "Scanning...", 2)
_GUICtrlListView_AddSubItem($listSoftware, 1, "Scanning...", 3)
_GUICtrlListView_AddItem($listSoftware, "Spectogram 16")
_GUICtrlListView_AddSubItem($listSoftware, 2, "Scanning...", 1)
_GUICtrlListView_AddSubItem($listSoftware, 2, "Scanning...", 2)
_GUICtrlListView_AddSubItem($listSoftware, 2, "Scanning...", 3)
$btnInstall = GUICtrlCreateButton("&Install", 417, 112, 75, 25, $WS_GROUP)
$btnUninstall = GUICtrlCreateButton("&Uninstall", 417, 136, 75, 25, $WS_GROUP)
$btnExit = GUICtrlCreateButton("E&xit", 417, 160, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

;Update listview columns
_AdobeFlashChk()
Sleep(1000)
_AdobeReaderChk()
Sleep(1000)
_SpectogramChk()
Sleep(1000)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnExit
            Exit
        Case $btnInstall
            If _GUICtrlListView_GetItemSelected($listSoftware, 0) Then
                _iAdobeFlash()
                _AdobeFlashChk()
            ElseIf _GUICtrlListView_GetItemSelected($listSoftware, 1) Then
                _iAdobeReader()
                _AdobeReaderChk()
            ElseIf _GUICtrlListView_GetItemSelected($listSoftware, 2) Then
                _iSpectogram()
                _SpectogramChk()
            EndIf
        Case $btnUninstall
            If _GUICtrlListView_GetItemSelected($listSoftware, 0) Then
                _uAdobeFlash()
                _AdobeFlashChk()
            ElseIf _GUICtrlListView_GetItemSelected($listSoftware, 1) Then
                _uAdobeReader()
                _AdobeReaderChk()
            ElseIf _GUICtrlListView_GetItemSelected($listSoftware, 2) Then
                _uSpectogram()
                _SpectogramChk()
            EndIf
    EndSwitch
WEnd

;Software Installation functions
Func _iAdobeFlash()
    RunAsWait("username", "domain", "password", 0, "M:\App Store\Adobe Flash - Install.exe")
    $regAdobeFlash = RegRead("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "AdobeFlash")
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "AdobeFlash", "REG_SZ", $regAdobeFlash + 1)
EndFunc   ;==>_iAdobeFlash

Func _iAdobeReader()
    RunAsWait("username", "domain", "password", 0, "M:\App Store\Adobe Reader - Install.exe")
    $regAdobeReader = RegRead("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "AdobeReader")
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "AdobeReader", "REG_SZ", $regAdobeReader + 1)
EndFunc   ;==>_iAdobeReader

Func _iSpectogram()
    RunAsWait("username", "domain", "password", 0, "M:\App Store\gram16.exe")
    $regSpectogram = RegRead("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "Spectogram")
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "Spectogram", "REG_SZ", $regSpectogram + 1)
EndFunc   ;==>_iSpectogram

;Software uninstall functions
Func _uAdobeFlash()
    Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uAdobeFlashDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 11)
        If $uAdobeFlashDisp = "Adobe Flash" Then
            $uAdobeFlashStr = RegRead($uRegKey & "\" & $AppKey, "UninstallString")
            RegDelete($uRegKey, $AppKey)
        EndIf
    Next

    MsgBox(0, "Adobe Flash", "Please make sure any web browsers are closed if open.")
    RunAsWait("username", @ComputerName, "password", 0, @ComSpec & ' /c ' & $uAdobeFlashStr & ' /QB /norestart', "", @SW_HIDE)
EndFunc   ;==>_uAdobeFlash

Func _uAdobeReader()
    Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uAdobeReaderDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 11)
        If $uAdobeReaderDisp = "Adobe Reader" Then
            $uAdobeReaderStr = RegRead($uRegKey & "\" & $AppKey, "UninstallString")
        EndIf
    Next

    MsgBox(0, "Adobe Reader", "Please make sure any web browsers are closed if open.")
    RunAsWait("username", @ComputerName, "password", 0, @ComSpec & ' /c ' & $uAdobeReaderStr & ' /QB /norestart', "", @SW_HIDE)

EndFunc   ;==>_uAdobeReader

Func _uSpectogram()
    Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uSpectogramDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 11)
        If $uSpectogramDisp = "Spectogram" Then
            $uSpectogramStr = RegRead($uRegKey & "\" & $AppKey, "UninstallString")
        EndIf
    Next

    RunAsWait("username", @ComputerName, "password", 0, @ComSpec & ' /c ' & $uSpectogramStr & ' /QB /norestart', "", @SW_HIDE)
EndFunc   ;==>_uSpectogram

;Software version check
Func _AdobeFlashChk()
    Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uAdobeFlashDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 11)
        If $uAdobeFlashDisp = "Adobe Flash" Then
            $uAdobeFlashVer = RegRead($uRegKey & "\" & $AppKey, "DisplayVersion")
        EndIf
    Next

    ;Reload version columns
    _GUICtrlListView_AddSubItem($listSoftware, 0, $uAdobeFlashVer, 1)
    ;Check number of installs
    $regAdobeFlash = RegRead("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "AdobeFlash")
    ;Reload the Number of installs columns
    _GUICtrlListView_AddSubItem($listSoftware, 0, $regAdobeFlash, 2)
    ;Scan software availability
    If FileExists("M:\App Store\Adobe Flash - Install.exe") Then
        $aAdobeFlash = "Available"
    Else
        $aAdobeFlash = "Not available"
    EndIf
    ;Reload the availability columns
    _GUICtrlListView_AddSubItem($listSoftware, 0, $aAdobeFlash, 3)
EndFunc   ;==>_AdobeFlashChk

Func _AdobeReaderChk()
    Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uAdobeReaderDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 12)
        If $uAdobeReaderDisp = "Adobe Reader" Then
            $uAdobeReaderVer = RegRead($uRegKey & "\" & $AppKey, "DisplayVersion")
        EndIf
    Next

    ;Reload the version columns
    _GUICtrlListView_AddSubItem($listSoftware, 1, $uAdobeReaderVer, 1)
    ;Check number of installs
    $regAdobeReader = RegRead("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "AdobeReader")
    ;Reload the Number of installs columns
    _GUICtrlListView_AddSubItem($listSoftware, 1, $regAdobeReader, 2)
    ;Scan software availability
    If FileExists("M:\App Store\Adobe Reader - Install.exe") Then
        $aAdobeReader = "Available"
    Else
        $aAdobeReader = "Not available"
    EndIf
    ;Reload the availability columns
    _GUICtrlListView_AddSubItem($listSoftware, 1, $aAdobeReader, 3)
EndFunc   ;==>_AdobeReaderChk

Func _SpectogramChk()
    Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uSpectogramDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 12)
        If $uSpectogramDisp = "Spectogram" Then
            $uSpectogramVer = RegRead($uRegKey & "\" & $AppKey, "DisplayVersion")
        EndIf
    Next

    ;Reload the version columns
    _GUICtrlListView_AddSubItem($listSoftware, 1, $uSpectogramVer, 1)
    ;Check number of installs
    $regSpectogram = RegRead("HKEY_CURRENT_USER\SOFTWARE\School\InstallLog", "Spectogram")
    ;Reload the Number of installs columns
    _GUICtrlListView_AddSubItem($listSoftware, 1, $regSpectogram, 2)
    ;Scan software availability
    If FileExists("M:\App Store\gram16.exe") Then
        $aSpectogram = "Available"
    Else
        $aSpectogram = "Not available"
    EndIf
    ;Reload the availability columns
    _GUICtrlListView_AddSubItem($listSoftware, 1, $aSpectogram, 3)
EndFunc   ;==>_SpectogramChk

Thanks for your help.

Link to comment
Share on other sites

Is the name of the program spectOgram or spectROgram? I did a quick google on the name spectogram and didn't find anything other than some obvious misspellings on the sites that listed it that way.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Just one improvement:

original

Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    For $i = 1 To 500
        $AppKey = RegEnumKey($uRegKey, $i)
        $uAdobeReaderDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 12)
        If $uAdobeReaderDisp = "Adobe Reader" Then
            $uAdobeReaderVer = RegRead($uRegKey & "\" & $AppKey, "DisplayVersion")
        EndIf
    Next

new

Local $uRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
 $i = 1
    While 1
        $AppKey = RegEnumKey($uRegKey, $i)
    If @error Then ExitLoop
  
        $uAdobeReaderDisp = StringLeft(RegRead($uRegKey & "\" & $AppKey, "DisplayName"), 12)
        If $uAdobeReaderDisp = "Adobe Reader" Then
            $uAdobeReaderVer = RegRead($uRegKey & "\" & $AppKey, "DisplayVersion")
        EndIf
    $i += 1
    WEnd

As you can see there is no limit for number of installed applications anymore.

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...