home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch16code / verify.bas < prev    next >
Encoding:
BASIC Source File  |  1995-08-12  |  6.6 KB  |  208 lines

  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3. ' Registration APIs used to check entry.
  4. #If Win16 Then
  5. Declare Function RegOpenKey Lib "Shell" _
  6.     (ByVal HKeyIn As Long, _
  7.     ByVal LPCSTR As String, _
  8.     HKeyOut As Long) _
  9.     As Long
  10. Declare Function RegCloseKey Lib "Shell" _
  11.     (ByVal HKeyIn As Long) _
  12.     As Long
  13. Declare Function RegEnumKey Lib "Shell" _
  14.     (ByVal HKeyIn As Long, ByVal SubKeyIn As Long, _
  15.     ByVal KeyName As String, ByVal KeyNameLen As Long) As Long
  16. Declare Function RegQueryValue Lib "Shell" _
  17.     (ByVal HKeyIn As Long, _
  18.     ByVal SubKey As String, _
  19.     ByVal KeyValue As String, _
  20.     KeyValueLen As Long) _
  21.     As Long
  22. Declare Function RegSetValue Lib "Shell" _
  23.     (ByVal HKeyIn As Long, _
  24.     ByVal SubKey As String, _
  25.     ByVal lType As Long, _
  26.     ByVal strNewValue As String, _
  27.     ByVal lIngnored As Long) _
  28.     As Long
  29. Declare Sub RegDeleteKey Lib "Shell" _
  30.     (ByVal HKeyIn As Long, _
  31.     ByVal SubKeyName As String)
  32. #Else
  33. Declare Function RegOpenKey Lib "advapi32" _
  34.     Alias "RegOpenKeyA" _
  35.     (ByVal HKeyIn As Long, _
  36.     ByVal LPCSTR As String, _
  37.     HKeyOut As Long) _
  38.     As Long
  39. Declare Function RegOpenKeyEx Lib "advapi32" _
  40.     Alias "RegOpenKeyExA" _
  41.     (ByVal HKeyIn As Long, ByVal LPCSTR _
  42.     As String, ByVal dwRes _
  43.     As Long, ByVal dwAccess _
  44.     As Long, HKeyOut As _
  45.     Long) As Long
  46. Declare Function RegCloseKey Lib "advapi32" _
  47.     (ByVal HKeyIn As Long) _
  48.     As Long
  49. Private Declare Function RegEnumKey Lib "advapi32" _
  50.     Alias "RegEnumKeyA" _
  51.     (ByVal hKey As Long, _
  52.     ByVal dwIndex As Long, _
  53.     ByVal lpName As String, _
  54.     ByVal cbName As Long) _
  55.     As Long
  56. Declare Function RegQueryValue Lib "advapi32" _
  57.     Alias "RegQueryValueA" _
  58.     (ByVal HKeyIn As Long, _
  59.     ByVal SubKey As String, _
  60.     ByVal KeyValue As String, _
  61.     KeyValueLen As Long) _
  62.     As Long
  63. Declare Function RegSetValue Lib "advapi32" _
  64.     Alias "RegSetValueA" _
  65.     (ByVal HKeyIn As Long, _
  66.     ByVal SubKey As String, _
  67.     ByVal lType As Long, _
  68.     ByVal strNewValue As String, _
  69.     ByVal lIngnored As Long) _
  70.     As Long
  71. Declare Function RegDeleteKey Lib "advapi32" _
  72.     Alias "RegDeleteKeyA" _
  73.     (ByVal HKeyIn As Long, _
  74.     ByVal SubKeyName As String) _
  75.     As Long
  76. #End If
  77.  
  78. #If Win16 Then
  79.     Const HKEY_CLASSES_ROOT = &H1
  80. #Else
  81.     Const HKEY_CLASSES_ROOT = &H80000000
  82.     Const HKEY_CURRENT_USER = &H80000001
  83.     Const HKEY_LOCAL_MACHINE = &H80000002
  84.     Const HKEY_USERS = &H80000003
  85.     Const HKEY_PERFORMANCE_DATA = &H80000004
  86. #End If
  87.  
  88. Public Const ERROR_SUCCESS = 0
  89.  
  90.  
  91.  
  92. Sub Main()
  93.     Dim strNotFound As String
  94.     Dim bWorked As Boolean
  95.     bWorked = CheckInstalled(Array("Microsoft Excel", "Microsoft Word"), strNotFound)
  96.     If bWorked Then
  97.         ' All required applications available -- continue.
  98.     Else
  99.         MsgBox "The following required applications were not found: " & strNotFound
  100.     End If
  101. End Sub
  102.  
  103. Function CheckInstalled(RequiredList, strNotFound As String) As Variant
  104.     Dim InstalledList As Variant
  105.     Dim InstalledName, RequiredName
  106.     Dim bFound As Boolean
  107.     Dim AppName As String
  108.     InstalledList = GetRegisteredList()
  109.     For Each RequiredName In RequiredList
  110.         For Each InstalledName In InstalledList
  111.             If InStr(InstalledName, RequiredName) Then
  112.                 bFound = True
  113.                 Exit For
  114.             End If
  115.             bFound = False
  116.         Next InstalledName
  117.         ' Build list of application that weren't found.
  118.         If bFound = False Then
  119.             strNotFound = strNotFound & ", " & RequiredName
  120.         End If
  121.     Next RequiredName
  122.     If Len(strNotFound) Then
  123.         strNotFound = Right(strNotFound, Len(strNotFound) - 2)
  124.         CheckInstalled = False
  125.     Else
  126.         CheckInstalled = True
  127.         strNotFound = ""
  128.     End If
  129. End Function
  130.  
  131. Function GetRegisteredList() As Variant
  132.     Dim hkroot As Long, x As Long, lLen As Long
  133.     ReDim Installed(99)
  134.     Dim strKeyID As String * 80, strKeyDesc As String * 80, iKeyCount As Integer
  135.     x = RegOpenKey(HKEY_CLASSES_ROOT, "", hkroot)
  136.     lLen = 255
  137.     Do
  138.         strKeyID = String(lLen, 0)
  139.         If RegEnumKey(hkroot, iKeyCount, strKeyID, lLen) = 0 Then
  140.             lLen = 255
  141.             ' If the string is not a file extension (.doc, .xls, etc.)
  142.             If Mid(strKeyID, 1, 1) <> "." Then
  143.                 strKeyDesc = String(lLen, 0)
  144.                 ' Get the value of the registry entry.
  145.                 x = RegQueryValue(hkroot, strKeyID, strKeyDesc, lLen)
  146.                 Installed(iKeyCount) = strKeyDesc
  147.                 lLen = 255
  148.             End If
  149.             iKeyCount = iKeyCount + 1
  150.             If iKeyCount > UBound(Installed) Then
  151.                 ' Add elements if the array gets full.
  152.                 ReDim Preserve Installed(UBound(Installed) + 100)
  153.             End If
  154.         Else
  155.             Exit Do
  156.         End If
  157.     Loop
  158.     ' Trim off excess array elements.
  159.     ReDim Preserve Installed(iKeyCount)
  160.     x = RegCloseKey(hkroot)
  161.     GetRegisteredList = Installed
  162. End Function
  163.  
  164. Function sTrim(sIn) As String
  165.     Dim iCount As Integer
  166.     For iCount = Len(sIn) To 1 Step -1
  167.         If Asc(Mid$(sIn, iCount, iCount)) <> 0 Then
  168.             sTrim = Mid$(sIn, 1, iCount)
  169.             Exit Function
  170.         End If
  171.     Next iCount
  172. End Function
  173.  
  174. Function GetRegisteredKeys(Optional sKeyName As Variant) As Variant
  175.     Dim hkroot As Long, x As Long, lLen As Long
  176.     Dim strKeyID As String * 80, strKeyDesc As String * 80, iKeyCount As Integer
  177.     ReDim KeyArray(1, 1) As String
  178.     If IsMissing(sKeyName) Then sKeyName = ""
  179.     x = RegOpenKey(HKEY_CLASSES_ROOT, sKeyName, hkroot)
  180.     lLen = 80
  181.     Do
  182.         strKeyID = String(lLen, 0)
  183.         If RegEnumKey(hkroot, iKeyCount, strKeyID, lLen) = 0 Then
  184.             lLen = 80
  185.             If Mid(strKeyID, 1, 1) <> "." Then
  186.                 KeyArray(iKeyCount, 0) = sTrim(strKeyID)
  187.                 strKeyDesc = String(lLen, 0)
  188.                 x = RegQueryValue(hkroot, strKeyID, strKeyDesc, lLen)
  189.                 lLen = 80
  190.                 KeyArray(iKeyCount, 1) = sTrim(strKeyDesc)
  191.             End If
  192.             iKeyCount = iKeyCount + 1
  193.             ' Reserve more space for the array.
  194.             If iKeyCount > UBound(KeyArray, 1) Then
  195.                 ReDim Preserve KeyArray(iKeyCount + 100, iKeyCount + 100)
  196.             End If
  197.         Else
  198.             Exit Do
  199.         End If
  200.     Loop
  201.     x = RegCloseKey(hkroot)
  202.     ' Trim off excess from array
  203.     If iKeyCount <= UBound(KeyArray, 1) Then
  204.         ReDim Preserve KeyArray(iKeyCount - 1, iKeyCount - 1)
  205.     End If
  206.     GetRegisteredKeys = KeyArray
  207. End Function
  208.