Software-Inventory

Developed or developing a new product in PureBasic? Tell the world about it.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Manne.

In some cases you want to know whats installed on each client, especially for big networks.
This little tool reads all Subkeys and values under "Uninstall" from the registry and saves it as .csv (easily import to excel).
You can run this tool as .exe or change it to .exs and install it as service.
Each time the client restarts an actual softinventory will be saved.

Code: Select all

; Autor: Manne
; Datum: 30.03.2003
; PrgName: SoftwareInventory


;- Deklaration der Variablen
DefType.s NodeName, cName, OutDir, OutFile, tOutFile, columnheadings, sName, Subkey, zeile, datum
DefType.l check, hkey, Cnt, pos, lpdwDisposition
NewList subName.s()

;- Zuweisen der Werte
#BUFFER_SIZE = 255
NodeName = Space(256)
cName = "COMPUTERNAME"
columnheadings = "NodeName,DisplayName,Description,Publisher,sVersion,ProductID"
Subkey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
OutDir = "C:\"
hkey = 0

;- Deklaration der Proceduren
Procedure.s ExePath() 
  tmp$ = Space(1000) 
  GetModuleFilename_(0,@tmp$,1000) 
  ProcedureReturn GetPathPart(tmp$) 
EndProcedure 

Procedure.s ReadRegKey(OpenKey.l,SubKey.s,ValueName.s)
  hKey.l=0
  keyvalue.s=Space(255)
  datasize.l=255
  
  If RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey)
    MessageBeep_(#MB_ICONEXCLAMATION)
    End
  Else 
    If RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@datasize)
        keyvalue = ""
    Else  
        keyvalue=Left(keyvalue,datasize-1)
    EndIf
    RegCloseKey_(hKey)
  EndIf
   
  ProcedureReturn keyvalue
EndProcedure  

;- Mainprogramm Registry bearbeiten, Ziel-File schreiben u.a. 
GetEnvironmentVariable_(cName, NodeName, 256)

If NodeName = ""
  End
EndIf

tOutFile = ExePath() + "tmp" + ".csv"
OutFile = OutDir + NodeName + ".csv"


check = CreateFile(0, tOutFile)
WriteStringN(columnheadings)

RegCreateKeyEx_(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\a", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_CREATE_SUB_KEY, 0, @hkey, @lpdwDisposition)
        
    If RegOpenKey_(#HKEY_LOCAL_MACHINE, Subkey, @hKey) = 0          
;       ;Enumerate the keys 
        While RegEnumKeyEx_(hKey, Cnt, sName.s, @Ret, 0, 0, 0, 0)  #ERROR_NO_MORE_ITEMS 
            Cnt = Cnt + 1 
            AddElement(subName())
            subName() = sName
            sName = Space(#BUFFER_SIZE) 
            Ret.l= #BUFFER_SIZE
        Wend 
    Else 
        WriteString("Fehler, konnte Registrierung nicht öffnen!") 
    EndIf        
 RegCloseKey_(hkey)
 
pos = 1

While pos <= Cnt - 1
  SelectElement(subName(), pos)

  DisplayName.s = ReadRegKey(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\"+ subName(), "DisplayName")
  Description.s = ReadRegKey(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\"+ subName(), "Comments")
  Publisher.s = ReadRegKey(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\"+ subName(), "Publisher")
  sVersion.s = ReadRegKey(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\"+ subName(), "DisplayVersion")
  ProductID.s = ReadRegKey(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\"+ subName(), "ProductID")

  If DisplayName = ""
    DisplayName = subName()
  EndIf

  zeile = Nodename + "," + DisplayName + "," + Description + "," + Publisher + "," + sVersion + "," + ProductID
  WriteStringN(zeile)

  pos = pos + 1
Wend

datum = FormatDate("%dd.%mm.%yyyy", Date())
WriteStringN("")
WriteStringN("SoftwareInventory vom "+ datum)
CloseFile(0)

RegDeleteKey_(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Uninstall\a")

CopyFile(tOutFile, OutFile)
DeleteFile(tOutFile)

End
Feedback are welcome!

Manne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Jose.

Manne, you should put this code in the "Tricks 'n' Tips" section.
Post Reply