Author Topic: Check if a program is installed using the registry (WinAPI)  (Read 3305 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Below is code to determine if a program is installed in Windows. If IsInstalled fails then it checks IsInstalled64 to see if it is under the other installation key.

Code: QB64: [Select]
  1.  
  2. If IsInstalled("EaseUS Partition Master 15.0") Then
  3.     Print "Installed"
  4.  
  5. If IsInstalled("PowerShell") Then
  6.     Print "Installed"
  7.  
  8.     Function RegOpenKey& Alias "RegOpenKeyExA" (ByVal hKey As _Offset, Byval lpSubKey As _Offset, Byval ulOptions As _Unsigned Long, Byval samDesired As _Unsigned Long, Byval phkResult As _Offset)
  9.     Function RegEnumKey& Alias "RegEnumKeyExA" (ByVal hKey As _Offset, Byval dwIndex As _Unsigned Long, Byval lpName As _Offset, Byval lpcchName As _Offset, Byval lpReserved As _Offset, Byval lpClass As _Offset, Byval lpcchClass As _Offset, Byval lpftLastWriteTime As _Offset)
  10.     Function RegQueryValue& Alias "RegQueryValueExA" (ByVal hKey As _Offset, lpValueName As String, Byval lpReserved As _Offset, Byval lpType As _Offset, Byval lpData As _Offset, Byval lpcbData As _Offset)
  11.     Sub RegCloseKey (ByVal hKey As _Offset)
  12.  
  13. Function IsInstalled%% (software As String)
  14.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  15.     Const KEY_ALL_ACCESS = &HF003F&
  16.     Const KEY_READ = &H20019&
  17.     Const ERROR_SUCCESS = 0
  18.  
  19.     Dim As _Offset hUninstKey, hAppKey
  20.     Dim As String sAppKeyName, sSubKey, sDisplayName
  21.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  22.     Dim As String sRoot
  23.     sRoot = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  24.     Dim As Long lResult: lResult = ERROR_SUCCESS
  25.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  26.     Dim As Long dwBufferSize
  27.  
  28.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  29.         IsInstalled = 0
  30.         Exit Function
  31.     End If
  32.     Dim As Long dwindex
  33.     Do
  34.         sAppKeyName = Space$(1024)
  35.         dwBufferSize = Len(sAppKeyName)
  36.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  37.         If lResult = ERROR_SUCCESS Then
  38.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  39.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  40.                 RegCloseKey hAppKey
  41.                 RegCloseKey hUninstKey
  42.                 IsInstalled = 0
  43.                 Exit Function
  44.             End If
  45.             dwBufferSize = Len(sDisplayName)
  46.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  47.                 'Print Mid$(sDisplayName, 1, dwBufferSize)
  48.                 If InStr(Mid$(sDisplayName, 1, dwBufferSize), software) Then
  49.                     RegCloseKey hAppKey
  50.                     RegCloseKey hUninstKey
  51.                     IsInstalled = -1
  52.                     Exit Function
  53.                 End If
  54.             End If
  55.             RegCloseKey hAppKey
  56.         End If
  57.         dwindex = dwindex + 1
  58.     Loop While lResult = ERROR_SUCCESS
  59.     RegCloseKey hUninstKey
  60.     IsInstalled = IsInstalled64(software)
  61.  
  62. Function IsInstalled64%% (software As String)
  63.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  64.     Const KEY_ALL_ACCESS = &HF003F&
  65.     Const KEY_READ = &H20019&
  66.     Const ERROR_SUCCESS = 0
  67.  
  68.     Dim As _Offset hUninstKey, hAppKey
  69.     Dim As String sAppKeyName, sSubKey, sDisplayName
  70.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  71.     Dim As String sRoot
  72.     sRoot = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  73.     Dim As Long lResult: lResult = ERROR_SUCCESS
  74.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  75.     Dim As Long dwBufferSize
  76.  
  77.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  78.         IsInstalled64 = 0
  79.         Exit Function
  80.     End If
  81.     Dim As Long dwindex
  82.     Do
  83.         sAppKeyName = Space$(1024)
  84.         dwBufferSize = Len(sAppKeyName)
  85.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  86.         If lResult = ERROR_SUCCESS Then
  87.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  88.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  89.                 RegCloseKey hAppKey
  90.                 RegCloseKey hUninstKey
  91.                 IsInstalled64 = 0
  92.                 Exit Function
  93.             End If
  94.             dwBufferSize = Len(sDisplayName)
  95.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  96.                 'Print Mid$(sDisplayName, 1, dwBufferSize)
  97.                 If InStr(Mid$(sDisplayName, 1, dwBufferSize), software) Then
  98.                     RegCloseKey hAppKey
  99.                     RegCloseKey hUninstKey
  100.                     IsInstalled64 = -1
  101.                     Exit Function
  102.                 End If
  103.             End If
  104.             RegCloseKey hAppKey
  105.         End If
  106.         dwindex = dwindex + 1
  107.     Loop While lResult = ERROR_SUCCESS
  108.     RegCloseKey hUninstKey
  109.     IsInstalled64 = 0
Shuwatch!

Marked as best answer by SpriggsySpriggs on June 18, 2021, 11:07:14 am

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
I've added some more features. Now you can not only check if something is installed, you can also check which version is installed. Also, you can get the "UninstallString" key which can be used with SHELL to uninstall the programs.

Code: QB64: [Select]
  1.  
  2. If IsInstalled("teamviewer") Then
  3.     Print "Installed"
  4.     Print CheckVersion("teamviewer")
  5.     Print GetUninstallString("teamviewer")
  6. Else Print "Not installed"
  7.  
  8. If IsInstalled("powershell 7-x64") Then
  9.     Print "Installed"
  10.     Print CheckVersion("powershell 7-x64")
  11.     Print GetUninstallString("powershell 7-x64")
  12. Else Print "Not installed"
  13.  
  14. If IsInstalled("Google Chrome") Then
  15.     Print "Installed"
  16.     Print CheckVersion("Google Chrome")
  17.     Print GetUninstallString("Google Chrome")
  18.     'Shell GetUninstallString("Google Chrome")
  19. Else Print "Not installed"
  20.  
  21.     Function RegOpenKey& Alias "RegOpenKeyExA" (ByVal hKey As _Offset, Byval lpSubKey As _Offset, Byval ulOptions As _Unsigned Long, Byval samDesired As _Unsigned Long, Byval phkResult As _Offset)
  22.     Function RegEnumKey& Alias "RegEnumKeyExA" (ByVal hKey As _Offset, Byval dwIndex As _Unsigned Long, Byval lpName As _Offset, Byval lpcchName As _Offset, Byval lpReserved As _Offset, Byval lpClass As _Offset, Byval lpcchClass As _Offset, Byval lpftLastWriteTime As _Offset)
  23.     Function RegQueryValue& Alias "RegQueryValueExA" (ByVal hKey As _Offset, lpValueName As String, Byval lpReserved As _Offset, Byval lpType As _Offset, Byval lpData As _Offset, Byval lpcbData As _Offset)
  24.     Sub RegCloseKey (ByVal hKey As _Offset)
  25.  
  26. Function IsInstalled%% (software As String)
  27.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  28.     Const KEY_ALL_ACCESS = &HF003F&
  29.     Const KEY_READ = &H20019&
  30.     Const ERROR_SUCCESS = 0
  31.  
  32.     Dim As _Offset hUninstKey, hAppKey
  33.     Dim As String sAppKeyName, sSubKey, sDisplayName
  34.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  35.     Dim As String sRoot
  36.     sRoot = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  37.     Dim As Long lResult: lResult = ERROR_SUCCESS
  38.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  39.     Dim As Long dwBufferSize
  40.  
  41.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  42.         IsInstalled = 0
  43.         Exit Function
  44.     End If
  45.     Dim As Long dwindex
  46.     Do
  47.         sAppKeyName = Space$(1024)
  48.         dwBufferSize = Len(sAppKeyName)
  49.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  50.         If lResult = ERROR_SUCCESS Then
  51.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  52.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  53.                 RegCloseKey hAppKey
  54.                 RegCloseKey hUninstKey
  55.                 IsInstalled = 0
  56.                 Exit Function
  57.             End If
  58.             dwBufferSize = Len(sDisplayName)
  59.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  60.                 If _StriCmp(Mid$(sDisplayName, 1, dwBufferSize - 1), software) = 0 Then
  61.                     RegCloseKey hAppKey
  62.                     RegCloseKey hUninstKey
  63.                     IsInstalled = -1
  64.                     Exit Function
  65.                 End If
  66.             End If
  67.             RegCloseKey hAppKey
  68.         End If
  69.         dwindex = dwindex + 1
  70.     Loop While lResult = ERROR_SUCCESS
  71.     RegCloseKey hUninstKey
  72.     IsInstalled = IsInstalled64(software)
  73.  
  74. Function IsInstalled64%% (software As String)
  75.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  76.     Const KEY_ALL_ACCESS = &HF003F&
  77.     Const KEY_READ = &H20019&
  78.     Const ERROR_SUCCESS = 0
  79.  
  80.     Dim As _Offset hUninstKey, hAppKey
  81.     Dim As String sAppKeyName, sSubKey, sDisplayName
  82.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  83.     Dim As String sRoot
  84.     sRoot = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  85.     Dim As Long lResult: lResult = ERROR_SUCCESS
  86.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  87.     Dim As Long dwBufferSize
  88.  
  89.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  90.         IsInstalled64 = 0
  91.         Exit Function
  92.     End If
  93.     Dim As Long dwindex
  94.     Do
  95.         sAppKeyName = Space$(1024)
  96.         dwBufferSize = Len(sAppKeyName)
  97.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  98.         If lResult = ERROR_SUCCESS Then
  99.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  100.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  101.                 RegCloseKey hAppKey
  102.                 RegCloseKey hUninstKey
  103.                 IsInstalled64 = 0
  104.                 Exit Function
  105.             End If
  106.             dwBufferSize = Len(sDisplayName)
  107.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  108.                 If _StriCmp(Mid$(sDisplayName, 1, dwBufferSize - 1), software) = 0 Then
  109.                     RegCloseKey hAppKey
  110.                     RegCloseKey hUninstKey
  111.                     IsInstalled64 = -1
  112.                     Exit Function
  113.                 End If
  114.             End If
  115.             RegCloseKey hAppKey
  116.         End If
  117.         dwindex = dwindex + 1
  118.     Loop While lResult = ERROR_SUCCESS
  119.     RegCloseKey hUninstKey
  120.     IsInstalled64 = 0
  121.  
  122. Function CheckVersion$ (software As String)
  123.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  124.     Const KEY_ALL_ACCESS = &HF003F&
  125.     Const KEY_READ = &H20019&
  126.     Const ERROR_SUCCESS = 0
  127.  
  128.     Dim As _Offset hUninstKey, hAppKey
  129.     Dim As String sAppKeyName, sSubKey, sDisplayName
  130.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  131.     Dim As String sRoot
  132.     sRoot = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  133.     Dim As Long lResult: lResult = ERROR_SUCCESS
  134.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  135.     Dim As Long dwBufferSize
  136.  
  137.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  138.         CheckVersion = ""
  139.         Exit Function
  140.     End If
  141.     Dim As Long dwindex
  142.     Do
  143.         sAppKeyName = Space$(1024)
  144.         dwBufferSize = Len(sAppKeyName)
  145.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  146.         If lResult = ERROR_SUCCESS Then
  147.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  148.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  149.                 RegCloseKey hAppKey
  150.                 RegCloseKey hUninstKey
  151.                 CheckVersion = ""
  152.                 Exit Function
  153.             End If
  154.             dwBufferSize = Len(sDisplayName)
  155.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  156.                 If _StriCmp(Mid$(sDisplayName, 1, dwBufferSize - 1), software) = 0 Then
  157.                     Dim As String version: version = Space$(1024)
  158.                     Dim As Long verlen: verlen = Len(version)
  159.                     If RegQueryValue(hAppKey, "DisplayVersion" + Chr$(0), 0, _Offset(dwType), _Offset(version), _Offset(verlen)) = ERROR_SUCCESS Then
  160.                         CheckVersion = Mid$(version, 1, verlen - 1)
  161.                     Else
  162.                         CheckVersion = ""
  163.                     End If
  164.                     RegCloseKey hAppKey
  165.                     RegCloseKey hUninstKey
  166.                     Exit Function
  167.                 End If
  168.             End If
  169.             RegCloseKey hAppKey
  170.         End If
  171.         dwindex = dwindex + 1
  172.     Loop While lResult = ERROR_SUCCESS
  173.     RegCloseKey hUninstKey
  174.     CheckVersion = CheckVersion64(software)
  175.  
  176. Function CheckVersion64$ (software As String)
  177.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  178.     Const KEY_ALL_ACCESS = &HF003F&
  179.     Const KEY_READ = &H20019&
  180.     Const ERROR_SUCCESS = 0
  181.  
  182.     Dim As _Offset hUninstKey, hAppKey
  183.     Dim As String sAppKeyName, sSubKey, sDisplayName
  184.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  185.     Dim As String sRoot
  186.     sRoot = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  187.     Dim As Long lResult: lResult = ERROR_SUCCESS
  188.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  189.     Dim As Long dwBufferSize
  190.  
  191.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  192.         CheckVersion64 = ""
  193.         Exit Function
  194.     End If
  195.     Dim As Long dwindex
  196.     Do
  197.         sAppKeyName = Space$(1024)
  198.         dwBufferSize = Len(sAppKeyName)
  199.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  200.         If lResult = ERROR_SUCCESS Then
  201.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  202.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  203.                 RegCloseKey hAppKey
  204.                 RegCloseKey hUninstKey
  205.                 CheckVersion64 = ""
  206.                 Exit Function
  207.             End If
  208.             dwBufferSize = Len(sDisplayName)
  209.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  210.                 If _StriCmp(Mid$(sDisplayName, 1, dwBufferSize - 1), software) = 0 Then
  211.                     Dim As String version: version = Space$(1024)
  212.                     Dim As Long verlen: verlen = Len(version)
  213.                     If RegQueryValue(hAppKey, "DisplayVersion" + Chr$(0), 0, _Offset(dwType), _Offset(version), _Offset(verlen)) = ERROR_SUCCESS Then
  214.                         CheckVersion64 = Mid$(version, 1, verlen - 1)
  215.                     Else
  216.                         CheckVersion64 = ""
  217.                     End If
  218.                     RegCloseKey hAppKey
  219.                     RegCloseKey hUninstKey
  220.                     Exit Function
  221.                 End If
  222.             End If
  223.             RegCloseKey hAppKey
  224.         End If
  225.         dwindex = dwindex + 1
  226.     Loop While lResult = ERROR_SUCCESS
  227.     RegCloseKey hUninstKey
  228.     CheckVersion64 = ""
  229.  
  230. Function GetUninstallString$ (software As String)
  231.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  232.     Const KEY_ALL_ACCESS = &HF003F&
  233.     Const KEY_READ = &H20019&
  234.     Const ERROR_SUCCESS = 0
  235.  
  236.     Dim As _Offset hUninstKey, hAppKey
  237.     Dim As String sAppKeyName, sSubKey, sDisplayName
  238.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  239.     Dim As String sRoot
  240.     sRoot = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  241.     Dim As Long lResult: lResult = ERROR_SUCCESS
  242.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  243.     Dim As Long dwBufferSize
  244.  
  245.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  246.         GetUninstallString = ""
  247.         Exit Function
  248.     End If
  249.     Dim As Long dwindex
  250.     Do
  251.         sAppKeyName = Space$(1024)
  252.         dwBufferSize = Len(sAppKeyName)
  253.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  254.         If lResult = ERROR_SUCCESS Then
  255.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  256.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  257.                 RegCloseKey hAppKey
  258.                 RegCloseKey hUninstKey
  259.                 GetUninstallString = ""
  260.                 Exit Function
  261.             End If
  262.             dwBufferSize = Len(sDisplayName)
  263.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  264.                 If _StriCmp(Mid$(sDisplayName, 1, dwBufferSize - 1), software) = 0 Then
  265.                     Dim As String uninstall: uninstall = Space$(1024)
  266.                     Dim As Long Lenuninstall: Lenuninstall = Len(uninstall)
  267.                     If RegQueryValue(hAppKey, "UninstallString" + Chr$(0), 0, _Offset(dwType), _Offset(uninstall), _Offset(Lenuninstall)) = ERROR_SUCCESS Then
  268.                         GetUninstallString = Mid$(uninstall, 1, Lenuninstall - 1)
  269.                     Else
  270.                         GetUninstallString = ""
  271.                     End If
  272.                     RegCloseKey hAppKey
  273.                     RegCloseKey hUninstKey
  274.                     Exit Function
  275.                 End If
  276.             End If
  277.             RegCloseKey hAppKey
  278.         End If
  279.         dwindex = dwindex + 1
  280.     Loop While lResult = ERROR_SUCCESS
  281.     RegCloseKey hUninstKey
  282.     GetUninstallString = GetUninstallString64(software)
  283.  
  284. Function GetUninstallString64$ (software As String)
  285.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  286.     Const KEY_ALL_ACCESS = &HF003F&
  287.     Const KEY_READ = &H20019&
  288.     Const ERROR_SUCCESS = 0
  289.  
  290.     Dim As _Offset hUninstKey, hAppKey
  291.     Dim As String sAppKeyName, sSubKey, sDisplayName
  292.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  293.     Dim As String sRoot
  294.     sRoot = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  295.     Dim As Long lResult: lResult = ERROR_SUCCESS
  296.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  297.     Dim As Long dwBufferSize
  298.  
  299.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  300.         GetUninstallString64 = ""
  301.         Exit Function
  302.     End If
  303.     Dim As Long dwindex
  304.     Do
  305.         sAppKeyName = Space$(1024)
  306.         dwBufferSize = Len(sAppKeyName)
  307.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  308.         If lResult = ERROR_SUCCESS Then
  309.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  310.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  311.                 RegCloseKey hAppKey
  312.                 RegCloseKey hUninstKey
  313.                 GetUninstallString64 = ""
  314.                 Exit Function
  315.             End If
  316.             dwBufferSize = Len(sDisplayName)
  317.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  318.                 If _StriCmp(Mid$(sDisplayName, 1, dwBufferSize - 1), software) = 0 Then
  319.                     Dim As String uninstall: uninstall = Space$(1024)
  320.                     Dim As Long Lenuninstall: Lenuninstall = Len(uninstall)
  321.                     If RegQueryValue(hAppKey, "UninstallString" + Chr$(0), 0, _Offset(dwType), _Offset(uninstall), _Offset(Lenuninstall)) = ERROR_SUCCESS Then
  322.                         GetUninstallString64 = Mid$(uninstall, 1, Lenuninstall - 1)
  323.                     Else
  324.                         GetUninstallString64 = ""
  325.                     End If
  326.                     RegCloseKey hAppKey
  327.                     RegCloseKey hUninstKey
  328.                     Exit Function
  329.                 End If
  330.             End If
  331.             RegCloseKey hAppKey
  332.         End If
  333.         dwindex = dwindex + 1
  334.     Loop While lResult = ERROR_SUCCESS
  335.     RegCloseKey hUninstKey
  336.     GetUninstallString64 = ""
  337.  
  338. Sub EnumInstalledPrograms
  339.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  340.     Const KEY_ALL_ACCESS = &HF003F&
  341.     Const KEY_READ = &H20019&
  342.     Const ERROR_SUCCESS = 0
  343.  
  344.     Dim As _Offset hUninstKey, hAppKey
  345.     Dim As String sAppKeyName, sSubKey, sDisplayName
  346.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  347.     Dim As String sRoot
  348.     sRoot = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  349.     Dim As Long lResult: lResult = ERROR_SUCCESS
  350.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  351.     Dim As Long dwBufferSize
  352.  
  353.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  354.         Exit Sub
  355.     End If
  356.     Dim As Long dwindex
  357.     Do
  358.         sAppKeyName = Space$(1024)
  359.         dwBufferSize = Len(sAppKeyName)
  360.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  361.         If lResult = ERROR_SUCCESS Then
  362.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  363.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  364.                 RegCloseKey hAppKey
  365.                 RegCloseKey hUninstKey
  366.             End If
  367.             dwBufferSize = Len(sDisplayName)
  368.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  369.                 Print Mid$(sDisplayName, 1, dwBufferSize)
  370.             End If
  371.             RegCloseKey hAppKey
  372.         End If
  373.         dwindex = dwindex + 1
  374.     Loop While lResult = ERROR_SUCCESS
  375.     RegCloseKey hUninstKey
  376.     EnumInstalledPrograms64
  377.  
  378. Sub EnumInstalledPrograms64
  379.     Const HKEY_LOCAL_MACHINE = &H80000002~&
  380.     Const KEY_ALL_ACCESS = &HF003F&
  381.     Const KEY_READ = &H20019&
  382.     Const ERROR_SUCCESS = 0
  383.  
  384.     Dim As _Offset hUninstKey, hAppKey
  385.     Dim As String sAppKeyName, sSubKey, sDisplayName
  386.     sAppKeyName = Space$(1024): sSubKey = Space$(1024): sDisplayName = Space$(1024)
  387.     Dim As String sRoot
  388.     sRoot = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" + Chr$(0)
  389.     Dim As Long lResult: lResult = ERROR_SUCCESS
  390.     Dim As Long dwType: dwType = KEY_ALL_ACCESS
  391.     Dim As Long dwBufferSize
  392.  
  393.     If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sRoot), 0, KEY_READ, _Offset(hUninstKey)) <> ERROR_SUCCESS Then
  394.         Exit Sub
  395.     End If
  396.     Dim As Long dwindex
  397.     Do
  398.         sAppKeyName = Space$(1024)
  399.         dwBufferSize = Len(sAppKeyName)
  400.         lResult = RegEnumKey(hUninstKey, dwindex, _Offset(sAppKeyName), _Offset(dwBufferSize), 0, 0, 0, 0)
  401.         If lResult = ERROR_SUCCESS Then
  402.             sSubKey = Mid$(sRoot, 1, InStr(sRoot, Chr$(0)) - 1) + "\" + Mid$(sAppKeyName, 1, InStr(sAppKeyName, Chr$(0)) - 1) + Chr$(0)
  403.             If RegOpenKey(HKEY_LOCAL_MACHINE, _Offset(sSubKey), 0, KEY_READ, _Offset(hAppKey)) <> ERROR_SUCCESS Then
  404.                 RegCloseKey hAppKey
  405.                 RegCloseKey hUninstKey
  406.             End If
  407.             dwBufferSize = Len(sDisplayName)
  408.             If RegQueryValue(hAppKey, "DisplayName" + Chr$(0), 0, _Offset(dwType), _Offset(sDisplayName), _Offset(dwBufferSize)) = ERROR_SUCCESS Then
  409.                 Print Mid$(sDisplayName, 1, dwBufferSize)
  410.             End If
  411.             RegCloseKey hAppKey
  412.         End If
  413.         dwindex = dwindex + 1
  414.     Loop While lResult = ERROR_SUCCESS
  415.     RegCloseKey hUninstKey

 
Screenshot 2021-06-18 150828.png
« Last Edit: June 18, 2021, 03:08:46 pm by SpriggsySpriggs »
Shuwatch!